Browse Source

Reduz mixins no crud

pull/255/head
Marcio Mazza 9 years ago
parent
commit
080f3430b7
  1. 79
      crud/base.py

79
crud/base.py

@ -1,6 +1,7 @@
from braces.views import FormMessagesMixin from braces.views import FormMessagesMixin
from django.conf.urls import url from django.conf.urls import url
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.decorators import classonlymethod
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import (CreateView, DeleteView, DetailView, ListView, from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
UpdateView) UpdateView)
@ -96,7 +97,7 @@ class BaseMixin(CrispyLayoutFormMixin):
return self.model._meta.verbose_name_plural return self.model._meta.verbose_name_plural
class ListMixin(): class CrudListView(ListView):
paginate_by = 10 paginate_by = 10
no_entries_msg = _('Nenhum registro encontrado.') no_entries_msg = _('Nenhum registro encontrado.')
@ -107,7 +108,7 @@ class ListMixin():
for i, name in enumerate(self.list_field_names)] for i, name in enumerate(self.list_field_names)]
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(ListMixin, self).get_context_data(**kwargs) context = super(CrudListView, self).get_context_data(**kwargs)
context.setdefault('title', self.verbose_name_plural) context.setdefault('title', self.verbose_name_plural)
# pagination # pagination
@ -128,7 +129,7 @@ class ListMixin():
return context return context
class CreateMixin(FormMessagesMixin): class CrudCreateView(FormMessagesMixin, CreateView):
form_valid_message, form_invalid_message = FORM_MESSAGES['create'] form_valid_message, form_invalid_message = FORM_MESSAGES['create']
@ -142,14 +143,10 @@ class CreateMixin(FormMessagesMixin):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs.setdefault('title', _('Adicionar %(verbose_name)s') % { kwargs.setdefault('title', _('Adicionar %(verbose_name)s') % {
'verbose_name': self.verbose_name}) 'verbose_name': self.verbose_name})
return super(CreateMixin, self).get_context_data(**kwargs) return super(CrudCreateView, self).get_context_data(**kwargs)
class DetailMixin(): class CrudUpdateView(FormMessagesMixin, UpdateView):
pass
class UpdateMixin(FormMessagesMixin):
form_valid_message, form_invalid_message = FORM_MESSAGES['update'] form_valid_message, form_invalid_message = FORM_MESSAGES['update']
@ -161,7 +158,7 @@ class UpdateMixin(FormMessagesMixin):
return self.detail_url return self.detail_url
class DeleteMixin(FormMessagesMixin): class CrudDeleteView(FormMessagesMixin, DeleteView):
form_valid_message, form_invalid_message = FORM_MESSAGES['delete'] form_valid_message, form_invalid_message = FORM_MESSAGES['delete']
@ -174,36 +171,32 @@ class DeleteMixin(FormMessagesMixin):
class Crud: class Crud:
BaseMixin = BaseMixin
def __init__(self, model, help_path, ListView = CrudListView
base_mixin=BaseMixin, CreateView = CrudCreateView
list_mixin=ListMixin, DetailView = DetailView
create_mixin=CreateMixin, UpdateView = CrudUpdateView
detail_mixin=DetailMixin, DeleteView = CrudDeleteView
update_mixin=UpdateMixin, help_path = ''
delete_mixin=DeleteMixin):
@classonlymethod
class CrudMixin(base_mixin): def get_urls(cls):
pass
CrudMixin.model = model def _from(view):
CrudMixin.help_path = help_path return type(view.__name__, (cls.BaseMixin, view,), {
'model': cls.model,
class CrudListView(CrudMixin, list_mixin, ListView): 'help_path': cls.help_path})
pass
CrudListView = _from(cls.ListView)
class CrudCreateView(CrudMixin, create_mixin, CreateView): CrudCreateView = _from(cls.CreateView)
pass CrudDetailView = _from(cls.DetailView)
CrudUpdateView = _from(cls.UpdateView)
class CrudDetailView(CrudMixin, detail_mixin, DetailView): CrudDeleteView = _from(cls.DeleteView)
pass
import ipdb
class CrudUpdateView(CrudMixin, update_mixin, UpdateView): ipdb.set_trace() # flake8: noqa #################O
pass
urlpatterns = [
class CrudDeleteView(CrudMixin, delete_mixin, DeleteView):
pass
self.urlpatterns = [
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'),
@ -213,8 +206,4 @@ class Crud:
CrudDeleteView.as_view(), name='delete'), CrudDeleteView.as_view(), name='delete'),
] ]
self.namespace = CrudMixin().namespace return urlpatterns, _from(object)().namespace
self.urls = self.urlpatterns, self.namespace
# FIXME Refatorar código que precisa desse atributo e remover
self.CrudDetailView = CrudDetailView

Loading…
Cancel
Save