From 3f35b7a79989395794c8f0a8221c375e0a97353f Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Wed, 22 Jul 2015 11:26:56 -0300 Subject: [PATCH] Improve url routing between crud views --- comissoes/views.py | 4 +- sapl/crud.py | 52 +++++++++++++++++++----- templates/comissoes/comissao_detail.html | 7 ++++ 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 templates/comissoes/comissao_detail.html diff --git a/comissoes/views.py b/comissoes/views.py index 6717a0f97..92cee3499 100644 --- a/comissoes/views.py +++ b/comissoes/views.py @@ -1,7 +1,5 @@ -from django.utils.translation import ugettext as _ - from .forms import ComissaoForm from sapl.crud import Crud -comissao_crud = Crud(ComissaoForm, _('Nova Comissão')) +comissao_crud = Crud(ComissaoForm) diff --git a/sapl/crud.py b/sapl/crud.py index 1dd592c6a..f89555d77 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -1,16 +1,16 @@ from braces.views import FormMessagesMixin from django.conf.urls import url from django.core.urlresolvers import reverse, reverse_lazy +from django.utils.translation import ugettext_lazy as _ from django.views.generic import ( CreateView, DeleteView, ListView, UpdateView, DetailView) class Crud(object): - def __init__(self, model_form, create_title): + def __init__(self, model_form): self.model_form = model_form - self.create_title = create_title self.model = model_form._meta.model @@ -20,25 +20,49 @@ class Crud(object): def in_namespace(url_name): return '%s:%s' % (self.namespace, url_name) + def make_form_invalid_message(msg): + return '%s %s' % (_('Formulário inválido.'), msg) + class BaseMixin(object): @property def title(self): return self.get_object() + list_url = reverse_lazy(in_namespace('list')) + create_url = reverse_lazy(in_namespace('create')) help_url = '/comissoes/ajuda' # FIXME + def get_url_for_this_object(self, url_name): + return reverse(in_namespace(url_name), args=(self.object.id,)) + + @property + def detail_url(self): + return self.get_url_for_this_object('detail') + + @property + def update_url(self): + return self.get_url_for_this_object('detail') + + @property + def delete_url(self): + return self.get_url_for_this_object('detail') + class CrudListView(BaseMixin, ListView): model = self.model title = model._meta.verbose_name_plural - class CrudCreateView(BaseMixin, CreateView): + class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): model = self.model form_class = model_form - title = self.create_title + title = _('Adicionar %(model_name)s') % { + 'model_name': self.model._meta.verbose_name} + form_valid_message = _('Registro criado com sucesso!') + form_invalid_message = make_form_invalid_message( + _('O registro não foi criado.')) def get_success_url(self): - return reverse(in_namespace('detail'), args=(self.object.id,)) + return self.detail_url class CrudDetailView(BaseMixin, DetailView): model = self.model @@ -46,15 +70,21 @@ class Crud(object): class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView): model = self.model form_class = model_form - success_url = reverse_lazy('comissao_list') - form_invalid_message = u"Something went wrong, post was not saved" + form_valid_message = _('Mudanças salvas com sucesso!') + form_invalid_message = make_form_invalid_message( + _('Suas mudanças não foram salvas.')) - def get_form_valid_message(self): - return u"{0} updated successfully!".format(self.object) + def get_success_url(self): + return self.detail_url - class CrudDeleteView(BaseMixin, DeleteView): + class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView): model = self.model - success_url = reverse_lazy('comissao_list') + form_valid_message = _('Registro excluído com sucesso!') + form_invalid_message = make_form_invalid_message( + _('O registro não foi excluído.')) + + def get_success_url(self): + return self.list_url self.CrudListView = CrudListView self.CrudCreateView = CrudCreateView diff --git a/templates/comissoes/comissao_detail.html b/templates/comissoes/comissao_detail.html new file mode 100644 index 000000000..db63bc5a5 --- /dev/null +++ b/templates/comissoes/comissao_detail.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% load i18n crispy_forms_tags %} + +{% block base_content %} + TODO ... +{% endblock %} +