From 0c71c1d91c63549f9f4d8f1e748d5d43b15311a7 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Tue, 21 Jul 2015 21:23:53 -0300 Subject: [PATCH] Start crud abstraction --- comissoes/urls.py | 13 ------- comissoes/views.py | 94 +++++++++++++++++++++++++++++++++------------- sapl/urls.py | 4 +- 3 files changed, 71 insertions(+), 40 deletions(-) delete mode 100644 comissoes/urls.py diff --git a/comissoes/urls.py b/comissoes/urls.py deleted file mode 100644 index 0efd513aa..000000000 --- a/comissoes/urls.py +++ /dev/null @@ -1,13 +0,0 @@ -from django.conf.urls import url - -from comissoes.views import ( - ComissaoListView, ComissaoDetailView, ComissaoUpdateView) - - -urlpatterns = [ - url(r'^$', ComissaoListView.as_view(), name='comissao_list'), - url(r'^(?P\d+)$', ComissaoDetailView.as_view(), - name='comissao_detail'), - url(r'^(?P\d+)/edit$', ComissaoUpdateView.as_view(), - name='comissao_update'), -] diff --git a/comissoes/views.py b/comissoes/views.py index 7891a4c95..bdb08d12a 100644 --- a/comissoes/views.py +++ b/comissoes/views.py @@ -1,47 +1,89 @@ from braces.views import FormMessagesMixin -from django.core.urlresolvers import reverse_lazy +from django.conf.urls import url +from django.core.urlresolvers import reverse, reverse_lazy from django.utils.translation import ugettext as _ from django.views.generic import ( CreateView, DeleteView, ListView, UpdateView, DetailView) from .forms import ComissaoForm -from .models import Comissao -class BaseMixin(object): +class Crud(object): - @property - def title(self): - return self.get_object() + def __init__(self, model_form, create_title): - help_url = '/comissoes/ajuda' + self.model_form = model_form + self.create_title = create_title + self.model = model_form._meta.model -class ComissaoListView(BaseMixin, ListView): - model = Comissao - title = Comissao._meta.verbose_name_plural + # urls names + self.namespace = self.model._meta.model_name + list_url_name = 'list' + create_url_name = 'create' + detail_url_name = 'detail' + update_url_name = 'update' + delete_url_name = 'delete' -class ComissaoDetailView(BaseMixin, DetailView): - model = Comissao + def with_namespace(url_name): + return '%s:%s' % (self.namespace, url_name) + class BaseMixin(object): -class ComissaoCreateView(BaseMixin, CreateView): - model = Comissao - success_url = reverse_lazy('comissao_list') - title = _('Nova Comissão') + @property + def title(self): + return self.get_object() + help_url = '/comissoes/ajuda' # FIXME -class ComissaoUpdateView(BaseMixin, FormMessagesMixin, UpdateView): - model = Comissao - form_class = ComissaoForm - success_url = reverse_lazy('comissao_list') - form_invalid_message = u"Something went wrong, post was not saved" + class CrudListView(BaseMixin, ListView): + model = self.model + title = model._meta.verbose_name_plural - def get_form_valid_message(self): - return u"{0} updated successfully!".format(self.object) + class CrudCreateView(BaseMixin, CreateView): + model = self.model + form_class = ComissaoForm + title = self.create_title + def get_success_url(self): + return reverse(with_namespace(detail_url_name), + args=(self.object.id,)) -class ComissaoDeleteView(BaseMixin, DeleteView): - model = Comissao - success_url = reverse_lazy('comissao_list') + class CrudDetailView(BaseMixin, DetailView): + model = self.model + + class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView): + model = self.model + form_class = ComissaoForm + success_url = reverse_lazy('comissao_list') + form_invalid_message = u"Something went wrong, post was not saved" + + def get_form_valid_message(self): + return u"{0} updated successfully!".format(self.object) + + class CrudDeleteView(BaseMixin, DeleteView): + model = self.model + success_url = reverse_lazy('comissao_list') + + self.CrudListView = CrudListView + self.CrudCreateView = CrudCreateView + self.CrudDetailView = CrudDetailView + self.CrudUpdateView = CrudUpdateView + self.CrudDeleteView = CrudDeleteView + + self.urls = [ + url(r'^$', + CrudListView.as_view(), name=list_url_name), + url(r'^create$', + CrudCreateView.as_view(), name=create_url_name), + url(r'^(?P\d+)$', + CrudDetailView.as_view(), name=detail_url_name), + url(r'^(?P\d+)/edit$', + CrudUpdateView.as_view(), name=update_url_name), + url(r'^(?P\d+)/delete$', + CrudDeleteView.as_view(), name=delete_url_name), + ], 'comissao', 'comissao' + + +comissao_crud = Crud(ComissaoForm, _('Nova Comissão')) diff --git a/sapl/urls.py b/sapl/urls.py index bc974e661..915cccf72 100644 --- a/sapl/urls.py +++ b/sapl/urls.py @@ -17,10 +17,12 @@ from django.conf.urls import include, url from django.contrib import admin from django.views.generic.base import TemplateView +from comissoes.views import comissao_crud + urlpatterns = [ url(r'^$', TemplateView.as_view(template_name='index.html')), url(r'^admin/', include(admin.site.urls)), - url(r'^comissoes/', include('comissoes.urls')), + url(r'^comissoes/', include(comissao_crud.urls)), url(r'^sessao/', include('sessao.urls')), ]