Commits

tehfink committed 510c201

new feature: CMSPLUGIN_PHOTOLOGUE_ROOT_URL to control root url when attaching pl app to cms page

  • Participants
  • Parent commits 7ede373
  • Branches cms-2.X

Comments (0)

Files changed (1)

File cmsplugin_photologue/urls.py

-#url bridge for django-cms2 to django-photologue
-#include this module in your django-cms2 project's settings.py
-#eg: CMS_APPLICATIONS_URLS = ( ('cmsplugin_photologue.urls', 'Photologue plugin app'), )
-
 from django.conf.urls.defaults import *
 from django.conf import settings
 
 
-#get variables defined in photologue.urls
-from photologue.urls import gallery_args
-
 #import photologue urls
 urlpatterns = patterns('',
     url(r'^', include('photologue.urls')),
 )
 
-#add urlpattern to display gallery list for the root request (not defined in photologue.urls)
-urlpatterns += patterns('django.views.generic.date_based',
-#   url(r'^/?$', 'archive_index', gallery_args, name='pl-gallery-archive',),
-    #not naming this urlpattern, so it doesn't conflict with identically named urlpattern in photologue.urls
-   url(r'^/?$', 'archive_index', gallery_args, ),
-)
+#check for custom settings, default to pl-gallery-archive
+CMSPLUGIN_PHOTOLOGUE_ROOT_URL = getattr(settings,"CMSPLUGIN_PHOTOLOGUE_ROOT_URL", {'name': 'pl-gallery-archive', 'template': '', 'extra_args' : '', } )
+
+#if a custom base url for the apphook is defined
+if CMSPLUGIN_PHOTOLOGUE_ROOT_URL['name']:
+    from photologue.urls import urlpatterns as pl_urls
+    #find the pattern in photologue's existing urlpatterns
+    for pl_url in pl_urls:
+        if pl_url.name == CMSPLUGIN_PHOTOLOGUE_ROOT_URL['name']:
+
+            #make a copy of the arg dictionary, so we're not modifying the original regex's dict
+            root_args = pl_url.default_args.copy()
+
+            #if a custom template is defined, add it to the context, and change the object name passed to the template
+            if CMSPLUGIN_PHOTOLOGUE_ROOT_URL['template']:
+                root_args.update({
+                    'template_name' : CMSPLUGIN_PHOTOLOGUE_ROOT_URL['template'],
+                    'template_object_name' : 'cmsplugin_photologue',
+                })
+
+            #if extra_args are defined, add it to the context to override the regex's captured values
+            if CMSPLUGIN_PHOTOLOGUE_ROOT_URL['extra_args']:
+                root_args.update(CMSPLUGIN_PHOTOLOGUE_ROOT_URL['extra_args'])
+
+            #define the base url; not naming it, so it doesn't conflict with identical urlpattern in photologue.urls
+            urlpatterns += patterns('', url(r'^/?$', pl_url._callback_str, root_args))
+else:
+    #hacky failsafe: if CMSPLUGIN_PHOTOLOGUE_ROOT_URL is defined improperly: i.e.: no CMSPLUGIN_PHOTOLOGUE_ROOT_URL['name']
+    from photologue.urls import gallery_args
+    #add urlpattern to display gallery list for the root request (not defined in photologue.urls)
+    urlpatterns += patterns('django.views.generic.date_based',
+        #url(r'^/?$', 'archive_index', gallery_args, name='pl-gallery-archive',),
+        #define the base url; not naming it, so it doesn't conflict with identical urlpattern in photologue.urls
+        url(r'^/?$', 'archive_index', gallery_args, ),
+    )