Browse Source

Refatora envio e confirmação de email para autores

pull/754/head
LeandroRoberto 8 years ago
parent
commit
e5496a54a0
  1. 7
      sapl/base/urls.py
  2. 60
      sapl/base/views.py
  3. 5
      sapl/materia/urls.py
  4. 12
      sapl/materia/views.py
  5. 0
      sapl/templates/email/confirma.html

7
sapl/base/urls.py

@ -3,7 +3,7 @@ from django.contrib.auth import views
from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required
from django.views.generic.base import TemplateView 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 .apps import AppConfig
from .forms import LoginForm from .forms import LoginForm
@ -52,6 +52,11 @@ urlpatterns = [
RelatorioAtasView.as_view(), RelatorioAtasView.as_view(),
name='atas'), name='atas'),
url(r'^email/validate/(?P<uidb64>[0-9A-Za-z_\-]+)/'
'(?P<token>[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 # todos os sublink s de sistema devem vir acima deste
url(r'^sistema/', permission_required('base.view_tabelas_auxiliares') url(r'^sistema/', permission_required('base.view_tabelas_auxiliares')
(TemplateView.as_view(template_name='sistema.html'))), (TemplateView.as_view(template_name='sistema.html'))),

60
sapl/base/views.py

@ -1,5 +1,6 @@
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.contrib.auth.tokens import default_token_generator 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.db.models import Count, Q
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.utils.encoding import force_bytes 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.utils.translation import ugettext_lazy as _
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django_filters.views import FilterView 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.materia.models import MateriaLegislativa, TipoMateriaLegislativa
from sapl.parlamentares.models import Parlamentar from sapl.parlamentares.models import Parlamentar
from sapl.sessao.models import OrdemDia, SessaoPlenaria from sapl.sessao.models import OrdemDia, SessaoPlenaria
from sapl.utils import sapl_logger
from .forms import (CasaLegislativaForm, ConfiguracoesAppForm, from .forms import (CasaLegislativaForm, ConfiguracoesAppForm,
RelatorioAtasFilterSet, RelatorioAtasFilterSet,
@ -34,6 +36,18 @@ def get_casalegislativa():
return CasaLegislativa.objects.first() 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): class TipoAutorCrud(CrudAux):
model = TipoAutor model = TipoAutor
help_path = 'tipo-autor' help_path = 'tipo-autor'
@ -69,23 +83,26 @@ class AutorCrud(CrudAux):
# devido a implement do form o form_valid do Crud deve ser pulado # devido a implement do form o form_valid do Crud deve ser pulado
return super(CrudAux.UpdateView, self).form_valid(form) 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): def get(self, request, *args, **kwargs):
if request.user.is_superuser: if request.user.is_superuser:
self.form_class = AutorFormForAdmin self.form_class = AutorFormForAdmin
return CrudAux.UpdateView.get(self, request, *args, **kwargs) return CrudAux.UpdateView.get(self, request, *args, **kwargs)
def get_success_url(self): def get_success_url(self):
# FIXME try except - testar envio de emails
pk_autor = self.object.id pk_autor = self.object.id
url_reverse = reverse('sapl.base:autor_detail',
kwargs={'pk': pk_autor})
try: try:
kwargs = {} kwargs = {}
user = self.object.user user = self.object.user
if user.is_active: if not user:
return reverse('sapl.base:autor_detail', return url_reverse
kwargs={'pk': pk_autor})
kwargs['token'] = default_token_generator.make_token(user) kwargs['token'] = default_token_generator.make_token(user)
kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk)) 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 " + "Caso você não tenha feito este cadastro, por favor " +
"ignore esta mensagem. Caso tenha, clique " + "ignore esta mensagem. Caso tenha, clique " +
"no link abaixo\n" + url_base + "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] remetente = [settings.EMAIL_SEND_USER]
destinatario = [user.email] destinatario = [user.email]
send_mail(assunto, mensagem, remetente, destinatario, send_mail(assunto, mensagem, remetente, destinatario,
fail_silently=False) fail_silently=False)
except: except:
pass sapl_logger.error(
return reverse('sapl.base:autor_detail', _('Erro no envio de email na edição de Autores.'))
kwargs={'pk': pk_autor}) return url_reverse
class CreateView(CrudAux.CreateView): class CreateView(CrudAux.CreateView):
form_class = AutorForm form_class = AutorForm
@ -120,6 +137,11 @@ class AutorCrud(CrudAux):
# devido a implement do form o form_valid do Crud deve ser pulado # devido a implement do form o form_valid do Crud deve ser pulado
return super(CrudAux.CreateView, self).form_valid(form) 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): def get(self, request, *args, **kwargs):
if request.user.is_superuser: if request.user.is_superuser:
self.form_class = AutorFormForAdmin self.form_class = AutorFormForAdmin
@ -127,10 +149,15 @@ class AutorCrud(CrudAux):
def get_success_url(self): def get_success_url(self):
pk_autor = self.object.id pk_autor = self.object.id
url_reverse = reverse('sapl.base:autor_detail',
kwargs={'pk': pk_autor})
try: try:
# FIXME try except - testar envio de emails
kwargs = {} kwargs = {}
user = self.object.user user = self.object.user
if not user:
return url_reverse
kwargs['token'] = default_token_generator.make_token(user) kwargs['token'] = default_token_generator.make_token(user)
kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk)) kwargs['uidb64'] = urlsafe_base64_encode(force_bytes(user.pk))
assunto = "SAPL - Confirmação de Conta" assunto = "SAPL - Confirmação de Conta"
@ -146,16 +173,15 @@ class AutorCrud(CrudAux):
"Caso você não tenha feito este cadastro, por favor " + "Caso você não tenha feito este cadastro, por favor " +
"ignore esta mensagem. Caso tenha, clique " + "ignore esta mensagem. Caso tenha, clique " +
"no link abaixo\n" + url_base + "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 remetente = settings.EMAIL_SEND_USER
destinatario = [user.email] destinatario = [user.email]
send_mail(assunto, mensagem, remetente, destinatario, send_mail(assunto, mensagem, remetente, destinatario,
fail_silently=False) fail_silently=False)
except: except:
pass sapl_logger.error(
_('Erro no envio de email na criação de Autores.'))
return reverse('sapl.base:autor_detail', return url_reverse
kwargs={'pk': pk_autor})
class RelatorioAtasView(FilterView): class RelatorioAtasView(FilterView):

5
sapl/materia/urls.py

@ -3,7 +3,7 @@ from django.conf.urls import include, url
from sapl.materia.views import (AcompanhamentoConfirmarView, from sapl.materia.views import (AcompanhamentoConfirmarView,
AcompanhamentoExcluirView, AcompanhamentoExcluirView,
AcompanhamentoMateriaView, AnexadaCrud, AcompanhamentoMateriaView, AnexadaCrud,
AutoriaCrud, ConfirmarEmailView, AutoriaCrud,
ConfirmarProposicao, DespachoInicialCrud, ConfirmarProposicao, DespachoInicialCrud,
DocumentoAcessorioCrud, DocumentoAcessorioCrud,
DocumentoAcessorioEmLoteView, DocumentoAcessorioEmLoteView,
@ -39,9 +39,6 @@ urlpatterns_materia = [
url(r'^materia/(?P<pk>[0-9]+)/ta$', url(r'^materia/(?P<pk>[0-9]+)/ta$',
MateriaTaView.as_view(), name='materia_ta'), MateriaTaView.as_view(), name='materia_ta'),
url(r'^materia/confirmar/(?P<uidb64>[0-9A-Za-z_\-]+)/'
'(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})$',
ConfirmarEmailView.as_view(), name='confirmar_email'),
url(r'^materia/pesquisar-materia$', url(r'^materia/pesquisar-materia$',
MateriaLegislativaPesquisaView.as_view(), name='pesquisar_materia'), MateriaLegislativaPesquisaView.as_view(), name='pesquisar_materia'),

12
sapl/materia/views.py

@ -120,18 +120,6 @@ def recuperar_materia(request):
return response 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') OrgaoCrud = CrudAux.build(Orgao, 'orgao')
TipoProposicaoCrud = CrudAux.build(TipoProposicao, 'tipo_proposicao') TipoProposicaoCrud = CrudAux.build(TipoProposicao, 'tipo_proposicao')
StatusTramitacaoCrud = CrudAux.build(StatusTramitacao, 'status_tramitacao') StatusTramitacaoCrud = CrudAux.build(StatusTramitacao, 'status_tramitacao')

0
sapl/templates/confirma_email.html → sapl/templates/email/confirma.html

Loading…
Cancel
Save