unify editor url creation and child model creation

This commit is contained in:
Laura Klünder 2017-05-21 14:43:16 +02:00
parent f35d548bed
commit bafe094785
2 changed files with 43 additions and 60 deletions

View file

@ -1,12 +1,21 @@
from django.apps import apps
from django.conf.urls import url
from c3nav.editor.views import edit, list_objects, main_index, section_detail, space_detail
def add_editor_urls(name, model, parent_name_plural=None, parent_name=None, with_list=True, explicit_edit=False):
prefix = ('' if parent_name is None else parent_name_plural+r'/(?P<'+parent_name+'>[0-9]+)/')+name
name_prefix = 'editor.'+name+'.'
kwargs = {'model': model, 'explicit_edit': explicit_edit}
def add_editor_urls(model_name, parent_model_name=None, with_list=True, explicit_edit=False):
model = apps.get_model('mapdata', model_name)
model_name_plural = model._meta.default_related_name
if parent_model_name:
parent_model = apps.get_model('mapdata', parent_model_name)
parent_model_name_plural = parent_model._meta.default_related_name
prefix = (parent_model_name_plural+r'/(?P<'+parent_model_name.lower()+'>[0-9]+)/')+model_name_plural
else:
prefix = model_name_plural
name_prefix = 'editor.'+model_name_plural+'.'
kwargs = {'model': model_name, 'explicit_edit': explicit_edit}
explicit_edit = r'edit' if explicit_edit else ''
result = []
@ -24,14 +33,14 @@ urlpatterns = [
url(r'^sections/(?P<pk>[0-9]+)/$', section_detail, name='editor.sections.detail'),
url(r'^sections/(?P<section>[0-9]+)/spaces/(?P<pk>[0-9]+)/$', space_detail, name='editor.spaces.detail'),
]
urlpatterns.extend(add_editor_urls('sections', 'Section', with_list=False, explicit_edit=True))
urlpatterns.extend(add_editor_urls('locationgroups', 'LocationGroup'))
urlpatterns.extend(add_editor_urls('buildings', 'Building', 'sections', 'section'))
urlpatterns.extend(add_editor_urls('spaces', 'Space', 'sections', 'section', explicit_edit=True))
urlpatterns.extend(add_editor_urls('doors', 'Door', 'sections', 'section'))
urlpatterns.extend(add_editor_urls('holes', 'Hole', 'sections', 'section'))
urlpatterns.extend(add_editor_urls('areas', 'Area', 'spaces', 'space'))
urlpatterns.extend(add_editor_urls('stairs', 'Stair', 'spaces', 'space'))
urlpatterns.extend(add_editor_urls('obstacles', 'Obstacle', 'spaces', 'space'))
urlpatterns.extend(add_editor_urls('lineobstacles', 'LineObstacle', 'spaces', 'space'))
urlpatterns.extend(add_editor_urls('points', 'Point', 'spaces', 'space'))
urlpatterns.extend(add_editor_urls('Section', with_list=False, explicit_edit=True))
urlpatterns.extend(add_editor_urls('LocationGroup'))
urlpatterns.extend(add_editor_urls('Building', 'Section'))
urlpatterns.extend(add_editor_urls('Space', 'Section', explicit_edit=True))
urlpatterns.extend(add_editor_urls('Door', 'Section'))
urlpatterns.extend(add_editor_urls('Hole', 'Section'))
urlpatterns.extend(add_editor_urls('Area', 'Space'))
urlpatterns.extend(add_editor_urls('Stair', 'Space'))
urlpatterns.extend(add_editor_urls('Obstacle', 'Space'))
urlpatterns.extend(add_editor_urls('LineObstacle', 'Space'))
urlpatterns.extend(add_editor_urls('Point', 'Space'))

View file

@ -1,5 +1,6 @@
from functools import wraps
from django.apps import apps
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
@ -8,10 +9,8 @@ from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.cache import never_cache
from c3nav.mapdata.models import (Area, Building, Door, Hole, LineObstacle, LocationGroup, Obstacle, Section, Space,
Stair)
from c3nav.mapdata.models import Section, Space
from c3nav.mapdata.models.base import EDITOR_FORM_MODELS
from c3nav.mapdata.models.geometry.space import Point
def sidebar_view(func):
@ -26,14 +25,23 @@ def sidebar_view(func):
return never_cache(with_ajax_check)
def child_model(model_name, kwargs=None, parent=None):
model = apps.get_model('mapdata', model_name)
related_name = model._meta.default_related_name
return {
'title': model._meta.verbose_name_plural,
'url': reverse('editor.'+related_name+'.list', kwargs=kwargs),
'count': None if parent is None else getattr(parent, related_name).count(),
}
@sidebar_view
def main_index(request):
return render(request, 'editor/index.html', {
'sections': Section.objects.all(),
'child_models': [{
'title': LocationGroup._meta.verbose_name_plural,
'url': reverse('editor.locationgroups.list'),
}],
'child_models': [
child_model('LocationGroup'),
],
})
@ -47,23 +55,8 @@ def section_detail(request, pk):
'section_url': 'editor.sections.detail',
'section_as_pk': True,
'child_models': [{
'title': Building._meta.verbose_name_plural,
'url': reverse('editor.buildings.list', kwargs={'section': pk}),
'count': section.buildings.count(),
}, {
'title': Space._meta.verbose_name_plural,
'url': reverse('editor.spaces.list', kwargs={'section': pk}),
'count': section.spaces.count(),
}, {
'title': Door._meta.verbose_name_plural,
'url': reverse('editor.doors.list', kwargs={'section': pk}),
'count': section.doors.count(),
}, {
'title': Hole._meta.verbose_name_plural,
'url': reverse('editor.holes.list', kwargs={'section': pk}),
'count': section.holes.count(),
}],
'child_models': [child_model(model_name, kwargs={'section': pk}, parent=section)
for model_name in ('Building', 'Space', 'Door', 'Hole')],
})
@ -75,27 +68,8 @@ def space_detail(request, section, pk):
'section': space.section,
'space': space,
'child_models': [{
'title': Area._meta.verbose_name_plural,
'url': reverse('editor.areas.list', kwargs={'space': pk}),
'count': space.areas.count(),
}, {
'title': Stair._meta.verbose_name_plural,
'url': reverse('editor.stairs.list', kwargs={'space': pk}),
'count': space.stairs.count(),
}, {
'title': Obstacle._meta.verbose_name_plural,
'url': reverse('editor.obstacles.list', kwargs={'space': pk}),
'count': space.obstacles.count(),
}, {
'title': LineObstacle._meta.verbose_name_plural,
'url': reverse('editor.lineobstacles.list', kwargs={'space': pk}),
'count': space.lineobstacles.count(),
}, {
'title': Point._meta.verbose_name_plural,
'url': reverse('editor.points.list', kwargs={'space': pk}),
'count': space.points.count(),
}],
'child_models': [child_model(model_name, kwargs={'space': pk}, parent=space)
for model_name in ('Area', 'Stair', 'Obstacle', 'LineObstacle', 'Point')],
})