Browse Source

Chamado 273957 - Nome e Sobrenome na edição de Usuário (#3122)

* 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>
pull/3163/head
Vinícius Cantuária 5 years ago
committed by eribeiro
parent
commit
fc91e6aade
  1. 68
      sapl/base/forms.py
  2. 12
      sapl/base/views.py

68
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 = [(g.id, g.name) for g in Group.objects.all().order_by('name')]
ROLES = [] 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( password1 = forms.CharField(
required=False, widget=forms.PasswordInput, label='Senha') required=False,
widget=forms.PasswordInput,
label='Senha')
password2 = forms.CharField( password2 = forms.CharField(
required=False, widget=forms.PasswordInput, label='Confirmar senha') required=False, widget=forms.PasswordInput,
user_active = forms.ChoiceField(choices=YES_NO_CHOICES, required=True, label='Confirmar senha')
label="Usuário ativo?", initial='True') user_active = forms.ChoiceField(
choices=YES_NO_CHOICES,
required=True,
label="Usuário ativo?",
initial='True')
roles = forms.MultipleChoiceField( roles = forms.MultipleChoiceField(
required=True, widget=forms.CheckboxSelectMultiple(), choices=get_roles) required=True,
widget=forms.CheckboxSelectMultiple(),
choices=get_roles)
class Meta: class Meta:
model = get_user_model() model = get_user_model()
fields = [ fields = [
get_user_model().USERNAME_FIELD, 'password1', get_user_model().USERNAME_FIELD,
'password2', 'user_active', 'roles' "first_name",
] + (['email'] "last_name",
if get_user_model().USERNAME_FIELD != 'email' else []) '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) super(UsuarioEditForm, self).__init__(*args, **kwargs)
row1 = to_row([('username', 12)]) rows = to_row((
row2 = to_row([('email', 6), ('username', 12),
('user_active', 6)]) ('first_name', 6),
row3 = to_row( ('last_name', 6),
[('password1', 6), ('email', 6),
('password2', 6)]) ('user_active', 6),
('password1', 6),
row4 = to_row([(form_actions(label='Salvar Alterações'), 6)]) ('password2', 6)))
self.helper = SaplFormHelper() self.helper = SaplFormHelper()
self.helper.layout = Layout( self.helper.layout = Layout(
row1, rows,
row2,
row3,
'roles', 'roles',
form_actions(label='Salvar Alterações')) form_actions(label='Salvar Alterações'))
def clean(self): def clean(self):
super(UsuarioEditForm, self).clean() super().clean()
if not self.is_valid(): if not self.is_valid():
return self.cleaned_data return self.cleaned_data
data = self.cleaned_data data = self.cleaned_data
if data['password1'] and data['password1'] != data['password2']: if data['password1'] and data['password1'] != data['password2']:
self.logger.error('Erro de validação. Senhas informadas ({}, {}) são diferentes.'.format( self.logger.error("Erro de validação. Senhas informadas ({}, {}) são diferentes."
data['password1'], data['password2'])) .format(data['password1'], data['password2']))
raise ValidationError('Senhas informadas são diferentes') raise ValidationError('Senhas informadas são diferentes')
return data return data

12
sapl/base/views.py

@ -1966,10 +1966,13 @@ class EditUsuarioView(PermissionRequiredMixin, UpdateView):
return reverse('sapl.base:usuario') return reverse('sapl.base:usuario')
def get_initial(self): def get_initial(self):
initial = super(EditUsuarioView, self).get_initial() initial = super().get_initial()
user = get_user_model().objects.get(id=self.kwargs['pk']) user = get_user_model().objects.get(id=self.kwargs['pk'])
roles = [str(g.id) for g in user.groups.all()] 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['roles'] = roles
initial['user_active'] = user.is_active initial['user_active'] = user.is_active
@ -1980,8 +1983,11 @@ class EditUsuarioView(PermissionRequiredMixin, UpdateView):
user = form.save(commit=False) user = form.save(commit=False)
data = form.cleaned_data data = form.cleaned_data
# new_user.first_name = data['firstname'] if 'first_name' in data and data['first_name'] != user.first_name:
# new_user.last_name = data['lastname'] 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']: if data['password1']:
user.set_password(data['password1']) user.set_password(data['password1'])

Loading…
Cancel
Save