From 7c3788f0199dd1bd7b50d195b3ff5ab6d7642cbc Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Fri, 26 Oct 2018 02:53:57 +0200 Subject: [PATCH] ["Printer] fix print_as keyerror --- printer/forms.py | 13 ++++++------- printer/models.py | 13 +++++++++++-- printer/views.py | 3 +-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/printer/forms.py b/printer/forms.py index bb970e88..8dcfa013 100644 --- a/printer/forms.py +++ b/printer/forms.py @@ -14,6 +14,7 @@ from django.utils.translation import ugettext_lazy as _ import itertools +from re2o.field_permissions import FieldPermissionFormMixin from re2o.mixins import FormRevMixin from users.models import User @@ -23,17 +24,15 @@ from .models import ( ) -class JobWithOptionsForm(FormRevMixin, ModelForm): +class JobWithOptionsForm(FieldPermissionFormMixin, FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) - self.user = kwargs.pop('user') + user=kwargs.get('user') super(JobWithOptionsForm, self).__init__(*args, prefix=prefix, **kwargs) - if not self.user.adherent.club_members.all(): - self.fields.pop('printAs') - else: + if 'printAs' in self.fields: self.fields['printAs'].label = _('Print As') - self.fields['printAs'].empty_label = self.user.pseudo - self.fields['printAs'].queryset = self.user.adherent.club_members.all() + self.fields['printAs'].empty_label = user.pseudo + self.fields['printAs'].queryset = user.adherent.club_members.all() class Meta: model = JobWithOptions diff --git a/printer/models.py b/printer/models.py index 2266d78d..1e36ac9b 100644 --- a/printer/models.py +++ b/printer/models.py @@ -19,7 +19,7 @@ from django.utils.translation import ugettext_lazy as _ from django.template.defaultfilters import filesizeformat from re2o.mixins import RevMixin, AclMixin - +from re2o.field_permissions import FieldPermissionModelMixin import users.models from .validators import ( @@ -87,7 +87,7 @@ class PrintOperation(RevMixin, AclMixin, models.Model): return False, _("This is not your print operation task") -class JobWithOptions(RevMixin, AclMixin, models.Model): +class JobWithOptions(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): """ This is the main model of printer application : @@ -234,6 +234,15 @@ class JobWithOptions(RevMixin, AclMixin, models.Model): return total_price/100 + def __init__(self, *args, **kwargs): + super(JobWithOptions, self).__init__(*args, **kwargs) + self.field_permissions = { + 'printAs': self.can_change_printas, + } + + def can_change_printas(self, user_request, *_args, **_kwargs): + return user_request.adherent.club_members.all(), None + def save(self, *args, **kwargs): self._update_price() super(JobWithOptions, self).save(*args, **kwargs) diff --git a/printer/views.py b/printer/views.py index 8e1f6711..8efce2f8 100644 --- a/printer/views.py +++ b/printer/views.py @@ -69,8 +69,7 @@ def new_job(request): 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 + job_instance.printAs = job.cleaned_data.get('printAs', request.user) metadata = pdfinfo(request.FILES['form-%s-file' % count].temporary_file_path()) job_instance.pages = metadata["Pages"] job_instance.save()