From 3e4b39bedb04701d0a3cb29c5a3074101399fe69 Mon Sep 17 00:00:00 2001 From: Maxime Bombar Date: Mon, 15 Oct 2018 02:03:13 +0200 Subject: [PATCH] [Printer] Limit PrintAs to clubs request.user belongs to --- printer/forms.py | 7 ++++++- printer/views.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/printer/forms.py b/printer/forms.py index 1b721c95..80595466 100644 --- a/printer/forms.py +++ b/printer/forms.py @@ -3,7 +3,6 @@ """printer.forms Form to add, edit, cancel printer jobs. Author : Maxime Bombar . -Date : 29/06/2018 """ from django import forms @@ -16,6 +15,8 @@ import itertools from re2o.mixins import FormRevMixin +from users.models import User + from .models import ( JobWithOptions, ) @@ -24,9 +25,11 @@ from .models import ( class JobWithOptionsForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) + self.user = kwargs.pop('user') super(JobWithOptionsForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['printAs'].label = 'Print As' self.fields['printAs'].empty_label = 'Print As' + self.fields['printAs'].queryset = self.user.adherent.club_members.all() self.fields['disposition'].label = 'disposition' self.fields['color'].label = 'color' self.fields['count'].label = 'count' @@ -45,8 +48,10 @@ class JobWithOptionsForm(FormRevMixin, ModelForm): class PrintForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) + self.user = kwargs.pop('user') super(PrintForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['printAs'].label = 'Print As' + self.fields['printAs'].queryset = self.user.adherent.club_members.all() self.fields['disposition'].label = 'disposition' self.fields['color'].label = 'color' self.fields['count'].label = 'count' diff --git a/printer/views.py b/printer/views.py index d013b43f..079372b0 100644 --- a/printer/views.py +++ b/printer/views.py @@ -49,6 +49,7 @@ def new_job(request): job_formset = formset_factory(JobWithOptionsForm)( request.POST, request.FILES, + form_kwargs={'user': request.user}, ) if job_formset.is_valid(): @@ -85,7 +86,10 @@ def new_job(request): # raise ValidationError("'%(plop)s'", code='plop', params = {'plop': job_data }) data.append(job_data) i+=1 - job_formset_filled_in = formset_factory(PrintForm, extra=0)(initial=data) + job_formset_filled_in = formset_factory(PrintForm, extra=0)( + initial=data, + form_kwargs={'user': request.user}, + ) return form( { 'jobform': job_formset_filled_in, @@ -102,6 +106,7 @@ def new_job(request): n = int(request.POST['form-TOTAL_FORMS']) job_formset = formset_factory(PrintForm)( request.POST, + form_kwargs={'user': request.user}, ) jids = [request.session['id-form-%s-file' % i] for i in range(n)] # raise ValidationError("'%(path)s'", code='path', params = {'path': id_list }) @@ -125,8 +130,8 @@ def new_job(request): else: job_formset = formset_factory(JobWithOptionsForm)( - None, - ) + form_kwargs={'user': request.user} + ) return form( { 'jobform': job_formset,