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. 36
      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)

36
sapl/crud.py

@ -7,27 +7,25 @@ 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()
self.model = model_form._meta.model crud.model = model_form._meta.model
crud.namespace = crud.model._meta.model_name
# urls names
self.namespace = self.model._meta.model_name
def in_namespace(url_name): def in_namespace(url_name):
return '%s:%s' % (self.namespace, url_name) return '%s:%s' % (crud.namespace, url_name)
def make_form_invalid_message(msg): def make_form_invalid_message(msg):
return '%s %s' % (_('Formulário inválido.'), msg) return '%s %s' % (_('Formulário inválido.'), msg)
class BaseMixin(object): class BaseMixin(object):
model = self.model model = crud.model
verbose_name = self.model._meta.verbose_name verbose_name = crud.model._meta.verbose_name
verbose_name_plural = self.model._meta.verbose_name_plural verbose_name_plural = crud.model._meta.verbose_name_plural
list_url = reverse_lazy(in_namespace('list')) list_url = reverse_lazy(in_namespace('list'))
create_url = reverse_lazy(in_namespace('create')) create_url = reverse_lazy(in_namespace('create'))
@ -89,13 +87,13 @@ class Crud(object):
def get_success_url(self): def get_success_url(self):
return self.list_url return self.list_url
self.CrudListView = CrudListView crud.CrudListView = CrudListView
self.CrudCreateView = CrudCreateView crud.CrudCreateView = CrudCreateView
self.CrudDetailView = CrudDetailView crud.CrudDetailView = CrudDetailView
self.CrudUpdateView = CrudUpdateView crud.CrudUpdateView = CrudUpdateView
self.CrudDeleteView = CrudDeleteView crud.CrudDeleteView = CrudDeleteView
self.urls = [ crud.urls = [
url(r'^$', CrudListView.as_view(), name='list'), url(r'^$', CrudListView.as_view(), name='list'),
url(r'^create$', CrudCreateView.as_view(), name='create'), url(r'^create$', CrudCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'), url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'),
@ -103,4 +101,6 @@ class Crud(object):
CrudUpdateView.as_view(), name='update'), CrudUpdateView.as_view(), name='update'),
url(r'^(?P<pk>\d+)/delete$', url(r'^(?P<pk>\d+)/delete$',
CrudDeleteView.as_view(), name='delete'), CrudDeleteView.as_view(), name='delete'),
], self.namespace, self.namespace ], crud.namespace, crud.namespace
return crud

Loading…
Cancel
Save