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-10-21 23:12:49 +00:00
|
|
|
from .utils import pdfinfo, send_mail_printer
|
2018-09-08 10:46:52 +00:00
|
|
|
|
|
|
|
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-10-23 18:10:21 +00:00
|
|
|
from cotisations.payment_methods.balance.models import BalancePayment
|
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
2018-10-21 20:31:21 +00:00
|
|
|
# raise ValidationError("'%(path)s'", code='path', params = {'path': job.printAs})
|
|
|
|
|
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
|
|
|
|
2018-10-21 20:31:21 +00:00
|
|
|
### First Step
|
|
|
|
if 'Next' in request.POST:
|
|
|
|
job_formset = formset_factory(JobWithOptionsForm)(
|
|
|
|
request.POST,
|
|
|
|
request.FILES or None,
|
|
|
|
form_kwargs={'user': request.user},
|
|
|
|
)
|
2018-10-21 17:48:10 +00:00
|
|
|
|
2018-09-08 10:46:52 +00:00
|
|
|
if job_formset.is_valid():
|
|
|
|
data = []
|
|
|
|
i=0
|
2018-10-21 20:31:21 +00:00
|
|
|
for job in job_formset:
|
|
|
|
### Fails if one of the forms is submitted without a file.
|
|
|
|
try:
|
|
|
|
filename = job.cleaned_data['file'].name
|
2018-10-21 17:48:10 +00:00
|
|
|
job = job.save(commit=False)
|
|
|
|
job.filename = filename
|
|
|
|
job.user=request.user
|
|
|
|
if job.printAs is None:
|
|
|
|
job.printAs = request.user
|
|
|
|
job.status='Pending'
|
|
|
|
metadata = pdfinfo(request.FILES['form-%s-file' % i].temporary_file_path())
|
|
|
|
job.pages = metadata["Pages"]
|
|
|
|
job._update_price()
|
|
|
|
job.save()
|
|
|
|
job_data = model_to_dict(job)
|
|
|
|
job_data['jid'] = job.id
|
|
|
|
data.append(job_data)
|
2018-10-21 20:31:21 +00:00
|
|
|
except KeyError:
|
|
|
|
job_formset.errors[i] = {'file': ['This field is required.']}
|
|
|
|
i+=1
|
2018-10-15 00:03:13 +00:00
|
|
|
job_formset_filled_in = formset_factory(PrintForm, extra=0)(
|
|
|
|
initial=data,
|
2018-10-21 20:31:21 +00:00
|
|
|
form_kwargs={'user': request.user},
|
2018-10-15 00:03:13 +00:00
|
|
|
)
|
2018-10-21 17:48:10 +00:00
|
|
|
|
2018-10-21 20:31:21 +00:00
|
|
|
if job_formset.total_error_count() == 0:
|
|
|
|
### Every job in the formset has been treated;
|
|
|
|
### And no empty file. --> Go to next step.
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
'jobform': job_formset_filled_in,
|
|
|
|
'action_name' : 'Print',
|
|
|
|
},
|
|
|
|
'printer/print.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
### No file
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
'jobform': job_formset,
|
|
|
|
'action_name': _("Next"),
|
|
|
|
},
|
|
|
|
'printer/newjob.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
|
|
|
### Formset is not valid --> Return the formset with errors
|
2018-10-21 17:48:10 +00:00
|
|
|
else:
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
'jobform': job_formset,
|
|
|
|
'action_name': _("Next"),
|
|
|
|
},
|
|
|
|
'printer/newjob.html',
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
2018-10-21 20:31:21 +00:00
|
|
|
### Second step
|
|
|
|
elif 'Print' in request.POST:
|
|
|
|
job_formset = formset_factory(PrintForm)(
|
|
|
|
request.POST,
|
|
|
|
form_kwargs={'user': request.user},
|
|
|
|
)
|
|
|
|
if job_formset.is_valid():
|
|
|
|
for job_form in job_formset:
|
2018-10-21 23:15:29 +00:00
|
|
|
data = job_form.cleaned_data
|
|
|
|
jid = data['jid']
|
2018-10-21 20:31:21 +00:00
|
|
|
job = JobWithOptions.objects.get(id=jid)
|
|
|
|
job.user = request.user
|
|
|
|
job.status = 'Printable'
|
2018-10-21 23:15:29 +00:00
|
|
|
if data['printAs']:
|
|
|
|
job.printAs = data['printAs']
|
|
|
|
job.format = data['format']
|
|
|
|
job.color = data['color']
|
|
|
|
job.disposition = data['disposition']
|
|
|
|
job.count = data['count']
|
|
|
|
job.stapling = data['stapling']
|
|
|
|
job.perforation = data['perforation']
|
2018-10-21 20:31:21 +00:00
|
|
|
job._update_price()
|
|
|
|
job.save()
|
|
|
|
return redirect('printer:payment')
|
2018-09-08 22:51:18 +00:00
|
|
|
|
2018-09-08 12:04:44 +00:00
|
|
|
|
2018-10-21 20:31:21 +00:00
|
|
|
### GET request
|
2018-09-08 10:46:52 +00:00
|
|
|
else:
|
|
|
|
job_formset = formset_factory(JobWithOptionsForm)(
|
2018-10-15 00:03:13 +00:00
|
|
|
form_kwargs={'user': request.user}
|
|
|
|
)
|
2018-10-21 20:31:21 +00:00
|
|
|
|
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
|
|
|
|
"""
|
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:
|
2018-10-23 18:10:21 +00:00
|
|
|
users[job.printAs][0]+=job.price
|
|
|
|
users[job.printAs][1].append(job.id)
|
2018-09-08 22:51:18 +00:00
|
|
|
except KeyError:
|
2018-10-23 18:10:21 +00:00
|
|
|
users[job.printAs]=[job.price, [job.id]]
|
2018-09-08 22:51:18 +00:00
|
|
|
|
2018-10-23 18:10:21 +00:00
|
|
|
balancePayment = BalancePayment.objects.get()
|
|
|
|
minimum_balance = balancePayment.minimum_balance
|
2018-09-08 22:51:18 +00:00
|
|
|
for user in users:
|
2018-10-21 20:31:21 +00:00
|
|
|
### If payment_method balance doesn't exist, then you're not allowed to print.
|
2018-09-08 22:51:18 +00:00
|
|
|
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.save()
|
|
|
|
Vente.objects.create(
|
|
|
|
facture=invoice,
|
2018-10-23 18:19:29 +00:00
|
|
|
name='Impressions (par %s)' % request.user,
|
2018-10-23 18:10:21 +00:00
|
|
|
prix=users[user][0],
|
2018-09-08 22:51:18 +00:00
|
|
|
number=1,
|
|
|
|
)
|
|
|
|
invoice.paiement.end_payment(invoice, request)
|
2018-10-23 18:10:21 +00:00
|
|
|
### If we are here, then either we were able to pay and it's ok,
|
|
|
|
### Either we weren't able to pay and we need to cancel the jobs.
|
|
|
|
jobs = JobWithOptions.objects.filter(id__in=users[user][1])
|
|
|
|
if user.solde - users[user][0] < 0:
|
|
|
|
for job in jobs:
|
|
|
|
job.status = 'Cancelled'
|
|
|
|
job.save()
|
|
|
|
else:
|
|
|
|
for job in jobs:
|
|
|
|
job.paid = True
|
|
|
|
job.save()
|
|
|
|
|
2018-09-08 22:51:18 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'printer:success',
|
|
|
|
))
|
|
|
|
|
2018-06-28 18:20:08 +00:00
|
|
|
def success(request):
|
2018-10-21 23:12:49 +00:00
|
|
|
send_mail_printer(request.user)
|
2018-06-28 18:20:08 +00:00
|
|
|
return form(
|
|
|
|
{},
|
|
|
|
'printer/success.html',
|
|
|
|
request
|
|
|
|
)
|