Browse Source

Simplifica nomes dos mixins no crud

pull/255/head
Marcio Mazza 9 years ago
parent
commit
67ac579341
  1. 4
      compilacao/views.py
  2. 30
      crud/base.py
  3. 6
      crud/tests/stub_app/views.py
  4. 8
      crud/tests/test_base.py

4
compilacao/views.py

@ -27,7 +27,7 @@ from compilacao.models import (Dispositivo, Nota,
TextoArticulado, TipoDispositivo, TipoNota, TextoArticulado, TipoDispositivo, TipoNota,
TipoPublicacao, TipoTextoArticulado, TipoVide, TipoPublicacao, TipoTextoArticulado, TipoVide,
VeiculoPublicacao, Vide) VeiculoPublicacao, Vide)
from crud.base import Crud, CrudListMixin, make_pagination from crud.base import Crud, ListMixin, make_pagination
DISPOSITIVO_SELECT_RELATED = ( DISPOSITIVO_SELECT_RELATED = (
'tipo_dispositivo', 'tipo_dispositivo',
@ -1667,7 +1667,7 @@ class PublicacaoListView(ListView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(PublicacaoListView, self).get_context_data(**kwargs) context = super(PublicacaoListView, self).get_context_data(**kwargs)
context['NO_ENTRIES_MSG'] = CrudListMixin.no_entries_msg context['NO_ENTRIES_MSG'] = ListMixin.no_entries_msg
return context return context

30
crud/base.py

@ -39,7 +39,7 @@ def make_pagination(index, num_pages):
return head + [None] + tail return head + [None] + tail
class BaseCrudMixin(CrispyLayoutFormMixin): class BaseMixin(CrispyLayoutFormMixin):
@property @property
def namespace(self): def namespace(self):
@ -69,7 +69,7 @@ class BaseCrudMixin(CrispyLayoutFormMixin):
return self.resolve_url('delete', args=(self.object.id,)) return self.resolve_url('delete', args=(self.object.id,))
def get_template_names(self): def get_template_names(self):
names = super(BaseCrudMixin, self).get_template_names() names = super(BaseMixin, self).get_template_names()
names.append("crud/%s.html" % names.append("crud/%s.html" %
self.template_name_suffix.lstrip('_')) self.template_name_suffix.lstrip('_'))
return names return names
@ -83,7 +83,7 @@ class BaseCrudMixin(CrispyLayoutFormMixin):
return self.model._meta.verbose_name_plural return self.model._meta.verbose_name_plural
class CrudListMixin(): class ListMixin():
paginate_by = 10 paginate_by = 10
no_entries_msg = _('Nenhum registro encontrado.') no_entries_msg = _('Nenhum registro encontrado.')
@ -94,7 +94,7 @@ class CrudListMixin():
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(CrudListMixin, self).get_context_data(**kwargs) context = super(ListMixin, self).get_context_data(**kwargs)
context.setdefault('title', self.verbose_name_plural) context.setdefault('title', self.verbose_name_plural)
# pagination # pagination
@ -119,7 +119,7 @@ def make_form_invalid_message(msg):
return '%s %s' % (_('Formulário inválido.'), msg) return '%s %s' % (_('Formulário inválido.'), msg)
class CrudCreateMixin(FormMessagesMixin): class CreateMixin(FormMessagesMixin):
form_valid_message = _('Registro criado com sucesso!') form_valid_message = _('Registro criado com sucesso!')
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
@ -135,14 +135,14 @@ class CrudCreateMixin(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(CrudCreateMixin, self).get_context_data(**kwargs) return super(CreateMixin, self).get_context_data(**kwargs)
class CrudDetailMixin(): class DetailMixin():
pass pass
class CrudUpdateMixin(FormMessagesMixin): class UpdateMixin(FormMessagesMixin):
form_valid_message = _('Registro alterado com sucesso!') form_valid_message = _('Registro alterado com sucesso!')
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
@ -156,7 +156,7 @@ class CrudUpdateMixin(FormMessagesMixin):
return self.detail_url return self.detail_url
class CrudDeleteMixin(FormMessagesMixin): class DeleteMixin(FormMessagesMixin):
form_valid_message = _('Registro excluído com sucesso!') form_valid_message = _('Registro excluído com sucesso!')
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
@ -173,12 +173,12 @@ class CrudDeleteMixin(FormMessagesMixin):
class Crud: class Crud:
def __init__(self, model, help_path, def __init__(self, model, help_path,
base_mixin=BaseCrudMixin, base_mixin=BaseMixin,
list_mixin=CrudListMixin, list_mixin=ListMixin,
create_mixin=CrudCreateMixin, create_mixin=CreateMixin,
detail_mixin=CrudDetailMixin, detail_mixin=DetailMixin,
update_mixin=CrudUpdateMixin, update_mixin=UpdateMixin,
delete_mixin=CrudDeleteMixin): delete_mixin=DeleteMixin):
class CrudMixin(base_mixin): class CrudMixin(base_mixin):
pass pass

6
crud/tests/stub_app/views.py

@ -1,13 +1,13 @@
from crud.base import Crud, CrudListMixin from crud.base import Crud, ListMixin
from .models import Country from .models import Country
class CountryCrudListMixin(CrudListMixin): class CountryListMixin(ListMixin):
paginate_by = 10 paginate_by = 10
country_crud = Crud( country_crud = Crud(
Country, Country,
'help_path', 'help_path',
list_mixin=CountryCrudListMixin) list_mixin=CountryListMixin)

8
crud/tests/test_base.py

@ -2,11 +2,11 @@ import pytest
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from model_mommy import mommy from model_mommy import mommy
from crud.base import (CrispyLayoutFormMixin, CrudListMixin, from_to, from crud.base import (CrispyLayoutFormMixin, ListMixin, from_to,
get_field_display, make_pagination) get_field_display, make_pagination)
from .stub_app.models import Continent, Country from .stub_app.models import Continent, Country
from .stub_app.views import CountryCrudListMixin from .stub_app.views import CountryListMixin
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -151,7 +151,7 @@ def assert_h1(res, title):
assert res.html.find('main').find('h1').text.strip() == title assert res.html.find('main').find('h1').text.strip() == title
NO_ENTRIES_MSG = str(CrudListMixin.no_entries_msg) # "unlazy" NO_ENTRIES_MSG = str(ListMixin.no_entries_msg) # "unlazy"
def assert_on_list_page(res): def assert_on_list_page(res):
@ -196,7 +196,7 @@ def test_flux_list_paginate_detail(
population=population, population=population,
is_cold=is_cold) is_cold=is_cold)
CountryCrudListMixin.paginate_by = page_size CountryListMixin.paginate_by = page_size
res = app.get('/countries/') res = app.get('/countries/')

Loading…
Cancel
Save