Skip to content

Commit

Permalink
added complimentary methods to URLStructure.
Browse files Browse the repository at this point in the history
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
dduan committed Sep 17, 2010
1 parent e833880 commit 4778ff3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
1 change: 0 additions & 1 deletion sophie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions sophie/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -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'):
Expand All @@ -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.

Expand Down
11 changes: 3 additions & 8 deletions sophie/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions sophie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.