From 19f955c1b3efb6806b714a42ab0f8a42abe80a5b Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Wed, 23 Nov 2016 10:44:48 -0200 Subject: [PATCH] =?UTF-8?q?corrige=20associa=C3=A7=C3=A3o=20com=20username?= =?UTF-8?q?=20e=20erro=20na=20api=20rest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/api/serializers.py | 1 + sapl/base/forms.py | 48 +++++++++++++++++++++++++++++------------ setup.py | 26 +++++++++++----------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/sapl/api/serializers.py b/sapl/api/serializers.py index bd3fb7def..a7ff559e3 100644 --- a/sapl/api/serializers.py +++ b/sapl/api/serializers.py @@ -51,3 +51,4 @@ class MateriaLegislativaSerializer(serializers.ModelSerializer): class Meta: model = MateriaLegislativa + fields = '__all__' diff --git a/sapl/base/forms.py b/sapl/base/forms.py index 5b4d9f734..75145bba7 100644 --- a/sapl/base/forms.py +++ b/sapl/base/forms.py @@ -1,4 +1,3 @@ -import django_filters from crispy_forms.bootstrap import FieldWithButtons, InlineRadios, StrictButton from crispy_forms.helper import FormHelper from crispy_forms.layout import HTML, Button, Div, Field, Fieldset, Layout, Row @@ -12,8 +11,9 @@ from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from django.db import models, transaction from django.forms import ModelForm -from django.utils.translation import ugettext_lazy as _ from django.utils.translation import string_concat +from django.utils.translation import ugettext_lazy as _ +import django_filters from sapl.base.models import Autor, TipoAutor from sapl.crispy_layout_mixin import (SaplFormLayout, form_actions, to_column, @@ -27,6 +27,7 @@ from sapl.utils import (RANGE_ANOS, ChoiceWithoutValidationField, from .models import AppConfig, CasaLegislativa + ACTION_CREATE_USERS_AUTOR_CHOICE = [ ('C', _('Criar novo Usuário')), ('A', _('Associar um usuário existente')), @@ -96,7 +97,7 @@ class AutorForm(ModelForm): label=_('Confirmar Email')) username = forms.CharField(label=get_user_model()._meta.get_field( - 'username').verbose_name.capitalize(), + get_user_model().USERNAME_FIELD).verbose_name.capitalize(), required=False, max_length=50) @@ -188,26 +189,36 @@ class AutorForm(ModelForm): self.fields['autor_related'].initial = self.instance.autor_related if self.instance.user: - self.fields['username'].initial = self.instance.user.username + self.fields['username'].initial = getattr( + self.instance.user, + get_user_model().USERNAME_FIELD) self.fields['action_user'].initial = 'A' self.fields['username'].label = string_concat( self.fields['username'].label, - ' (', self.instance.user.username, ')') + ' (', getattr( + self.instance.user, + get_user_model().USERNAME_FIELD), ')') if 'status_user' in self.Meta.fields: self.fields['status_user'].initial = 'R' self.fields['status_user'].label = string_concat( self.fields['status_user'].label, - ' (', self.instance.user.username, ')') + ' (', getattr( + self.instance.user, + get_user_model().USERNAME_FIELD), ')') self.fields['username'].widget.attrs.update({ - 'data': self.instance.user.username + 'data': getattr( + self.instance.user, + get_user_model().USERNAME_FIELD) if self.instance.user else ''}) if 'status_user' in self.Meta.fields: self.fields['status_user'].widget.attrs.update({ - 'data': self.instance.user.username + 'data': getattr( + self.instance.user, + get_user_model().USERNAME_FIELD) if self.instance.user else ''}) def valida_igualdade(self, texto1, texto2, msg): @@ -225,7 +236,9 @@ class AutorForm(ModelForm): if 'status_user' in self.Meta.fields: if self.instance.pk and self.instance.user_id: - if self.instance.user.username != cd['username']: + if getattr( + self.instance.user.username, + get_user_model().USERNAME_FIELD) != cd['username']: if 'status_user' not in cd or not cd['status_user']: raise ValidationError( _('Foi trocado ou removido o usuário deste Autor, ' @@ -241,7 +254,8 @@ class AutorForm(ModelForm): qs_user = qs_user.exclude(pk=self.instance.user.pk) if cd['action_user'] == 'C': - if User.objects.filter(username=cd['username']).exists(): + param_username = {get_user_model().USERNAME_FIELD: cd['username']} + if User.objects.filter(**param_username).exists(): raise ValidationError( _('Já existe usuário com o username "%s". ' 'Para utilizar esse username você deve selecionar ' @@ -275,7 +289,8 @@ class AutorForm(ModelForm): _('Já existe um Autor com este email.')) elif cd['action_user'] == 'A': - if not User.objects.filter(username=cd['username']).exists(): + param_username = {get_user_model().USERNAME_FIELD: cd['username']} + if not User.objects.filter(**param_username).exists(): raise ValidationError( _('Não existe usuário com username "%s". ' 'Para utilizar esse username você deve selecionar ' @@ -286,7 +301,9 @@ class AutorForm(ModelForm): if 'username' not in cd or not cd['username']: raise ValidationError(_('O username deve ser informado.')) - if qs_autor.filter(user__username=cd['username']).exists(): + param_username = { + 'user__' + get_user_model().USERNAME_FIELD: cd['username']} + if qs_autor.filter(**param_username).exists(): raise ValidationError( _('Já existe um Autor para este usuário.')) @@ -332,16 +349,19 @@ class AutorForm(ModelForm): user_old = autor.user if autor.user_id else None u = None + param_username = { + get_user_model().USERNAME_FIELD: self.cleaned_data['username']} if self.cleaned_data['action_user'] == 'A': - u = get_user_model().objects.get( - username=self.cleaned_data['username']) + u = get_user_model().objects.get(**param_username) if not u.is_active: u.is_active = settings.DEBUG u.save() elif self.cleaned_data['action_user'] == 'C': + u = get_user_model().objects.create( username=self.cleaned_data['username'], email=self.cleaned_data['email']) + u.set_password(self.cleaned_data['senha']) # Define usuário como ativo em ambiente de desenvolvimento # pode logar sem a necessidade de passar pela validação de email diff --git a/setup.py b/setup.py index 330749885..ab6d283ac 100644 --- a/setup.py +++ b/setup.py @@ -13,27 +13,27 @@ install_requires = [ 'django-admin-bootstrapped==2.5.7', 'django-bootstrap3==7.0.1', 'django-bower==5.1.0', - 'django-braces==1.8.1', + 'django-braces==1.9.0', 'django-compressor==2.0', 'django-crispy-forms==1.6.0', - 'django-extensions==1.6.1', - 'django-extra-views==0.7.1', - 'django-filter==0.13.0', - 'django-floppyforms==1.6.1', - 'django-model-utils==2.4', - 'django-sass-processor==0.3.4', - 'django>=1.9.5', + 'django-extensions==1.6.7', + 'django-extra-views==0.8.0', + 'django-filter==0.15.3', + 'django-floppyforms==1.6.2', + 'django-model-utils==2.5', + 'django-sass-processor==0.4.6', + 'django==1.9.7', 'djangorestframework', 'easy-thumbnails==2.3', - 'libsass==0.11.0', - 'psycopg2==2.6.1', + 'libsass==0.11.1', + 'psycopg2==2.6.2', 'python-decouple==3.0', - 'pytz==2016.3', + 'pytz==2016.4', 'pyyaml==3.11', - 'rtyaml==0.0.2', + 'rtyaml==0.0.3', 'unipath==1.1', 'python-magic==0.4.10', - 'gunicorn==19.4.5', + 'gunicorn==19.6.0', # git+git://github.com/interlegis/trml2pdf.git ] setup(