Browse Source

Refactor crud build code to a simple function

pull/6/head
Marcio Mazza 10 years ago
parent
commit
7b5adc3f9b
  1. 4
      comissoes/views.py
  2. 146
      sapl/crud.py

4
comissoes/views.py

@ -1,5 +1,5 @@
from .forms import ComissaoForm from .forms import ComissaoForm
from sapl.crud import Crud from sapl.crud import build_crud
comissao_crud = Crud(ComissaoForm) comissao_crud = build_crud(ComissaoForm)

146
sapl/crud.py

@ -7,100 +7,100 @@ from django.views.generic import (
class Crud(object): class Crud(object):
pass
def __init__(self, model_form):
self.model_form = model_form def build_crud(model_form):
crud = Crud()
crud.model = model_form._meta.model
crud.namespace = crud.model._meta.model_name
self.model = model_form._meta.model def in_namespace(url_name):
return '%s:%s' % (crud.namespace, url_name)
# urls names def make_form_invalid_message(msg):
self.namespace = self.model._meta.model_name return '%s %s' % (_('Formulário inválido.'), msg)
def in_namespace(url_name): class BaseMixin(object):
return '%s:%s' % (self.namespace, url_name) model = crud.model
def make_form_invalid_message(msg): verbose_name = crud.model._meta.verbose_name
return '%s %s' % (_('Formulário inválido.'), msg) verbose_name_plural = crud.model._meta.verbose_name_plural
class BaseMixin(object): list_url = reverse_lazy(in_namespace('list'))
model = self.model create_url = reverse_lazy(in_namespace('create'))
help_url = '/comissoes/ajuda' # FIXME
verbose_name = self.model._meta.verbose_name def get_url_for_this_object(self, url_name):
verbose_name_plural = self.model._meta.verbose_name_plural return reverse(in_namespace(url_name), args=(self.object.id,))
list_url = reverse_lazy(in_namespace('list')) @property
create_url = reverse_lazy(in_namespace('create')) def detail_url(self):
help_url = '/comissoes/ajuda' # FIXME return self.get_url_for_this_object('detail')
def get_url_for_this_object(self, url_name): @property
return reverse(in_namespace(url_name), args=(self.object.id,)) def update_url(self):
return self.get_url_for_this_object('update')
@property @property
def detail_url(self): def delete_url(self):
return self.get_url_for_this_object('detail') return self.get_url_for_this_object('delete')
@property class CrudListView(BaseMixin, ListView):
def update_url(self): title = BaseMixin.verbose_name_plural
return self.get_url_for_this_object('update')
@property class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView):
def delete_url(self): form_class = model_form
return self.get_url_for_this_object('delete') 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.'))
class CrudListView(BaseMixin, ListView): def get_success_url(self):
title = BaseMixin.verbose_name_plural return self.detail_url
class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): class CrudDetailView(BaseMixin, DetailView):
form_class = model_form
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.'))
def get_success_url(self): @property
return self.detail_url def title(self):
return self.get_object()
class CrudDetailView(BaseMixin, DetailView): class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView):
form_class = model_form
form_valid_message = _('Registro alterado com sucesso!')
form_invalid_message = make_form_invalid_message(
_('Suas alterações não foram salvas.'))
@property @property
def title(self): def title(self):
return self.get_object() return self.get_object()
class CrudUpdateView(BaseMixin, FormMessagesMixin, UpdateView): def get_success_url(self):
form_class = model_form return self.detail_url
form_valid_message = _('Registro alterado com sucesso!')
form_invalid_message = make_form_invalid_message(
_('Suas alterações não foram salvas.'))
@property class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView):
def title(self): form_valid_message = _('Registro excluído com sucesso!')
return self.get_object() form_invalid_message = make_form_invalid_message(
_('O registro não foi excluído.'))
def get_success_url(self): def get_success_url(self):
return self.detail_url return self.list_url
class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView): crud.CrudListView = CrudListView
form_valid_message = _('Registro excluído com sucesso!') crud.CrudCreateView = CrudCreateView
form_invalid_message = make_form_invalid_message( crud.CrudDetailView = CrudDetailView
_('O registro não foi excluído.')) crud.CrudUpdateView = CrudUpdateView
crud.CrudDeleteView = CrudDeleteView
def get_success_url(self): crud.urls = [
return self.list_url url(r'^$', CrudListView.as_view(), name='list'),
url(r'^create$', CrudCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/edit$',
CrudUpdateView.as_view(), name='update'),
url(r'^(?P<pk>\d+)/delete$',
CrudDeleteView.as_view(), name='delete'),
], crud.namespace, crud.namespace
self.CrudListView = CrudListView return crud
self.CrudCreateView = CrudCreateView
self.CrudDetailView = CrudDetailView
self.CrudUpdateView = CrudUpdateView
self.CrudDeleteView = CrudDeleteView
self.urls = [
url(r'^$', CrudListView.as_view(), name='list'),
url(r'^create$', CrudCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/edit$',
CrudUpdateView.as_view(), name='update'),
url(r'^(?P<pk>\d+)/delete$',
CrudDeleteView.as_view(), name='delete'),
], self.namespace, self.namespace

Loading…
Cancel
Save