Permalink
Browse files

added complimentary methods to URLStructure.

When URLStructure.pagination_suffix is set to be true,
urls defined in urlpatterns will be suffixed with page_slug field.
Similarly, when URLStructure.blog_prefix is set to be true,
urls in urlpatterns will be prefixed with the blog_slug field if
necessary.

URLStructure.pagination_suffix is False by default while
URLStructure.blog_prefix is True by default.
  • Loading branch information...
1 parent e833880 commit 4778ff3f46d850875769bb749c352ac0b70c9214 @dduan committed Sep 17, 2010
Showing with 33 additions and 9 deletions.
  1. +0 −1 sophie/models.py
  2. +24 −0 sophie/plugins/__init__.py
  3. +3 −8 sophie/urls.py
  4. +6 −0 sophie/utils.py
View
1 sophie/models.py
@@ -7,7 +7,6 @@
from sophie.managers import LiveEntryManager, ShownCategoryManager
from sophie.utils import multiblog_enabled
-
class Blog(models.Model):
title = models.CharField(
max_length = 200,
View
24 sophie/plugins/__init__.py
@@ -1,3 +1,5 @@
+from sophie.utils import blog_bit, page_bit
+
class PluginMount(type):
def __init__(cls, name, bases, attrs):
if not hasattr(cls, 'plugins'):
@@ -8,6 +10,28 @@ def __init__(cls, name, bases, attrs):
class URLStructure:
__metaclass__ = PluginMount
+ urlpatterns = ()
+ pagination_suffix = False
+ blog_prefix = True
+
+ @classmethod
+ def get_patterns(cls):
+ if cls.blog_prefix:
+ cls._add_blog_prefix()
+ if cls.pagination:
+ cls._add_pagination_suffix()
+ return cls.urlpatterns
+
+ @classmethod
+ def _add_blog_prefix(cls):
+ for p,i in enumerate(cls.urlpatterns):
+ cls.urlpatterns[i] = blog_bit + p
+
+ @classmethod
+ def _add_pagination_suffix(cls):
+ for i in range(len(cls.urlpatterns)):
+ cls.urlpatterns[i] += page_bit
+
# Auto-discover the modules under this directory.
View
11 sophie/urls.py
@@ -11,22 +11,17 @@
from sophie.models import Blog
from sophie.sitemaps import BlogSitemap
from sophie.feeds import BlogFeed, CategoryFeed
-from sophie.models import multiblog_enabled
+from sophie.utils import multiblog_enabled, blog_bit, page_bit
-# To save some typing later...
-if multiblog_enabled:
- blog_bit = r'(?:(?P<blog_slug>[\w-]+)/)?'
-else:
- blog_bit = ''
-page_bit = r'(?:(?P<page_num>\d+)/)?'
+# This is here to save some typings later..
slug_bit = r'(?P<%s_slug>[\w-]+)'
urlpatterns = patterns('',)
from sophie.plugins import URLStructure
for struct in URLStructure.plugins:
- urlpatterns += struct.urlpatterns
+ urlpatterns += struct.get_patterns()
# Feed urls
View
6 sophie/utils.py
@@ -54,3 +54,9 @@ def page(self, number):
except (EmptyPage, InvalidPage):
return super(LaidbackPaginator, self).page(self.num_pages)
+# Stuff used in urls.py as well as in plugins.URLStructure
+if multiblog_enabled:
+ blog_bit = r'(?:(?P<blog_slug>[\w-]+)/)?'
+else:
+ blog_bit = ''
+page_bit = r'(?:(?P<page_num>\d+)/)?'

0 comments on commit 4778ff3

Please sign in to comment.