From c47785a34981c6835f37774c500c2c3c2181c5a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Cantu=C3=A1ria?= Date: Wed, 11 Mar 2020 16:01:48 -0300 Subject: [PATCH] =?UTF-8?q?Chamado=20273957=20-=20Nome=20e=20Sobrenome=20n?= =?UTF-8?q?a=20edi=C3=A7=C3=A3o=20de=20Usu=C3=A1rio=20(#3122)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adiciona possibilidade de editar nome e sobrenome de usuário * Update sapl/base/views.py * Update sapl/base/views.py Co-authored-by: Edward <9326037+edwardoliveira@users.noreply.github.com> --- sapl/base/forms.py | 68 +++++++++++++++++++++++++++++----------------- sapl/base/views.py | 12 ++++++-- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/sapl/base/forms.py b/sapl/base/forms.py index 96420e77d..b68fcb59b 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -177,54 +177,72 @@ class UsuarioEditForm(ModelForm): # ROLES = [(g.id, g.name) for g in Group.objects.all().order_by('name')] ROLES = [] + first_name = forms.CharField( + required=False, + label="Nome", + max_length=30) + last_name = forms.CharField( + required=False, + label="Sobrenome", + max_length=30) password1 = forms.CharField( - required=False, widget=forms.PasswordInput, label='Senha') + required=False, + widget=forms.PasswordInput, + label='Senha') password2 = forms.CharField( - required=False, widget=forms.PasswordInput, label='Confirmar senha') - user_active = forms.ChoiceField(choices=YES_NO_CHOICES, required=True, - label="Usuário ativo?", initial='True') + required=False, widget=forms.PasswordInput, + label='Confirmar senha') + user_active = forms.ChoiceField( + choices=YES_NO_CHOICES, + required=True, + label="Usuário ativo?", + initial='True') roles = forms.MultipleChoiceField( - required=True, widget=forms.CheckboxSelectMultiple(), choices=get_roles) + required=True, + widget=forms.CheckboxSelectMultiple(), + choices=get_roles) class Meta: model = get_user_model() fields = [ - get_user_model().USERNAME_FIELD, 'password1', - 'password2', 'user_active', 'roles' - ] + (['email'] - if get_user_model().USERNAME_FIELD != 'email' else []) + get_user_model().USERNAME_FIELD, + "first_name", + "last_name", + 'password1', + 'password2', + 'user_active', + 'roles'] - def __init__(self, *args, **kwargs): + if get_user_model().USERNAME_FIELD != 'email': + fields.extend(['email']) + def __init__(self, *args, **kwargs): super(UsuarioEditForm, self).__init__(*args, **kwargs) - row1 = to_row([('username', 12)]) - row2 = to_row([('email', 6), - ('user_active', 6)]) - row3 = to_row( - [('password1', 6), - ('password2', 6)]) - - row4 = to_row([(form_actions(label='Salvar Alterações'), 6)]) + rows = to_row(( + ('username', 12), + ('first_name', 6), + ('last_name', 6), + ('email', 6), + ('user_active', 6), + ('password1', 6), + ('password2', 6))) self.helper = SaplFormHelper() self.helper.layout = Layout( - row1, - row2, - row3, + rows, 'roles', form_actions(label='Salvar Alterações')) def clean(self): - super(UsuarioEditForm, self).clean() - + super().clean() if not self.is_valid(): return self.cleaned_data data = self.cleaned_data if data['password1'] and data['password1'] != data['password2']: - self.logger.error('Erro de validação. Senhas informadas ({}, {}) são diferentes.'.format( - data['password1'], data['password2'])) + self.logger.error("Erro de validação. Senhas informadas ({}, {}) são diferentes." + .format(data['password1'], data['password2'])) raise ValidationError('Senhas informadas são diferentes') return data diff --git a/sapl/base/views.py b/sapl/base/views.py index 305518ae0..a02011b12 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -1881,10 +1881,13 @@ class EditUsuarioView(PermissionRequiredMixin, UpdateView): return reverse('sapl.base:usuario') def get_initial(self): - initial = super(EditUsuarioView, self).get_initial() + initial = super().get_initial() user = get_user_model().objects.get(id=self.kwargs['pk']) roles = [str(g.id) for g in user.groups.all()] + + initial['first_name'] = user.first_name + initial['last_name'] = user.last_name initial['roles'] = roles initial['user_active'] = user.is_active @@ -1895,8 +1898,11 @@ class EditUsuarioView(PermissionRequiredMixin, UpdateView): user = form.save(commit=False) data = form.cleaned_data - # new_user.first_name = data['firstname'] - # new_user.last_name = data['lastname'] + if 'first_name' in data and data['first_name'] != user.first_name: + user.first_name = data['first_name'] + + if 'last_name' in data and data['last_name'] != user.last_name: + user.last_name = data['last_name'] if data['password1']: user.set_password(data['password1'])