Browse Source

Inicia crud master detail com urls

pull/341/head
Marcio Mazza 9 years ago
committed by LeandroRoberto
parent
commit
d3c08177e9
  1. 36
      crud/base.py
  2. 35
      crud/masterdetail.py
  3. 5
      crud/tests/stub_app/models.py
  4. 5
      crud/tests/stub_app/urls.py
  5. 8
      crud/tests/stub_app/views.py
  6. 14
      crud/tests/test_masterdetail.py

36
crud/base.py

@ -104,6 +104,10 @@ class BaseMixin(CrispyLayoutFormMixin):
class CrudListView(ListView):
@classmethod
def get_url_regex(cls):
return r'^$'
paginate_by = 10
no_entries_msg = _('Nenhum registro encontrado.')
@ -142,6 +146,10 @@ class CrudListView(ListView):
class CrudCreateView(FormMessagesMixin, CreateView):
@classmethod
def get_url_regex(cls):
return r'^create$'
form_valid_message, form_invalid_message = FORM_MESSAGES[CREATE]
@property
@ -157,8 +165,19 @@ class CrudCreateView(FormMessagesMixin, CreateView):
return super(CrudCreateView, self).get_context_data(**kwargs)
class CrudDetailView(DetailView):
@classmethod
def get_url_regex(cls):
return r'^(?P<pk>\d+)$'
class CrudUpdateView(FormMessagesMixin, UpdateView):
@classmethod
def get_url_regex(cls):
return r'^(?P<pk>\d+)/edit$'
form_valid_message, form_invalid_message = FORM_MESSAGES[UPDATE]
@property
@ -171,6 +190,10 @@ class CrudUpdateView(FormMessagesMixin, UpdateView):
class CrudDeleteView(FormMessagesMixin, DeleteView):
@classmethod
def get_url_regex(cls):
return r'^(?P<pk>\d+)/delete$'
form_valid_message, form_invalid_message = FORM_MESSAGES[DELETE]
@property
@ -185,7 +208,7 @@ class Crud:
BaseMixin = BaseMixin
ListView = CrudListView
CreateView = CrudCreateView
DetailView = DetailView
DetailView = CrudDetailView
UpdateView = CrudUpdateView
DeleteView = CrudDeleteView
help_path = ''
@ -208,11 +231,12 @@ class Crud:
return [url(regex, view.as_view(), name=view.url_name(suffix))
for regex, view, suffix in [
(r'^$', CrudListView, LIST),
(r'^create$', CrudCreateView, CREATE),
(r'^(?P<pk>\d+)$', CrudDetailView, DETAIL),
(r'^(?P<pk>\d+)/edit$', CrudUpdateView, UPDATE),
(r'^(?P<pk>\d+)/delete$', CrudDeleteView, DELETE), ]]
(CrudListView.get_url_regex(), CrudListView, LIST),
(CrudCreateView.get_url_regex(), CrudCreateView, CREATE),
(CrudDetailView.get_url_regex(), CrudDetailView, DETAIL),
(CrudUpdateView.get_url_regex(), CrudUpdateView, UPDATE),
(CrudDeleteView.get_url_regex(), CrudDeleteView, DELETE),
]]
@classonlymethod
def build(cls, _model, _help_path):

35
crud/masterdetail.py

@ -0,0 +1,35 @@
from .base import (Crud, CrudCreateView, CrudDeleteView, CrudDetailView,
CrudListView, CrudUpdateView)
class MasterDetailCrud(Crud):
class ListView(CrudListView):
@classmethod
def get_url_regex(cls):
return r'^(?P<pk>\d+)/%s$' % cls.model._meta.model_name
class CreateView(CrudCreateView):
@classmethod
def get_url_regex(cls):
return r'^(?P<pk>\d+)/%s/create$' % cls.model._meta.model_name
class DetailView(CrudDetailView):
@classmethod
def get_url_regex(cls):
return r'^%s/(?P<pk>\d+)$' % cls.model._meta.model_name
class UpdateView(CrudUpdateView):
@classmethod
def get_url_regex(cls):
return r'^%s/(?P<pk>\d+)/edit$' % cls.model._meta.model_name
class DeleteView(CrudDeleteView):
@classmethod
def get_url_regex(cls):
return r'^%s/(?P<pk>\d+)/delete$' % cls.model._meta.model_name

5
crud/tests/stub_app/models.py

@ -21,3 +21,8 @@ class Country(models.Model):
def __str__(self):
return self.name
class City(models.Model):
name = models.CharField(max_length=50)
country = models.ForeignKey(Country)

5
crud/tests/stub_app/urls.py

@ -1,7 +1,8 @@
from django.conf.urls import include, url
from .views import CountryCrud
from .views import CityCrud, CountryCrud
urlpatterns = [
url(r'^country/', include(CountryCrud.get_urls(), 'stub_app')),
url(r'^country/', include(
CountryCrud.get_urls() + CityCrud.get_urls(), 'stub_app')),
]

8
crud/tests/stub_app/views.py

@ -1,6 +1,7 @@
from crud.base import Crud, CrudListView
from crud.masterdetail import MasterDetailCrud
from .models import Country
from .models import City, Country
class CountryCrud(Crud):
@ -9,3 +10,8 @@ class CountryCrud(Crud):
class ListView(CrudListView):
paginate_by = 10
class CityCrud(MasterDetailCrud):
model = City
help_path = 'help_path',

14
crud/tests/test_masterdetail.py

@ -0,0 +1,14 @@
import pytest
from django.core.urlresolvers import reverse
@pytest.mark.parametrize('path_name', [
'/country/1/city stub_app:city_list',
'/country/1/city/create stub_app:city_create',
'/country/city/1 stub_app:city_detail',
'/country/city/1/edit stub_app:city_update',
'/country/city/1/delete stub_app:city_delete',
])
def test_reverse(path_name):
path, name = path_name.split()
assert path == reverse(name, args=(1,))
Loading…
Cancel
Save