2018-06-28 18:20:08 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
"""printer.views
|
|
|
|
The views for the printer app
|
|
|
|
Author : Maxime Bombar <bombar@crans.org>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.forms import modelformset_factory, formset_factory
|
2018-09-08 10:46:52 +00:00
|
|
|
from django.forms.models import model_to_dict
|
2018-06-29 04:06:52 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2018-10-20 00:24:54 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-06-28 18:20:08 +00:00
|
|
|
|
|
|
|
from re2o.views import form
|
|
|
|
from users.models import User
|
|
|
|
|
|
|
|
from . import settings
|
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
from .utils import pdfinfo
|
|
|
|
|
|
|
|
from .models import (
|
|
|
|
JobWithOptions,
|
|
|
|
)
|
|
|
|
|
2018-06-28 18:20:08 +00:00
|
|
|
from .forms import (
|
2018-06-29 03:05:15 +00:00
|
|
|
JobWithOptionsForm,
|
2018-09-08 10:46:52 +00:00
|
|
|
PrintForm,
|
2018-06-28 18:20:08 +00:00
|
|
|
)
|
|
|
|
|
2018-09-08 22:51:18 +00:00
|
|
|
|
|
|
|
from cotisations.models import(
|
|
|
|
Paiement,
|
|
|
|
Facture,
|
|
|
|
Vente,
|
|
|
|
)
|
|
|
|
|
|
|
|
from cotisations.utils import find_payment_method
|
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
2018-06-29 04:06:52 +00:00
|
|
|
@login_required
|
2018-06-28 18:20:08 +00:00
|
|
|
def new_job(request):
|
|
|
|
"""
|
|
|
|
View to create a new printing job
|
|
|
|
"""
|
2018-09-08 10:46:52 +00:00
|
|
|
if request.method == 'POST':
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise Exception('coucou3')
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES})
|
|
|
|
|
2018-10-20 00:27:12 +00:00
|
|
|
job_formset = formset_factory(JobWithOptionsForm)(
|
|
|
|
request.POST,
|
|
|
|
request.FILES or None,
|
|
|
|
form_kwargs={'user': request.user},
|
|
|
|
)
|
|
|
|
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES})
|
|
|
|
|
|
|
|
|
2018-10-20 00:27:12 +00:00
|
|
|
tmp_job_formset = job_formset
|
2018-09-08 12:04:44 +00:00
|
|
|
|
2018-10-20 00:27:12 +00:00
|
|
|
if request.FILES:
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise Exception("Coucou4")
|
2018-10-16 11:47:52 +00:00
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
if job_formset.is_valid():
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise Exception("Valide !!!")
|
2018-09-08 10:46:52 +00:00
|
|
|
files = request.FILES
|
|
|
|
data = []
|
|
|
|
i=0
|
2018-10-21 17:48:10 +00:00
|
|
|
# request.POST['jids'] = []
|
|
|
|
jids = []
|
|
|
|
for job in job_formset.forms:
|
|
|
|
if job.is_valid():
|
|
|
|
try:
|
|
|
|
filename = job.cleaned_data['file'].name
|
|
|
|
except KeyError:
|
|
|
|
raise ValidationError("'%(path)s'", code='path', params = {'path': job})
|
|
|
|
job = job.save(commit=False)
|
|
|
|
job.filename = filename
|
|
|
|
job.user=request.user
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': job.printAs})
|
|
|
|
if job.printAs is None:
|
|
|
|
job.printAs = request.user
|
|
|
|
job.status='Pending'
|
|
|
|
# raise
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES['form-%s-file' % i].temporary_file_path()})
|
|
|
|
# job_data = model_to_dict(job)
|
|
|
|
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': bool(job.printAs is None) })
|
|
|
|
metadata = pdfinfo(request.FILES['form-%s-file' % i].temporary_file_path())
|
|
|
|
job.pages = metadata["Pages"]
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': type(job)})
|
|
|
|
# job.save()
|
|
|
|
# job_data = model_to_dict(job)
|
|
|
|
# job_data['file'] = request.FILES['form-%s-file' % i]
|
|
|
|
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job_data })
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.session })
|
|
|
|
job._update_price()
|
|
|
|
job.save()
|
|
|
|
job_data = model_to_dict(job)
|
|
|
|
jids.append(job.id)
|
|
|
|
# request.session['id-form-%s-file' % i] = job.id
|
|
|
|
# request.session['form-%s-file' % i] = request.FILES['form-%s-file' % i].temporary_file_path()
|
|
|
|
job_data['jid'] = job.id
|
|
|
|
data.append(job_data)
|
|
|
|
i+=1
|
2018-10-15 00:03:13 +00:00
|
|
|
job_formset_filled_in = formset_factory(PrintForm, extra=0)(
|
|
|
|
initial=data,
|
2018-10-21 17:48:10 +00:00
|
|
|
# jids=jids,
|
|
|
|
form_kwargs={'user': request.user,
|
|
|
|
},
|
2018-10-15 00:03:13 +00:00
|
|
|
)
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': job_formset_filled_in.forms })
|
|
|
|
|
|
|
|
# request.POST['job_ids'] = jids
|
2018-09-08 10:46:52 +00:00
|
|
|
return form(
|
|
|
|
{
|
|
|
|
'jobform': job_formset_filled_in,
|
|
|
|
'action_name' : 'Print',
|
|
|
|
},
|
|
|
|
'printer/print.html',
|
|
|
|
request
|
|
|
|
)
|
2018-10-21 17:48:10 +00:00
|
|
|
else:
|
|
|
|
job_formset = tmp_job_formset
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
'jobform': job_formset,
|
|
|
|
'action_name': _("Next"),
|
|
|
|
},
|
|
|
|
'printer/newjob.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
|
|
|
# else:
|
|
|
|
# job_formset = tmp_job_formset
|
|
|
|
# return form(
|
|
|
|
# {
|
|
|
|
# 'jobform': job_formset,
|
|
|
|
# 'action_name': _("Next"),
|
|
|
|
# },
|
|
|
|
# 'printer/newjob.html',
|
|
|
|
# request
|
|
|
|
# )
|
2018-09-08 12:04:44 +00:00
|
|
|
|
2018-10-21 17:48:10 +00:00
|
|
|
|
|
|
|
# else:
|
|
|
|
# raise Exception("Coucou5")
|
2018-09-08 10:46:52 +00:00
|
|
|
# elif 'Print' in request.POST:
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.POST })
|
2018-09-08 12:04:44 +00:00
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
# raise Exception('On a déjà upload !')
|
2018-10-21 17:48:10 +00:00
|
|
|
# n = int(request.POST['form-TOTAL_FORMS'])
|
2018-09-08 10:46:52 +00:00
|
|
|
job_formset = formset_factory(PrintForm)(
|
|
|
|
request.POST,
|
2018-10-15 00:03:13 +00:00
|
|
|
form_kwargs={'user': request.user},
|
2018-09-08 10:46:52 +00:00
|
|
|
)
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.POST })
|
|
|
|
# jids = request.POST['job_ids']
|
2018-09-08 10:46:52 +00:00
|
|
|
if job_formset.is_valid():
|
2018-10-21 17:48:10 +00:00
|
|
|
for job_form in job_formset:
|
2018-09-08 10:46:52 +00:00
|
|
|
i=0
|
2018-10-21 17:48:10 +00:00
|
|
|
# old_job = JobWithOptions.objects.get(id=jids[i])
|
|
|
|
jid = job_form.cleaned_data['jid']
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': job_obj.cleaned_data })
|
|
|
|
job = JobWithOptions.objects.get(id=jid)
|
|
|
|
# job = job_obj.save(commit=False)
|
2018-09-08 10:46:52 +00:00
|
|
|
job.user = request.user
|
2018-09-25 11:53:30 +00:00
|
|
|
job.status = 'Printable'
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job.file})
|
|
|
|
# job.file = old_job.file
|
2018-09-08 12:04:44 +00:00
|
|
|
job._update_price()
|
2018-09-08 10:46:52 +00:00
|
|
|
job.save()
|
|
|
|
i+=1
|
|
|
|
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': request.method})
|
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': str(n) })
|
2018-10-21 17:48:10 +00:00
|
|
|
# request.session['jids']=jids
|
2018-09-08 22:51:18 +00:00
|
|
|
return redirect('printer:payment')
|
|
|
|
|
2018-09-08 12:04:44 +00:00
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
else:
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise Exception("Coucou2")
|
2018-09-08 10:46:52 +00:00
|
|
|
job_formset = formset_factory(JobWithOptionsForm)(
|
2018-10-15 00:03:13 +00:00
|
|
|
form_kwargs={'user': request.user}
|
|
|
|
)
|
2018-10-21 17:48:10 +00:00
|
|
|
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job_formset})
|
|
|
|
|
2018-06-28 18:20:08 +00:00
|
|
|
return form(
|
|
|
|
{
|
|
|
|
'jobform': job_formset,
|
2018-10-20 00:24:54 +00:00
|
|
|
'action_name': _("Next"),
|
2018-06-28 18:20:08 +00:00
|
|
|
},
|
|
|
|
'printer/newjob.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
2018-09-08 22:51:18 +00:00
|
|
|
def payment(request):
|
|
|
|
"""
|
|
|
|
View used to create a new invoice and make the payment
|
|
|
|
"""
|
|
|
|
# user = request.user
|
2018-10-21 17:48:10 +00:00
|
|
|
# jids = request.session['jids']
|
2018-09-08 22:51:18 +00:00
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': jids})
|
2018-10-21 17:48:10 +00:00
|
|
|
jobs = JobWithOptions.objects.filter(user=request.user, status='Printable', paid='False')
|
2018-09-08 22:51:18 +00:00
|
|
|
users = {}
|
|
|
|
for job in jobs:
|
|
|
|
try:
|
|
|
|
users[job.printAs]+=job.price
|
2018-10-21 17:48:10 +00:00
|
|
|
job.paid = True
|
|
|
|
job.save()
|
2018-09-08 22:51:18 +00:00
|
|
|
except KeyError:
|
|
|
|
users[job.printAs]=job.price
|
2018-10-21 17:48:10 +00:00
|
|
|
job.paid = True
|
|
|
|
job.save()
|
2018-09-08 22:51:18 +00:00
|
|
|
|
|
|
|
for user in users:
|
|
|
|
# If payment_method balance doesn't exist, then you're not allowed to print.
|
|
|
|
try:
|
|
|
|
balance = find_payment_method(Paiement.objects.get(is_balance=True))
|
|
|
|
except Paiement.DoesNotExist:
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("You are not allowed to print")
|
|
|
|
)
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': request.user.id}
|
|
|
|
))
|
|
|
|
invoice = Facture(user=user)
|
|
|
|
invoice.paiement = balance.payment
|
|
|
|
# invoice.valid = True
|
|
|
|
invoice.save()
|
|
|
|
Vente.objects.create(
|
|
|
|
facture=invoice,
|
2018-10-15 00:13:36 +00:00
|
|
|
name='Printing (requested by %s)' % request.user,
|
2018-09-08 22:51:18 +00:00
|
|
|
prix=users[user],
|
|
|
|
number=1,
|
|
|
|
)
|
|
|
|
invoice.paiement.end_payment(invoice, request)
|
|
|
|
return redirect(reverse(
|
|
|
|
'printer:success',
|
|
|
|
))
|
|
|
|
|
2018-06-28 18:20:08 +00:00
|
|
|
def success(request):
|
|
|
|
return form(
|
|
|
|
{},
|
|
|
|
'printer/success.html',
|
|
|
|
request
|
|
|
|
)
|