Browse Source

Merge pull request #258 from interlegis/256-parar-de-usar-namespaces-em-urls

fix #256 parar de usar namespaces em urls
pull/269/head
Marcio Mazza 9 years ago
parent
commit
1d27dd22d0
  1. 6
      comissoes/urls.py
  2. 53
      crud/base.py
  3. 10
      crud/tests/test_base.py
  4. 5
      materia/urls.py
  5. 6
      norma/urls.py
  6. 6
      sessao/urls.py

6
comissoes/urls.py

@ -7,9 +7,7 @@ from comissoes.views import (CadastrarComissaoView, CargoCrud, ComissaoCrud,
PeriodoComposicaoCrud, ReunioesView, PeriodoComposicaoCrud, ReunioesView,
TipoComissaoCrud) TipoComissaoCrud)
comissao_url_patterns, namespace = ComissaoCrud.get_urls() comissao_url_patterns = ComissaoCrud.get_urls() + [
comissao_url_patterns = comissao_url_patterns + [
url(r'^(?P<pk>\d+)/composicao$', url(r'^(?P<pk>\d+)/composicao$',
ComposicaoView.as_view(), name='composicao'), ComposicaoView.as_view(), name='composicao'),
url(r'^(?P<pk>\d+)/materias-em-tramitacao$', url(r'^(?P<pk>\d+)/materias-em-tramitacao$',
@ -29,7 +27,7 @@ comissao_url_patterns = comissao_url_patterns + [
] ]
urlpatterns = [ urlpatterns = [
url(r'^comissoes/', include(comissao_url_patterns, namespace)), url(r'^comissoes/', include(comissao_url_patterns)),
url(r'^sistema/comissoes/cargo/', include(CargoCrud.get_urls())), url(r'^sistema/comissoes/cargo/', include(CargoCrud.get_urls())),
url(r'^sistema/comissoes/periodo-composicao/', url(r'^sistema/comissoes/periodo-composicao/',

53
crud/base.py

@ -8,15 +8,18 @@ from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
from crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display from crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display
LIST, CREATE, DETAIL, UPDATE, DELETE = \
'list', 'create', 'detail', 'update', 'delete'
def _form_invalid_message(msg): def _form_invalid_message(msg):
return '%s %s' % (_('Formulário inválido.'), msg) return '%s %s' % (_('Formulário inválido.'), msg)
FORM_MESSAGES = {'create': (_('Registro criado com sucesso!'), FORM_MESSAGES = {CREATE: (_('Registro criado com sucesso!'),
_('O registro não foi criado.')), _('O registro não foi criado.')),
'update': (_('Registro alterado com sucesso!'), UPDATE: (_('Registro alterado com sucesso!'),
_('Suas alterações não foram salvas.')), _('Suas alterações não foram salvas.')),
'delete': (_('Registro excluído com sucesso!'), DELETE: (_('Registro excluído com sucesso!'),
_('O registro não foi excluído.'))} _('O registro não foi excluído.'))}
FORM_MESSAGES = {k: (a, _form_invalid_message(b)) FORM_MESSAGES = {k: (a, _form_invalid_message(b))
for k, (a, b) in FORM_MESSAGES.items()} for k, (a, b) in FORM_MESSAGES.items()}
@ -55,32 +58,32 @@ def make_pagination(index, num_pages):
class BaseMixin(CrispyLayoutFormMixin): class BaseMixin(CrispyLayoutFormMixin):
@property @classmethod
def namespace(self): def url_name(cls, suffix):
return self.model._meta.model_name return '%s_%s' % (cls.model._meta.model_name, suffix)
def resolve_url(self, url_name, args=None): def resolve_url(self, suffix, args=None):
return reverse('%s:%s' % (self.namespace, url_name), args=args) return reverse(self.url_name(suffix), args=args)
@property @property
def list_url(self): def list_url(self):
return self.resolve_url('list') return self.resolve_url(LIST)
@property @property
def create_url(self): def create_url(self):
return self.resolve_url('create') return self.resolve_url(CREATE)
@property @property
def detail_url(self): def detail_url(self):
return self.resolve_url('detail', args=(self.object.id,)) return self.resolve_url(DETAIL, args=(self.object.id,))
@property @property
def update_url(self): def update_url(self):
return self.resolve_url('update', args=(self.object.id,)) return self.resolve_url(UPDATE, args=(self.object.id,))
@property @property
def delete_url(self): def delete_url(self):
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(BaseMixin, self).get_template_names() names = super(BaseMixin, self).get_template_names()
@ -131,7 +134,7 @@ class CrudListView(ListView):
class CrudCreateView(FormMessagesMixin, CreateView): class CrudCreateView(FormMessagesMixin, CreateView):
form_valid_message, form_invalid_message = FORM_MESSAGES['create'] form_valid_message, form_invalid_message = FORM_MESSAGES[CREATE]
@property @property
def cancel_url(self): def cancel_url(self):
@ -148,7 +151,7 @@ class CrudCreateView(FormMessagesMixin, CreateView):
class CrudUpdateView(FormMessagesMixin, UpdateView): class CrudUpdateView(FormMessagesMixin, UpdateView):
form_valid_message, form_invalid_message = FORM_MESSAGES['update'] form_valid_message, form_invalid_message = FORM_MESSAGES[UPDATE]
@property @property
def cancel_url(self): def cancel_url(self):
@ -160,7 +163,7 @@ class CrudUpdateView(FormMessagesMixin, UpdateView):
class CrudDeleteView(FormMessagesMixin, DeleteView): class CrudDeleteView(FormMessagesMixin, DeleteView):
form_valid_message, form_invalid_message = FORM_MESSAGES['delete'] form_valid_message, form_invalid_message = FORM_MESSAGES[DELETE]
@property @property
def cancel_url(self): def cancel_url(self):
@ -195,17 +198,13 @@ class Crud:
CrudUpdateView = _add_base(cls.UpdateView) CrudUpdateView = _add_base(cls.UpdateView)
CrudDeleteView = _add_base(cls.DeleteView) CrudDeleteView = _add_base(cls.DeleteView)
urlpatterns = [ return [url(regex, view.as_view(), name=view.url_name(suffix))
url(r'^$', CrudListView.as_view(), name='list'), for regex, view, suffix in [
url(r'^create$', CrudCreateView.as_view(), name='create'), (r'^$', CrudListView, LIST),
url(r'^(?P<pk>\d+)$', CrudDetailView.as_view(), name='detail'), (r'^create$', CrudCreateView, CREATE),
url(r'^(?P<pk>\d+)/edit$', (r'^(?P<pk>\d+)$', CrudDetailView, DETAIL),
CrudUpdateView.as_view(), name='update'), (r'^(?P<pk>\d+)/edit$', CrudUpdateView, UPDATE),
url(r'^(?P<pk>\d+)/delete$', (r'^(?P<pk>\d+)/delete$', CrudDeleteView, DELETE), ]]
CrudDeleteView.as_view(), name='delete'),
]
return urlpatterns, _add_base(object)().namespace
@classonlymethod @classonlymethod
def build(cls, _model, _help_path): def build(cls, _model, _help_path):

10
crud/tests/test_base.py

@ -140,11 +140,11 @@ def test_layout_detail_fieldsets(monkeypatch):
def test_reverse(): def test_reverse():
assert '/countries/' == reverse('country:list') assert '/countries/' == reverse('country_list')
assert '/countries/create' == reverse('country:create') assert '/countries/create' == reverse('country_create')
assert '/countries/2' == reverse('country:detail', args=(2,)) assert '/countries/2' == reverse('country_detail', args=(2,))
assert '/countries/2/edit' == reverse('country:update', args=(2,)) assert '/countries/2/edit' == reverse('country_update', args=(2,))
assert '/countries/2/delete' == reverse('country:delete', args=(2,)) assert '/countries/2/delete' == reverse('country_delete', args=(2,))
def assert_h1(res, title): def assert_h1(res, title):

5
materia/urls.py

@ -22,11 +22,8 @@ from materia.views import (AcompanhamentoConfirmarView,
TramitacaoEditView, TramitacaoView, TramitacaoEditView, TramitacaoView,
UnidadeTramitacaoCrud) UnidadeTramitacaoCrud)
materia_legislativa_patterns, namespace = MateriaLegislativaCrud.get_urls()
urlpatterns = [ urlpatterns = [
url(r'^materia/', include(materia_legislativa_patterns, namespace)), url(r'^materia/', include(MateriaLegislativaCrud.get_urls())),
url(r'^materia/(?P<pk>[0-9]+)/ta$', url(r'^materia/(?P<pk>[0-9]+)/ta$',

6
norma/urls.py

@ -14,15 +14,13 @@ from norma.views import (AssuntoNormaCrud, NormaEditView, NormaIncluirView,
# o código abaixo: # o código abaixo:
# url(r'^norma/(?P<pk>[0-9]+)/ta$', NormaTaView.as_view(), name='ta') # url(r'^norma/(?P<pk>[0-9]+)/ta$', NormaTaView.as_view(), name='ta')
# bem como a classe NormaTaView que está em norma.views # bem como a classe NormaTaView que está em norma.views
norma_url_patterns, namespace = NormaTemporarioCrud.get_urls() norma_url_patterns = NormaTemporarioCrud.get_urls() + [
norma_url_patterns += [
url(r'^norma/(?P<pk>[0-9]+)/ta$', url(r'^norma/(?P<pk>[0-9]+)/ta$',
NormaTaView.as_view(), name='ta') NormaTaView.as_view(), name='ta')
] ]
urlpatterns = [ urlpatterns = [
url(r'^norma/', include(norma_url_patterns, namespace)), url(r'^norma/', include(norma_url_patterns)),
url(r'^sistema/norma/tipo/', include(TipoNormaCrud.get_urls())), url(r'^sistema/norma/tipo/', include(TipoNormaCrud.get_urls())),
url(r'^sistema/norma/assunto/', include(AssuntoNormaCrud.get_urls())), url(r'^sistema/norma/assunto/', include(AssuntoNormaCrud.get_urls())),

6
sessao/urls.py

@ -21,9 +21,7 @@ from sessao.views import (EditExpedienteOrdemDiaView, EditMateriaOrdemDiaView,
VotacaoNominalExpedienteView, VotacaoNominalView, VotacaoNominalExpedienteView, VotacaoNominalView,
VotacaoView) VotacaoView)
urlpatterns_sessao, namespace = SessaoCrud.get_urls() urlpatterns_sessao = SessaoCrud.get_urls() + [
urlpatterns_sessao = urlpatterns_sessao + [
url(r'^(?P<pk>\d+)/expediente$', url(r'^(?P<pk>\d+)/expediente$',
ExpedienteView.as_view(), name='expediente'), ExpedienteView.as_view(), name='expediente'),
url(r'^(?P<pk>\d+)/presenca$', url(r'^(?P<pk>\d+)/presenca$',
@ -105,7 +103,7 @@ sessao_rest = [
] ]
urlpatterns = [ urlpatterns = [
url(r'^sessao/', include(urlpatterns_sessao, namespace)), url(r'^sessao/', include(urlpatterns_sessao)),
url(r'^media/(?P<path>.*)$', serve, url(r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT}), {'document_root': settings.MEDIA_ROOT}),
url(r'^rest/', include(sessao_rest)), url(r'^rest/', include(sessao_rest)),

Loading…
Cancel
Save