From 8c9b78768c196861202ba45ee2ad93a0e238a869 Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Fri, 14 Jun 2013 20:40:19 -0400 Subject: [PATCH] update url routes with nicer .format()ing --- karmaworld/urls.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/karmaworld/urls.py b/karmaworld/urls.py index 817394e..5de44f9 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -21,6 +21,25 @@ from karmaworld.apps.notes.views import PDFView # See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf admin.autodiscover() +# reused named regex capture groups +SLUG = r'(?P<{0}slug>[-A-Za-z0-9_]+)' + +""" +# ex: SLUG.format('') :> (?P[-A-Za-z0-9_]+) +# ex: SLUG.format('school_') :> (?P[-A-Za-z0-9_]+) + + ex: course url + url(r'^' + SLUG.format('school_') + '/' + SLUG.format('') + '/' + CourseDetailView.as_view(), name='course_detail'), + + (?P[^/]+)/(?P[^/]+)/(?P[\d^/]+)$', \ + NoteView.as_view(), name='note_detail_pk'), +""" + +SCHOOL_SLUG = r'(?P[-A-Za-z0-9_]+)' +COURSE_SLUG = r'(?P[-A-Za-z0-9_]+)' +NOTE_SLUG = r'(?P[-A-Za-z0-9_]+)' + # See: https://docs.djangoproject.com/en/dev/topics/http/urls/ urlpatterns = patterns('', ## Administrative URLpatterns @@ -44,15 +63,22 @@ urlpatterns = patterns('', {'document_root': settings.MEDIA_ROOT, }), # VIEW for displaying a single Course - url(r'^(?P[^/]+)/(?P[-A-Za-z0-9_]+)$', \ + url(r'^' + SLUG.format('school_') + '/' + SLUG.format('') + '$', CourseDetailView.as_view(), name='course_detail'), + ## NOTE MODEL + # Valid url cases to the Note page + # a: school/course/id + # b: school/course/id/slug + # c: s../c../slug # note file as id, for notes without titles yet url(r'^(?P[^/]+)/(?P[^/]+)/(?P[\d^/]+)$', \ NoteView.as_view(), name='note_detail_pk'), # note file by note.slug - url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$', \ + url(r'^' + SLUG.format('school_') + '/' + SLUG.format('course_') +'/'+ SLUG.format('') +'$', NoteView.as_view(), name='note_detail'), + #url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$', \ + # NoteView.as_view(), name='note_detail'), # ---- JSON views ----# # uploading files -- 2.25.1