8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-06 01:46:27 +00:00
re2o/printer/views.py

182 lines
5.2 KiB
Python
Raw Normal View History

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
from django.forms.models import model_to_dict
2018-06-29 04:06:52 +00:00
from django.contrib.auth.decorators import login_required
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
from .utils import pdfinfo, send_mail_printer
from .models import (
JobWithOptions,
PrintOperation
)
2018-06-28 18:20:08 +00:00
from .forms import (
2018-06-29 03:05:15 +00:00
JobWithOptionsForm,
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
from cotisations.payment_methods.balance.models import BalancePayment
from django.core.exceptions import ValidationError
from re2o.acl import (
can_edit
)
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
"""
print_operation = PrintOperation(user=request.user)
job_formset = formset_factory(JobWithOptionsForm)(
request.POST or None,
request.FILES or None,
form_kwargs={'user': request.user},
)
if job_formset.is_valid():
job_form = job_formset
print_operation.save()
for count, job in enumerate(job_form):
### Fails if one of the forms is submitted without a file.
try:
filename = job.cleaned_data['file'].name
job_instance = job.save(commit=False)
job_instance.filename = filename
job_instance.print_operation = print_operation
job_instance.user=request.user
job_instance.printAs = job.cleaned_data['printAs'] or request.user
metadata = pdfinfo(request.FILES['form-%s-file' % count].temporary_file_path())
job_instance.pages = metadata["Pages"]
job_instance.save()
except KeyError:
job_form.errors[count] = {'file': ['This field is required.']}
if job_formset.total_error_count() == 0:
return redirect(reverse(
'printer:print-job',
kwargs={'printoperationid': print_operation.id}
))
2018-06-28 18:20:08 +00:00
return form(
{
'jobform': job_formset,
'action_name': _('Next'),
2018-06-28 18:20:08 +00:00
},
'printer/newjob.html',
request
)
@login_required
@can_edit(PrintOperation)
def print_job(request, printoperation, **_kwargs):
jobs_to_edit = JobWithOptions.objects.filter(print_operation=printoperation)
job_modelformset = modelformset_factory(
JobWithOptions,
extra=0,
fields=('color', 'disposition', 'count', 'stapling', 'perforation'),
max_num=jobs_to_edit.count()
)
job_formset = job_modelformset(
request.POST or None,
queryset=jobs_to_edit
)
if job_formset.is_valid():
job_formset.save()
for job_form in job_formset:
job = job_form.instance
job.status = 'Printable'
job.save()
return payment(request, jobs_to_edit)
return form(
{
'jobform': job_formset,
'action_name': _('Print'),
},
'printer/print.html',
request
)
def payment(request, jobs):
2018-09-08 22:51:18 +00:00
"""
View used to create a new invoice and make the payment
"""
success = 0
2018-09-08 22:51:18 +00:00
users = {}
for job in jobs:
try:
users[job.printAs][0]+=job.price
users[job.printAs][1].append(job.id)
2018-09-08 22:51:18 +00:00
except KeyError:
users[job.printAs]=[job.price, [job.id]]
2018-09-08 22:51:18 +00:00
balancePayment = BalancePayment.objects.get()
minimum_balance = balancePayment.minimum_balance
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.
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,
name='Impressions',
prix=users[user][0],
2018-09-08 22:51:18 +00:00
number=1,
)
invoice.paiement.end_payment(invoice, request)
### 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()
success=1
if success:
send_mail_printer(request.user)
2018-09-08 22:51:18 +00:00
return redirect(reverse(
'users:profil',
kwargs={'userid': str(request.user.id)}
2018-09-08 22:51:18 +00:00
))