Browse Source

HOT-FIX: ajustes na refatoração de manut de usuários

pull/3372/head
Leandro Roberto 4 years ago
parent
commit
358d1e3de8
  1. 49
      sapl/base/forms.py
  2. 9
      sapl/base/views.py

49
sapl/base/forms.py

@ -79,11 +79,6 @@ class UserAdminForm(ModelForm):
widget=forms.PasswordInput(), widget=forms.PasswordInput(),
help_text='Deixe os campos em branco para não fazer alteração de senha') 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( token = forms.CharField(
required=False, required=False,
label="Token", label="Token",
@ -103,7 +98,8 @@ class UserAdminForm(ModelForm):
'new_password1', 'new_password1',
'new_password2', 'new_password2',
'groups', 'groups',
'user_permissions'
'user_permissions',
] ]
if get_user_model().USERNAME_FIELD != 'email': if get_user_model().USERNAME_FIELD != 'email':
@ -113,6 +109,7 @@ class UserAdminForm(ModelForm):
self.user_session = kwargs.pop('user_session', None) self.user_session = kwargs.pop('user_session', None)
self.granular = kwargs.pop('granular', None) self.granular = kwargs.pop('granular', None)
self.instance = kwargs.get('instance', None)
row_pwd = to_row( row_pwd = to_row(
[ [
@ -121,18 +118,19 @@ class UserAdminForm(ModelForm):
('is_active', 2), ('is_active', 2),
('first_name', 6), ('first_name', 6),
('last_name', 6), ('last_name', 6),
('new_password1', 3), ('new_password1', 3 if self.instance and self.instance.pk else 6),
('new_password2', 3), ('new_password2', 3 if self.instance and self.instance.pk else 6),
( (
FieldWithButtons( FieldWithButtons(
'token', 'token',
StrictButton( StrictButton(
'Renovar', 'Renovar',
id="renovar-token", 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 6
), ),
('groups', 12), ('groups', 12),
] + ([('user_permissions', 12)] if not self.granular is None else []) ] + ([('user_permissions', 12)] if not self.granular is None else [])
@ -142,17 +140,21 @@ class UserAdminForm(ModelForm):
self.helper.layout = SaplFormLayout(row_pwd) self.helper.layout = SaplFormLayout(row_pwd)
super(UserAdminForm, self).__init__(*args, **kwargs) 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['token'].initial = self.instance.auth_token.key
self.fields[
'groups'].widget = forms.CheckboxSelectMultiple()
self.fields['groups'].choices = [ 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( (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[ self.fields[
@ -186,12 +188,16 @@ class UserAdminForm(ModelForm):
if self.instance.id: if self.instance.id:
inst_old = get_user_model().objects.get(pk=self.instance.pk) inst_old = get_user_model().objects.get(pk=self.instance.pk)
votante = inst_old.groups.filter(name='Votante').first() votante = inst_old.groups.filter(name='Votante').first()
autor = inst_old.groups.filter(name='Autor').first()
inst_new = super().save(commit) inst_new = super().save(commit)
if votante: if votante:
inst_new.groups.add(votante) inst_new.groups.add(votante)
if autor:
inst_new.groups.add(autor)
return inst_new return inst_new
def clean(self): def clean(self):
@ -211,6 +217,19 @@ class UserAdminForm(ModelForm):
if new_password1 and new_password2: if new_password1 and new_password2:
password_validation.validate_password( password_validation.validate_password(
new_password2, self.instance) 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 return data

9
sapl/base/views.py

@ -1959,6 +1959,7 @@ class UserCrud(Crud):
class CreateView(Crud.CreateView): class CreateView(Crud.CreateView):
form_class = UserAdminForm form_class = UserAdminForm
layout_key = None
class UpdateView(Crud.UpdateView): class UpdateView(Crud.UpdateView):
form_class = UserAdminForm form_class = UserAdminForm
@ -2017,6 +2018,7 @@ class UserCrud(Crud):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = Crud.ListView.get_context_data(self, **kwargs) context = Crud.ListView.get_context_data(self, **kwargs)
context['subnav_template_name'] = None context['subnav_template_name'] = None
context['title'] = _('Usuários')
return context return context
def hook_header_usuario(self, *args, **kwargs): def hook_header_usuario(self, *args, **kwargs):
@ -2025,8 +2027,8 @@ class UserCrud(Crud):
def hook_header_groups(self, *args, **kwargs): def hook_header_groups(self, *args, **kwargs):
return 'Grupos' return 'Grupos'
def hook_header_active(self, *args, **kwargs): def hook_header_is_active(self, *args, **kwargs):
return 'is_active' return 'Ativo'
def hook_usuario(self, *args, **kwargs): def hook_usuario(self, *args, **kwargs):
return '{} <i>({})</i><br><small>{}</small>'.format( return '{} <i>({})</i><br><small>{}</small>'.format(
@ -2047,9 +2049,6 @@ class UserCrud(Crud):
return qs return qs
def dispatch(self, request, *args, **kwargs):
return Crud.ListView.dispatch(self, request, *args, **kwargs)
class CasaLegislativaCrud(CrudAux): class CasaLegislativaCrud(CrudAux):
model = CasaLegislativa model = CasaLegislativa

Loading…
Cancel
Save