diff --git a/sapl/base/urls.py b/sapl/base/urls.py index e06580cfa..4fbda888b 100644 --- a/sapl/base/urls.py +++ b/sapl/base/urls.py @@ -3,7 +3,7 @@ from django.contrib.auth import views from django.contrib.auth.decorators import permission_required from django.views.generic.base import TemplateView -from sapl.base.views import AutorCrud, TipoAutorCrud +from sapl.base.views import AutorCrud, TipoAutorCrud, ConfirmarEmailView from .apps import AppConfig from .forms import LoginForm @@ -52,6 +52,11 @@ urlpatterns = [ RelatorioAtasView.as_view(), name='atas'), + url(r'^email/validate/(?P[0-9A-Za-z_\-]+)/' + '(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})$', + ConfirmarEmailView.as_view(), name='confirmar_email'), + + # todos os sublink s de sistema devem vir acima deste url(r'^sistema/', permission_required('base.view_tabelas_auxiliares') (TemplateView.as_view(template_name='sistema.html'))), diff --git a/sapl/base/views.py b/sapl/base/views.py index 83cc776f1..ff0910294 100644 --- a/sapl/base/views.py +++ b/sapl/base/views.py @@ -1,5 +1,6 @@ from django.conf import settings +from django.contrib.auth import get_user_model from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.models import Group from django.contrib.auth.tokens import default_token_generator @@ -8,7 +9,7 @@ from django.core.urlresolvers import reverse from django.db.models import Count, Q from django.http import HttpResponseRedirect from django.utils.encoding import force_bytes -from django.utils.http import urlsafe_base64_encode +from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode from django.utils.translation import ugettext_lazy as _ from django.views.generic.base import TemplateView from django_filters.views import FilterView @@ -19,6 +20,7 @@ from sapl.crud.base import CrudAux from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa from sapl.parlamentares.models import Parlamentar from sapl.sessao.models import OrdemDia, SessaoPlenaria +from sapl.utils import sapl_logger from .forms import (CasaLegislativaForm, ConfiguracoesAppForm, RelatorioAtasFilterSet, @@ -34,6 +36,18 @@ def get_casalegislativa(): return CasaLegislativa.objects.first() +class ConfirmarEmailView(TemplateView): + template_name = "email/confirma.html" + + def get(self, request, *args, **kwargs): + uid = urlsafe_base64_decode(self.kwargs['uidb64']) + user = get_user_model().objects.get(id=uid) + user.is_active = True + user.save() + context = self.get_context_data(**kwargs) + return self.render_to_response(context) + + class TipoAutorCrud(CrudAux): model = TipoAutor help_path = 'tipo-autor' @@ -69,23 +83,26 @@ class AutorCrud(CrudAux): # devido a implement do form o form_valid do Crud deve ser pulado return super(CrudAux.UpdateView, self).form_valid(form) + def post(self, request, *args, **kwargs): + if request.user.is_superuser: + self.form_class = AutorFormForAdmin + return CrudAux.UpdateView.post(self, request, *args, **kwargs) + def get(self, request, *args, **kwargs): if request.user.is_superuser: self.form_class = AutorFormForAdmin return CrudAux.UpdateView.get(self, request, *args, **kwargs) def get_success_url(self): - - # FIXME try except - testar envio de emails - pk_autor = self.object.id + url_reverse = reverse('sapl.base:autor_detail', + kwargs={'pk': pk_autor}) try: kwargs = {} user = self.object.user - if user.is_active: - return reverse('sapl.base:autor_detail', - kwargs={'pk': pk_autor}) + if not user: + return url_reverse kwargs['token'] = default_token_generator.make_token(user) kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk)) @@ -102,15 +119,15 @@ class AutorCrud(CrudAux): "Caso você não tenha feito este cadastro, por favor " + "ignore esta mensagem. Caso tenha, clique " + "no link abaixo\n" + url_base + - reverse('sapl.materia:confirmar_email', kwargs=kwargs)) + reverse('sapl.base:confirmar_email', kwargs=kwargs)) remetente = [settings.EMAIL_SEND_USER] destinatario = [user.email] send_mail(assunto, mensagem, remetente, destinatario, fail_silently=False) except: - pass - return reverse('sapl.base:autor_detail', - kwargs={'pk': pk_autor}) + sapl_logger.error( + _('Erro no envio de email na edição de Autores.')) + return url_reverse class CreateView(CrudAux.CreateView): form_class = AutorForm @@ -120,6 +137,11 @@ class AutorCrud(CrudAux): # devido a implement do form o form_valid do Crud deve ser pulado return super(CrudAux.CreateView, self).form_valid(form) + def post(self, request, *args, **kwargs): + if request.user.is_superuser: + self.form_class = AutorFormForAdmin + return CrudAux.CreateView.post(self, request, *args, **kwargs) + def get(self, request, *args, **kwargs): if request.user.is_superuser: self.form_class = AutorFormForAdmin @@ -127,10 +149,15 @@ class AutorCrud(CrudAux): def get_success_url(self): pk_autor = self.object.id + url_reverse = reverse('sapl.base:autor_detail', + kwargs={'pk': pk_autor}) try: - # FIXME try except - testar envio de emails kwargs = {} user = self.object.user + + if not user: + return url_reverse + kwargs['token'] = default_token_generator.make_token(user) kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk)) assunto = "SAPL - Confirmação de Conta" @@ -146,16 +173,15 @@ class AutorCrud(CrudAux): "Caso você não tenha feito este cadastro, por favor " + "ignore esta mensagem. Caso tenha, clique " + "no link abaixo\n" + url_base + - reverse('sapl.materia:confirmar_email', kwargs=kwargs)) + reverse('sapl.base:confirmar_email', kwargs=kwargs)) remetente = settings.EMAIL_SEND_USER destinatario = [user.email] send_mail(assunto, mensagem, remetente, destinatario, fail_silently=False) except: - pass - - return reverse('sapl.base:autor_detail', - kwargs={'pk': pk_autor}) + sapl_logger.error( + _('Erro no envio de email na criação de Autores.')) + return url_reverse class RelatorioAtasView(FilterView): diff --git a/sapl/materia/urls.py b/sapl/materia/urls.py index a6f85016a..5400978c6 100644 --- a/sapl/materia/urls.py +++ b/sapl/materia/urls.py @@ -3,7 +3,7 @@ from django.conf.urls import include, url from sapl.materia.views import (AcompanhamentoConfirmarView, AcompanhamentoExcluirView, AcompanhamentoMateriaView, AnexadaCrud, - AutoriaCrud, ConfirmarEmailView, + AutoriaCrud, ConfirmarProposicao, DespachoInicialCrud, DocumentoAcessorioCrud, DocumentoAcessorioEmLoteView, @@ -39,9 +39,6 @@ urlpatterns_materia = [ url(r'^materia/(?P[0-9]+)/ta$', MateriaTaView.as_view(), name='materia_ta'), - url(r'^materia/confirmar/(?P[0-9A-Za-z_\-]+)/' - '(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})$', - ConfirmarEmailView.as_view(), name='confirmar_email'), url(r'^materia/pesquisar-materia$', MateriaLegislativaPesquisaView.as_view(), name='pesquisar_materia'), diff --git a/sapl/materia/views.py b/sapl/materia/views.py index 600283648..492091880 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -120,18 +120,6 @@ def recuperar_materia(request): return response -class ConfirmarEmailView(TemplateView): - template_name = "confirma_email.html" - - def get(self, request, *args, **kwargs): - uid = urlsafe_base64_decode(self.kwargs['uidb64']) - user = get_user_model().objects.get(id=uid) - user.is_active = True - user.save() - context = self.get_context_data(**kwargs) - return self.render_to_response(context) - - OrgaoCrud = CrudAux.build(Orgao, 'orgao') TipoProposicaoCrud = CrudAux.build(TipoProposicao, 'tipo_proposicao') StatusTramitacaoCrud = CrudAux.build(StatusTramitacao, 'status_tramitacao') diff --git a/sapl/templates/confirma_email.html b/sapl/templates/email/confirma.html similarity index 100% rename from sapl/templates/confirma_email.html rename to sapl/templates/email/confirma.html