11# Permafrost Forms
2+ from django .conf import settings
23from django .contrib .auth .models import Permission
4+ from django .core .exceptions import ValidationError
35from django .forms import ModelForm , MultipleChoiceField , CheckboxSelectMultiple
46from django .forms .fields import CharField , ChoiceField , BooleanField
57from django .forms .widgets import CheckboxInput , Textarea
810
911CHOICES = [('' , _ ("Choose Role Type" ))] + get_choices ()
1012
13+ LABELS = {
14+ 'name' : _ ('Role Name' ),
15+ 'category' : _ ('Role Type' )
16+ }
17+
1118def assemble_optiongroups_for_widget (permissions ):
1219 choices = []
1320 optgroups = {}
@@ -40,6 +47,7 @@ class SelectPermafrostRoleTypeForm(ModelForm):
4047 class Meta :
4148 model = PermafrostRole
4249 fields = ('name' , 'description' , 'category' ,)
50+ labels = LABELS
4351
4452 def __init__ (self , * args , ** kwargs ):
4553 super ().__init__ (* args , ** kwargs )
@@ -55,24 +63,27 @@ class Meta:
5563 widgets = {
5664 'description' : Textarea (),
5765 }
66+ labels = LABELS
5867
5968 def __init__ (self , * args , ** kwargs ):
6069 super ().__init__ (* args , ** kwargs )
6170
6271 self .fields ['category' ].choices = CHOICES
63- category = self .initial .get ('category' , None )
72+ category = self .initial .get (
73+ 'category' ,
74+ self .data .get ('category' , None )
75+ )
6476
6577 bootstrappify (self .fields )
6678
6779 if category :
68-
80+
6981 required_perms = get_required_by_category (category )
7082 optional_perms = get_optional_by_category (category )
7183 required_choices = assemble_optiongroups_for_widget (required_perms )
7284 optional_choices = assemble_optiongroups_for_widget (optional_perms )
7385
7486 initial = [perm .pk for perm in required_perms ]
75-
7687 self .fields [f'optional_{ category } _perms' ] = MultipleChoiceField (label = _ ("Optional Permissions" ), choices = optional_choices , widget = CheckboxSelectMultiple (), required = False )
7788 self .fields [f'required_{ category } _perms' ] = MultipleChoiceField (label = _ ("Required Permissions" ), initial = initial , choices = required_choices , widget = CheckboxSelectMultiple (attrs = {'readonly' :True , 'disabled' : True }), required = False )
7889
@@ -89,13 +100,42 @@ def save(self, commit=True):
89100 instance .permissions_clear ()
90101 return instance
91102
103+ def clean_name (self ):
104+ name = self .cleaned_data ['name' ]
105+ name_exists = False
106+
107+ if self .instance : ## on update check if name change exists
108+
109+ if 'name' in self .changed_data :
110+ name_exists = PermafrostRole .objects .filter (
111+
112+ name = name ,
113+ site__id = settings .SITE_ID ,
114+
115+ ).exclude (pk = self .instance .pk ).first ()
116+
117+ else :
118+
119+ try :
120+ name_exists = PermafrostRole .objects .get (
121+ name = name ,
122+ site__id = settings .SITE_ID
123+ )
124+ except PermafrostRole .DoesNotExist :
125+ pass
126+
127+ if name_exists :
128+ raise ValidationError ('Role with this name already exists' )
129+
130+ # Always return field
131+ return name
132+
92133class PermafrostRoleUpdateForm (PermafrostRoleCreateForm ):
93134 """
94135 Form used to display role detail
95136 Only allowed to edit optional permissions, name and description
96137 Category and required permissions stay locked
97138 """
98- category = ChoiceField (choices = CHOICES , required = False )
99139 deleted = BooleanField (required = False )
100140
101141 def __init__ (self , * args , ** kwargs ):
0 commit comments