|
@ -1,4 +1,3 @@ |
|
|
import django_filters |
|
|
|
|
|
from crispy_forms.bootstrap import FieldWithButtons, InlineRadios, StrictButton |
|
|
from crispy_forms.bootstrap import FieldWithButtons, InlineRadios, StrictButton |
|
|
from crispy_forms.helper import FormHelper |
|
|
from crispy_forms.helper import FormHelper |
|
|
from crispy_forms.layout import HTML, Button, Div, Field, Fieldset, Layout, Row |
|
|
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.core.exceptions import ValidationError |
|
|
from django.db import models, transaction |
|
|
from django.db import models, transaction |
|
|
from django.forms import ModelForm |
|
|
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 string_concat |
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
|
|
import django_filters |
|
|
|
|
|
|
|
|
from sapl.base.models import Autor, TipoAutor |
|
|
from sapl.base.models import Autor, TipoAutor |
|
|
from sapl.crispy_layout_mixin import (SaplFormLayout, form_actions, to_column, |
|
|
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 |
|
|
from .models import AppConfig, CasaLegislativa |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ACTION_CREATE_USERS_AUTOR_CHOICE = [ |
|
|
ACTION_CREATE_USERS_AUTOR_CHOICE = [ |
|
|
('C', _('Criar novo Usuário')), |
|
|
('C', _('Criar novo Usuário')), |
|
|
('A', _('Associar um usuário existente')), |
|
|
('A', _('Associar um usuário existente')), |
|
@ -96,7 +97,7 @@ class AutorForm(ModelForm): |
|
|
label=_('Confirmar Email')) |
|
|
label=_('Confirmar Email')) |
|
|
|
|
|
|
|
|
username = forms.CharField(label=get_user_model()._meta.get_field( |
|
|
username = forms.CharField(label=get_user_model()._meta.get_field( |
|
|
'username').verbose_name.capitalize(), |
|
|
get_user_model().USERNAME_FIELD).verbose_name.capitalize(), |
|
|
required=False, |
|
|
required=False, |
|
|
max_length=50) |
|
|
max_length=50) |
|
|
|
|
|
|
|
@ -188,26 +189,36 @@ class AutorForm(ModelForm): |
|
|
self.fields['autor_related'].initial = self.instance.autor_related |
|
|
self.fields['autor_related'].initial = self.instance.autor_related |
|
|
|
|
|
|
|
|
if self.instance.user: |
|
|
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['action_user'].initial = 'A' |
|
|
|
|
|
|
|
|
self.fields['username'].label = string_concat( |
|
|
self.fields['username'].label = string_concat( |
|
|
self.fields['username'].label, |
|
|
self.fields['username'].label, |
|
|
' (', self.instance.user.username, ')') |
|
|
' (', getattr( |
|
|
|
|
|
self.instance.user, |
|
|
|
|
|
get_user_model().USERNAME_FIELD), ')') |
|
|
|
|
|
|
|
|
if 'status_user' in self.Meta.fields: |
|
|
if 'status_user' in self.Meta.fields: |
|
|
self.fields['status_user'].initial = 'R' |
|
|
self.fields['status_user'].initial = 'R' |
|
|
self.fields['status_user'].label = string_concat( |
|
|
self.fields['status_user'].label = string_concat( |
|
|
self.fields['status_user'].label, |
|
|
self.fields['status_user'].label, |
|
|
' (', self.instance.user.username, ')') |
|
|
' (', getattr( |
|
|
|
|
|
self.instance.user, |
|
|
|
|
|
get_user_model().USERNAME_FIELD), ')') |
|
|
|
|
|
|
|
|
self.fields['username'].widget.attrs.update({ |
|
|
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 self.instance.user else ''}) |
|
|
|
|
|
|
|
|
if 'status_user' in self.Meta.fields: |
|
|
if 'status_user' in self.Meta.fields: |
|
|
self.fields['status_user'].widget.attrs.update({ |
|
|
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 ''}) |
|
|
if self.instance.user else ''}) |
|
|
|
|
|
|
|
|
def valida_igualdade(self, texto1, texto2, msg): |
|
|
def valida_igualdade(self, texto1, texto2, msg): |
|
@ -225,7 +236,9 @@ class AutorForm(ModelForm): |
|
|
|
|
|
|
|
|
if 'status_user' in self.Meta.fields: |
|
|
if 'status_user' in self.Meta.fields: |
|
|
if self.instance.pk and self.instance.user_id: |
|
|
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']: |
|
|
if 'status_user' not in cd or not cd['status_user']: |
|
|
raise ValidationError( |
|
|
raise ValidationError( |
|
|
_('Foi trocado ou removido o usuário deste Autor, ' |
|
|
_('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) |
|
|
qs_user = qs_user.exclude(pk=self.instance.user.pk) |
|
|
|
|
|
|
|
|
if cd['action_user'] == 'C': |
|
|
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( |
|
|
raise ValidationError( |
|
|
_('Já existe usuário com o username "%s". ' |
|
|
_('Já existe usuário com o username "%s". ' |
|
|
'Para utilizar esse username você deve selecionar ' |
|
|
'Para utilizar esse username você deve selecionar ' |
|
@ -275,7 +289,8 @@ class AutorForm(ModelForm): |
|
|
_('Já existe um Autor com este email.')) |
|
|
_('Já existe um Autor com este email.')) |
|
|
|
|
|
|
|
|
elif cd['action_user'] == 'A': |
|
|
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( |
|
|
raise ValidationError( |
|
|
_('Não existe usuário com username "%s". ' |
|
|
_('Não existe usuário com username "%s". ' |
|
|
'Para utilizar esse username você deve selecionar ' |
|
|
'Para utilizar esse username você deve selecionar ' |
|
@ -286,7 +301,9 @@ class AutorForm(ModelForm): |
|
|
if 'username' not in cd or not cd['username']: |
|
|
if 'username' not in cd or not cd['username']: |
|
|
raise ValidationError(_('O username deve ser informado.')) |
|
|
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( |
|
|
raise ValidationError( |
|
|
_('Já existe um Autor para este usuário.')) |
|
|
_('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 |
|
|
user_old = autor.user if autor.user_id else None |
|
|
|
|
|
|
|
|
u = None |
|
|
u = None |
|
|
|
|
|
param_username = { |
|
|
|
|
|
get_user_model().USERNAME_FIELD: self.cleaned_data['username']} |
|
|
if self.cleaned_data['action_user'] == 'A': |
|
|
if self.cleaned_data['action_user'] == 'A': |
|
|
u = get_user_model().objects.get( |
|
|
u = get_user_model().objects.get(**param_username) |
|
|
username=self.cleaned_data['username']) |
|
|
|
|
|
if not u.is_active: |
|
|
if not u.is_active: |
|
|
u.is_active = settings.DEBUG |
|
|
u.is_active = settings.DEBUG |
|
|
u.save() |
|
|
u.save() |
|
|
elif self.cleaned_data['action_user'] == 'C': |
|
|
elif self.cleaned_data['action_user'] == 'C': |
|
|
|
|
|
|
|
|
u = get_user_model().objects.create( |
|
|
u = get_user_model().objects.create( |
|
|
username=self.cleaned_data['username'], |
|
|
username=self.cleaned_data['username'], |
|
|
email=self.cleaned_data['email']) |
|
|
email=self.cleaned_data['email']) |
|
|
|
|
|
|
|
|
u.set_password(self.cleaned_data['senha']) |
|
|
u.set_password(self.cleaned_data['senha']) |
|
|
# Define usuário como ativo em ambiente de desenvolvimento |
|
|
# Define usuário como ativo em ambiente de desenvolvimento |
|
|
# pode logar sem a necessidade de passar pela validação de email |
|
|
# pode logar sem a necessidade de passar pela validação de email |
|
|