8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-01 09:02:26 +00:00

[Printer] Handles submission of empty files

This commit is contained in:
Maxime Bombar 2018-10-21 22:31:21 +02:00 committed by root
parent 8656f6dcf3
commit d945dd013d
2 changed files with 59 additions and 104 deletions

View file

@ -21,7 +21,7 @@
{% endfor %} {% endfor %}
</div> </div>
<!-- <input class="btn btn-primary btn-sm" role="button" value="{% trans "Add a file"%}" id="add_one"> --> <!-- <input class="btn btn-primary btn-sm" role="button" value="{% trans "Add a file"%}" id="add_one"> -->
{% bootstrap_button action_name name="options" button_type="submit" icon="star" %} {% bootstrap_button action_name name="Print" button_type="submit" icon="star" %}
</form> </form>
<!-- <script type="text/javascript"> --> <!-- <script type="text/javascript"> -->

View file

@ -40,89 +40,77 @@ from cotisations.utils import find_payment_method
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
# raise ValidationError("'%(path)s'", code='path', params = {'path': job.printAs})
@login_required @login_required
def new_job(request): def new_job(request):
""" """
View to create a new printing job View to create a new printing job
""" """
if request.method == 'POST': if request.method == 'POST':
# raise Exception('coucou3')
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES})
job_formset = formset_factory(JobWithOptionsForm)( ### First Step
request.POST, if 'Next' in request.POST:
request.FILES or None, job_formset = formset_factory(JobWithOptionsForm)(
form_kwargs={'user': request.user}, request.POST,
) request.FILES or None,
form_kwargs={'user': request.user},
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES}) )
tmp_job_formset = job_formset
if request.FILES:
# raise Exception("Coucou4")
if job_formset.is_valid(): if job_formset.is_valid():
# raise Exception("Valide !!!")
files = request.FILES
data = [] data = []
i=0 i=0
# request.POST['jids'] = [] for job in job_formset:
jids = [] ### Fails if one of the forms is submitted without a file.
for job in job_formset.forms: try:
if job.is_valid(): filename = job.cleaned_data['file'].name
try:
filename = job.cleaned_data['file'].name
except KeyError:
raise ValidationError("'%(path)s'", code='path', params = {'path': job})
job = job.save(commit=False) job = job.save(commit=False)
job.filename = filename job.filename = filename
job.user=request.user job.user=request.user
# raise ValidationError("'%(path)s'", code='path', params = {'path': job.printAs})
if job.printAs is None: if job.printAs is None:
job.printAs = request.user job.printAs = request.user
job.status='Pending' 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()) metadata = pdfinfo(request.FILES['form-%s-file' % i].temporary_file_path())
job.pages = metadata["Pages"] 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._update_price()
job.save() job.save()
job_data = model_to_dict(job) 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 job_data['jid'] = job.id
data.append(job_data) data.append(job_data)
i+=1 except KeyError:
job_formset.errors[i] = {'file': ['This field is required.']}
i+=1
job_formset_filled_in = formset_factory(PrintForm, extra=0)( job_formset_filled_in = formset_factory(PrintForm, extra=0)(
initial=data, initial=data,
# jids=jids, form_kwargs={'user': request.user},
form_kwargs={'user': request.user,
},
) )
# raise ValidationError("'%(path)s'", code='path', params = {'path': job_formset_filled_in.forms })
# request.POST['job_ids'] = jids if job_formset.total_error_count() == 0:
return form( ### Every job in the formset has been treated;
{ ### And no empty file. --> Go to next step.
'jobform': job_formset_filled_in, return form(
'action_name' : 'Print', {
}, 'jobform': job_formset_filled_in,
'printer/print.html', 'action_name' : 'Print',
request },
) '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
else: else:
job_formset = tmp_job_formset
return form( return form(
{ {
'jobform': job_formset, 'jobform': job_formset,
@ -132,58 +120,29 @@ def new_job(request):
request request
) )
# else: ### Second step
# job_formset = tmp_job_formset elif 'Print' in request.POST:
# return form( job_formset = formset_factory(PrintForm)(
# { request.POST,
# 'jobform': job_formset, form_kwargs={'user': request.user},
# 'action_name': _("Next"), )
# }, if job_formset.is_valid():
# 'printer/newjob.html', for job_form in job_formset:
# request jid = job_form.cleaned_data['jid']
# ) job = JobWithOptions.objects.get(id=jid)
job.user = request.user
job.status = 'Printable'
# else: job._update_price()
# raise Exception("Coucou5") job.save()
# elif 'Print' in request.POST: return redirect('printer:payment')
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.POST })
# raise Exception('On a déjà upload !')
# n = int(request.POST['form-TOTAL_FORMS'])
job_formset = formset_factory(PrintForm)(
request.POST,
form_kwargs={'user': request.user},
)
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.POST })
# jids = request.POST['job_ids']
if job_formset.is_valid():
for job_form in job_formset:
i=0
# 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)
job.user = request.user
job.status = 'Printable'
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job.file})
# job.file = old_job.file
job._update_price()
job.save()
i+=1
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': request.method})
# raise ValidationError("'%(path)s'", code='path', params = {'path': str(n) })
# request.session['jids']=jids
return redirect('printer:payment')
### GET request
else: else:
# raise Exception("Coucou2")
job_formset = formset_factory(JobWithOptionsForm)( job_formset = formset_factory(JobWithOptionsForm)(
form_kwargs={'user': request.user} form_kwargs={'user': request.user}
) )
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job_formset})
return form( return form(
{ {
@ -198,9 +157,6 @@ def payment(request):
""" """
View used to create a new invoice and make the payment View used to create a new invoice and make the payment
""" """
# user = request.user
# jids = request.session['jids']
# raise ValidationError("'%(path)s'", code='path', params = {'path': jids})
jobs = JobWithOptions.objects.filter(user=request.user, status='Printable', paid='False') jobs = JobWithOptions.objects.filter(user=request.user, status='Printable', paid='False')
users = {} users = {}
for job in jobs: for job in jobs:
@ -214,7 +170,7 @@ def payment(request):
job.save() job.save()
for user in users: for user in users:
# If payment_method balance doesn't exist, then you're not allowed to print. ### If payment_method balance doesn't exist, then you're not allowed to print.
try: try:
balance = find_payment_method(Paiement.objects.get(is_balance=True)) balance = find_payment_method(Paiement.objects.get(is_balance=True))
except Paiement.DoesNotExist: except Paiement.DoesNotExist:
@ -228,7 +184,6 @@ def payment(request):
)) ))
invoice = Facture(user=user) invoice = Facture(user=user)
invoice.paiement = balance.payment invoice.paiement = balance.payment
# invoice.valid = True
invoice.save() invoice.save()
Vente.objects.create( Vente.objects.create(
facture=invoice, facture=invoice,