diff --git a/sapl/base/forms.py b/sapl/base/forms.py index 9abd4ee47..15c4edc10 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -79,11 +79,6 @@ class UserAdminForm(ModelForm): widget=forms.PasswordInput(), help_text='Deixe os campos em branco para não fazer alteração de senha') - autor = forms.ModelChoiceField( - label=_('Operador de Autor'), - queryset=Autor.objects.all(), - required=False) - token = forms.CharField( required=False, label="Token", @@ -103,7 +98,8 @@ class UserAdminForm(ModelForm): 'new_password1', 'new_password2', 'groups', - 'user_permissions' + + 'user_permissions', ] if get_user_model().USERNAME_FIELD != 'email': @@ -113,6 +109,7 @@ class UserAdminForm(ModelForm): self.user_session = kwargs.pop('user_session', None) self.granular = kwargs.pop('granular', None) + self.instance = kwargs.get('instance', None) row_pwd = to_row( [ @@ -121,18 +118,19 @@ class UserAdminForm(ModelForm): ('is_active', 2), ('first_name', 6), ('last_name', 6), - ('new_password1', 3), - ('new_password2', 3), + ('new_password1', 3 if self.instance and self.instance.pk else 6), + ('new_password2', 3 if self.instance and self.instance.pk else 6), ( FieldWithButtons( 'token', StrictButton( 'Renovar', id="renovar-token", - css_class="btn-outline-primary") - ), + css_class="btn-outline-primary"), + css_class='' if self.instance and self.instance.pk else 'd-none'), 6 ), + ('groups', 12), ] + ([('user_permissions', 12)] if not self.granular is None else []) @@ -142,17 +140,21 @@ class UserAdminForm(ModelForm): self.helper.layout = SaplFormLayout(row_pwd) super(UserAdminForm, self).__init__(*args, **kwargs) - if self.instance.pk: + self.fields['groups'].widget = forms.CheckboxSelectMultiple() + + if not self.instance.pk: + self.fields['groups'].choices = [ + (g.id, g) for g in Group.objects.exclude(name__in=['Autor', 'Votante']).order_by('name') + ] + else: self.fields['token'].initial = self.instance.auth_token.key - self.fields[ - 'groups'].widget = forms.CheckboxSelectMultiple() self.fields['groups'].choices = [ - (g.id, g) for g in self.instance.groups.exclude(name='Votante').order_by('name') + (g.id, g) for g in self.instance.groups.exclude(name__in=['Autor', 'Votante']).order_by('name') ] + [ (g.id, g) for g in Group.objects.exclude( - user=self.instance).exclude(name='Votante').order_by('name') + user=self.instance).exclude(name__in=['Autor', 'Votante']).order_by('name') ] self.fields[ @@ -186,12 +188,16 @@ class UserAdminForm(ModelForm): if self.instance.id: inst_old = get_user_model().objects.get(pk=self.instance.pk) votante = inst_old.groups.filter(name='Votante').first() + autor = inst_old.groups.filter(name='Autor').first() inst_new = super().save(commit) if votante: inst_new.groups.add(votante) + if autor: + inst_new.groups.add(autor) + return inst_new def clean(self): @@ -211,6 +217,19 @@ class UserAdminForm(ModelForm): if new_password1 and new_password2: password_validation.validate_password( new_password2, self.instance) + + if 'email' in data and data['email']: + duplicidade = get_user_model().objects.filter(email=data['email']) + if self.instance.id: + duplicidade = duplicidade.exclude(id=self.instance.id) + + if duplicidade.exists(): + raise forms.ValidationError( + "Email já cadastrado para: {}".format( + ', '.join(map(lambda x: str(x), duplicidade.all())), + ) + ) + return data diff --git a/sapl/base/views.py b/sapl/base/views.py index a85d8370f..21a8a74a5 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -1959,6 +1959,7 @@ class UserCrud(Crud): class CreateView(Crud.CreateView): form_class = UserAdminForm + layout_key = None class UpdateView(Crud.UpdateView): form_class = UserAdminForm @@ -2017,6 +2018,7 @@ class UserCrud(Crud): def get_context_data(self, **kwargs): context = Crud.ListView.get_context_data(self, **kwargs) context['subnav_template_name'] = None + context['title'] = _('Usuários') return context def hook_header_usuario(self, *args, **kwargs): @@ -2025,8 +2027,8 @@ class UserCrud(Crud): def hook_header_groups(self, *args, **kwargs): return 'Grupos' - def hook_header_active(self, *args, **kwargs): - return 'is_active' + def hook_header_is_active(self, *args, **kwargs): + return 'Ativo' def hook_usuario(self, *args, **kwargs): return '{} ({})
{}'.format( @@ -2047,9 +2049,6 @@ class UserCrud(Crud): return qs - def dispatch(self, request, *args, **kwargs): - return Crud.ListView.dispatch(self, request, *args, **kwargs) - class CasaLegislativaCrud(CrudAux): model = CasaLegislativa