From e8afda18532a33480058dd277ab73b8a764ae356 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Wed, 22 Jul 2015 15:46:09 -0300 Subject: [PATCH] Refactor crud --- sapl/crud.py | 31 +++++++++++++------------- templates/comissoes/comissao_list.html | 4 +++- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/sapl/crud.py b/sapl/crud.py index 5e91db62f..b72e02f96 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -24,18 +24,15 @@ class Crud(object): return '%s %s' % (_('Formulário inválido.'), msg) class BaseMixin(object): + model = self.model - @property - def title(self): - return self.get_object() + verbose_name = self.model._meta.verbose_name + verbose_name_plural = self.model._meta.verbose_name_plural list_url = reverse_lazy(in_namespace('list')) create_url = reverse_lazy(in_namespace('create')) help_url = '/comissoes/ajuda' # FIXME - create_title = _('Adicionar %(model_name)s') % { - 'model_name': self.model._meta.verbose_name} - def get_url_for_this_object(self, url_name): return reverse(in_namespace(url_name), args=(self.object.id,)) @@ -45,20 +42,19 @@ class Crud(object): @property def update_url(self): - return self.get_url_for_this_object('detail') + return self.get_url_for_this_object('update') @property def delete_url(self): - return self.get_url_for_this_object('detail') + return self.get_url_for_this_object('delete') class CrudListView(BaseMixin, ListView): - model = self.model - title = model._meta.verbose_name_plural + title = BaseMixin.verbose_name_plural class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): - model = self.model form_class = model_form - title = BaseMixin.create_title + title = _('Adicionar %(verbose_name)s') % { + 'verbose_name': BaseMixin.verbose_name} form_valid_message = _('Registro criado com sucesso!') form_invalid_message = make_form_invalid_message( _('O registro não foi criado.')) @@ -67,20 +63,25 @@ class Crud(object): return self.detail_url class CrudDetailView(BaseMixin, DetailView): - model = self.model + + @property + def title(self): + return self.get_object() class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView): - model = self.model form_class = model_form form_valid_message = _('Mudanças salvas com sucesso!') form_invalid_message = make_form_invalid_message( _('Suas mudanças não foram salvas.')) + @property + def title(self): + return self.get_object() + def get_success_url(self): return self.detail_url class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView): - model = self.model form_valid_message = _('Registro excluído com sucesso!') form_invalid_message = make_form_invalid_message( _('O registro não foi excluído.')) diff --git a/templates/comissoes/comissao_list.html b/templates/comissoes/comissao_list.html index 056686fbc..88a8a7494 100644 --- a/templates/comissoes/comissao_list.html +++ b/templates/comissoes/comissao_list.html @@ -5,7 +5,9 @@ {# FIXME is this the best markup to use? #}