mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-16 08:23:12 +00:00
[Printer] Handles submission of empty files
This commit is contained in:
parent
8656f6dcf3
commit
d945dd013d
2 changed files with 59 additions and 104 deletions
|
@ -21,7 +21,7 @@
|
|||
{% endfor %}
|
||||
</div>
|
||||
<!-- <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>
|
||||
<!-- <script type="text/javascript"> -->
|
||||
|
||||
|
|
101
printer/views.py
101
printer/views.py
|
@ -40,79 +40,56 @@ from cotisations.utils import find_payment_method
|
|||
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
# raise ValidationError("'%(path)s'", code='path', params = {'path': job.printAs})
|
||||
|
||||
@login_required
|
||||
def new_job(request):
|
||||
"""
|
||||
View to create a new printing job
|
||||
"""
|
||||
if request.method == 'POST':
|
||||
# raise Exception('coucou3')
|
||||
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES})
|
||||
|
||||
### First Step
|
||||
if 'Next' in request.POST:
|
||||
job_formset = formset_factory(JobWithOptionsForm)(
|
||||
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():
|
||||
# raise Exception("Valide !!!")
|
||||
files = request.FILES
|
||||
data = []
|
||||
i=0
|
||||
# request.POST['jids'] = []
|
||||
jids = []
|
||||
for job in job_formset.forms:
|
||||
if job.is_valid():
|
||||
for job in job_formset:
|
||||
### Fails if one of the forms is submitted without a file.
|
||||
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)
|
||||
except KeyError:
|
||||
job_formset.errors[i] = {'file': ['This field is required.']}
|
||||
i+=1
|
||||
job_formset_filled_in = formset_factory(PrintForm, extra=0)(
|
||||
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:
|
||||
### Every job in the formset has been treated;
|
||||
### And no empty file. --> Go to next step.
|
||||
return form(
|
||||
{
|
||||
'jobform': job_formset_filled_in,
|
||||
|
@ -122,7 +99,7 @@ def new_job(request):
|
|||
request
|
||||
)
|
||||
else:
|
||||
job_formset = tmp_job_formset
|
||||
### No file
|
||||
return form(
|
||||
{
|
||||
'jobform': job_formset,
|
||||
|
@ -132,58 +109,40 @@ def new_job(request):
|
|||
request
|
||||
)
|
||||
|
||||
# else:
|
||||
# job_formset = tmp_job_formset
|
||||
# return form(
|
||||
# {
|
||||
# 'jobform': job_formset,
|
||||
# 'action_name': _("Next"),
|
||||
# },
|
||||
# 'printer/newjob.html',
|
||||
# request
|
||||
# )
|
||||
### Formset is not valid --> Return the formset with errors
|
||||
else:
|
||||
return form(
|
||||
{
|
||||
'jobform': job_formset,
|
||||
'action_name': _("Next"),
|
||||
},
|
||||
'printer/newjob.html',
|
||||
request
|
||||
)
|
||||
|
||||
|
||||
# else:
|
||||
# raise Exception("Coucou5")
|
||||
# elif 'Print' in request.POST:
|
||||
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.POST })
|
||||
|
||||
# raise Exception('On a déjà upload !')
|
||||
# n = int(request.POST['form-TOTAL_FORMS'])
|
||||
### Second step
|
||||
elif 'Print' in request.POST:
|
||||
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:
|
||||
# raise Exception("Coucou2")
|
||||
job_formset = formset_factory(JobWithOptionsForm)(
|
||||
form_kwargs={'user': request.user}
|
||||
)
|
||||
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job_formset})
|
||||
|
||||
|
||||
return form(
|
||||
{
|
||||
|
@ -198,9 +157,6 @@ def payment(request):
|
|||
"""
|
||||
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')
|
||||
users = {}
|
||||
for job in jobs:
|
||||
|
@ -214,7 +170,7 @@ def payment(request):
|
|||
job.save()
|
||||
|
||||
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:
|
||||
balance = find_payment_method(Paiement.objects.get(is_balance=True))
|
||||
except Paiement.DoesNotExist:
|
||||
|
@ -228,7 +184,6 @@ def payment(request):
|
|||
))
|
||||
invoice = Facture(user=user)
|
||||
invoice.paiement = balance.payment
|
||||
# invoice.valid = True
|
||||
invoice.save()
|
||||
Vente.objects.create(
|
||||
facture=invoice,
|
||||
|
|
Loading…
Reference in a new issue