22from django .conf import settings
33from django .contrib .auth .models import Permission
44from django .core .exceptions import ValidationError
5- from django .forms import ModelForm , MultipleChoiceField , CheckboxSelectMultiple
5+ from django .forms import ModelForm
66from django .forms .fields import CharField , ChoiceField , BooleanField
7- from django .forms .widgets import CheckboxInput , Textarea
7+ from django .forms .models import ModelMultipleChoiceField
8+ from django .forms .widgets import CheckboxInput
89from django .utils .translation import ugettext_lazy as _
9- from .models import PermafrostRole , get_optional_by_category , get_required_by_category , get_choices
10+ from .models import PermafrostRole , get_optional_by_category , get_choices
1011
1112CHOICES = [('' , _ ("Choose Role Type" ))] + get_choices ()
1213
@@ -41,7 +42,7 @@ def bootstrappify(fields):
4142
4243class SelectPermafrostRoleTypeForm (ModelForm ):
4344 name = CharField (required = False )
44- description = CharField (required = False , widget = Textarea () )
45+ description = CharField (required = False )
4546 category = ChoiceField (choices = CHOICES )
4647
4748 class Meta :
@@ -57,43 +58,41 @@ def __init__(self, *args, **kwargs):
5758
5859
5960class PermafrostRoleCreateForm (ModelForm ):
61+ permissions = ModelMultipleChoiceField (queryset = Permission .objects .all (), required = False )
6062 class Meta :
6163 model = PermafrostRole
62- fields = ('name' , 'description' , 'category' ,)
63- widgets = {
64- 'description' : Textarea (),
65- }
64+ fields = ('name' , 'description' , 'category' , 'permissions' )
6665 labels = LABELS
6766
6867 def __init__ (self , * args , ** kwargs ):
6968 super ().__init__ (* args , ** kwargs )
7069
7170 self .fields ['category' ].choices = CHOICES
71+
7272 category = self .initial .get (
7373 'category' ,
7474 self .data .get ('category' , None )
7575 )
7676
77- bootstrappify (self .fields )
77+ if self .instance :
78+ category = self .instance .category if self .instance .category else category
7879
79- if category :
80-
81- required_perms = get_required_by_category (category )
82- optional_perms = get_optional_by_category (category )
83- required_choices = assemble_optiongroups_for_widget (required_perms )
84- optional_choices = assemble_optiongroups_for_widget (optional_perms )
85-
86- initial = [perm .pk for perm in required_perms ]
87- self .fields [f'optional_{ category } _perms' ] = MultipleChoiceField (label = _ ("Optional Permissions" ), choices = optional_choices , widget = CheckboxSelectMultiple (), required = False )
88- self .fields [f'required_{ category } _perms' ] = MultipleChoiceField (label = _ ("Required Permissions" ), initial = initial , choices = required_choices , widget = CheckboxSelectMultiple (attrs = {'readonly' :True , 'disabled' : True }), required = False )
80+ if category :
81+ all_optional_permissions = get_optional_by_category (category = category )
82+ ids = [perm .pk for perm in all_optional_permissions ]
83+
84+ self .fields ['permissions' ].queryset = Permission .objects .filter (id__in = ids )
8985
86+ bootstrappify (self .fields )
87+
9088 def save (self , commit = True ):
91- instance = super ().save (commit )
89+ instance = super ().save (commit )
9290 category = instance .category
93- if self .cleaned_data and f'optional_{ category } _perms' in self .cleaned_data :
91+
92+ if 'permissions' in self .cleaned_data :
9493 perm_ids = []
9594 if category :
96- perm_ids = self .cleaned_data [f'optional_ { category } _perms' ]
95+ perm_ids = self .cleaned_data ['permissions' ]
9796 if perm_ids :
9897 instance .permissions_set (Permission .objects .filter (id__in = perm_ids ))
9998 else :
@@ -144,31 +143,14 @@ def __init__(self, *args, **kwargs):
144143 self .fields ['category' ].widget .attrs .update ({'readonly' : True , 'disabled' : True })
145144 self .fields ['category' ].disabled = True
146145 self .fields ['category' ].required = False
146+ self .fields ['category' ].choices = [choice for choice in CHOICES if choice [0 ] == self .instance .category ]
147147 self .fields ['category' ].initial = self .instance .category
148-
148+ ## limit choices to saved category
149149 self .fields ['deleted' ].initial = self .instance .deleted
150-
151- category = self .instance .category
152-
153- optional_perms = get_optional_by_category (category )
154- optional_choices = assemble_optiongroups_for_widget (optional_perms )
155-
156- available_optional_ids = [permission .id for permission in optional_perms ]
157- preselected_optional = [permission .id for permission in self .instance .permissions ().all () if permission .id in available_optional_ids ]
158-
159- self .fields .update ({
160- f'optional_{ category } _perms' : MultipleChoiceField (
161- label = _ ("Optional Permissions" ),
162- initial = preselected_optional ,
163- choices = optional_choices ,
164- widget = CheckboxSelectMultiple (),
165- required = False
166- )
167- })
168150
169151 def save (self , commit = True ):
170152 if self .cleaned_data ['deleted' ]:
171153 self .instance .deleted = self .cleaned_data ['deleted' ]
172154 instance = super ().save (commit )
173155 return instance
174-
156+
0 commit comments