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 = []
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

12
sapl/base/views.py

@ -1966,10 +1966,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
@ -1980,8 +1983,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'])

Loading…
Cancel
Save