X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/1d21b0f7b336f5761e570023d64d413748b07cc1..a6694613ce70707661b795a121ef126b11c1777a:/philo/contrib/penfield/models.py?ds=inline diff --git a/philo/contrib/penfield/models.py b/philo/contrib/penfield/models.py index 7a3eaa8..6eef5df 100644 --- a/philo/contrib/penfield/models.py +++ b/philo/contrib/penfield/models.py @@ -1,328 +1,17 @@ +# encoding: utf-8 from datetime import date, datetime from django.conf import settings from django.conf.urls.defaults import url, patterns, include -from django.contrib.sites.models import Site, RequestSite -from django.contrib.syndication.views import add_domain from django.db import models from django.http import Http404, HttpResponse -from django.template import RequestContext, Template as DjangoTemplate -from django.utils import feedgenerator, tzinfo -from django.utils.datastructures import SortedDict -from django.utils.encoding import smart_unicode, force_unicode -from django.utils.html import escape -from philo.contrib.penfield.exceptions import HttpNotAcceptable -from philo.contrib.penfield.middleware import http_not_acceptable +from philo.contrib.winer.models import FeedView from philo.exceptions import ViewCanNotProvideSubpath -from philo.models import Tag, Entity, MultiView, Page, register_value_model, Template +from philo.models import Tag, Entity, Page, register_value_model from philo.models.fields import TemplateField from philo.utils import paginate -try: - import mimeparse -except: - mimeparse = None - - -ATOM = feedgenerator.Atom1Feed.mime_type -RSS = feedgenerator.Rss201rev2Feed.mime_type -FEEDS = SortedDict([ - (ATOM, feedgenerator.Atom1Feed), - (RSS, feedgenerator.Rss201rev2Feed), -]) -FEED_CHOICES = ( - (ATOM, "Atom"), - (RSS, "RSS"), -) - - -class FeedView(MultiView): - """ - :class:`FeedView` handles a number of pages and related feeds for a single object such as a blog or newsletter. In addition to all other methods and attributes, :class:`FeedView` supports the same generic API as `django.contrib.syndication.views.Feed `_. - - """ - #: The type of feed which should be served by the :class:`FeedView`. - feed_type = models.CharField(max_length=50, choices=FEED_CHOICES, default=ATOM) - #: The suffix which will be appended to a page URL for a feed of its items. Default: "feed" - feed_suffix = models.CharField(max_length=255, blank=False, default="feed") - #: A :class:`BooleanField` - whether or not feeds are enabled. - feeds_enabled = models.BooleanField(default=True) - #: A :class:`PositiveIntegerField` - the maximum number of items to return for this feed. All items will be returned if this field is blank. Default: 15. - feed_length = models.PositiveIntegerField(blank=True, null=True, default=15, help_text="The maximum number of items to return for this feed. All items will be returned if this field is blank.") - - #: A :class:`ForeignKey` to a :class:`.Template` which can be used to render the title of each item in the feed. - item_title_template = models.ForeignKey(Template, blank=True, null=True, related_name="%(app_label)s_%(class)s_title_related") - #: A :class:`ForeignKey` to a :class:`.Template` which can be used to render the description of each item in the feed. - item_description_template = models.ForeignKey(Template, blank=True, null=True, related_name="%(app_label)s_%(class)s_description_related") - - #: The name of the context variable to be populated with the items managed by the :class:`FeedView`. - item_context_var = 'items' - #: The attribute on a subclass of :class:`FeedView` which will contain the main object of a feed (such as a :class:`Blog`.) - object_attr = 'object' - - #: A description of the feeds served by the :class:`FeedView`. This is a required part of the :class:`django.contrib.syndication.view.Feed` API. - description = "" - - def feed_patterns(self, base, get_items_attr, page_attr, reverse_name): - """ - Given the name to be used to reverse this view and the names of the attributes for the function that fetches the objects, returns patterns suitable for inclusion in urlpatterns. - - :param base: The base of the returned patterns - that is, the subpath pattern which will reference the page for the items. The :attr:`feed_suffix` will be appended to this subpath. - :param get_items_attr: A callable or the name of a callable on the :class:`FeedView` which will return an (``items``, ``extra_context``) tuple. This will be passed directly to :meth:`feed_view` and :meth:`page_view`. - :param page_attr: A :class:`.Page` instance or the name of an attribute on the :class:`FeedView` which contains a :class:`.Page` instance. This will be passed directly to :meth:`page_view` and will be rendered with the items from ``get_items_attr``. - :param reverse_name: The string which is considered the "name" of the view function returned by :meth:`page_view` for the given parameters. - :returns: Patterns suitable for use in urlpatterns. - - Example:: - - @property - def urlpatterns(self): - urlpatterns = self.feed_patterns(r'^', 'get_all_entries', 'index_page', 'index') - urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_day') - return urlpatterns - - """ - urlpatterns = patterns('') - if self.feeds_enabled: - feed_reverse_name = "%s_feed" % reverse_name - feed_view = http_not_acceptable(self.feed_view(get_items_attr, feed_reverse_name)) - feed_pattern = r'%s%s%s$' % (base, (base and base[-1] != "^") and "/" or "", self.feed_suffix) - urlpatterns += patterns('', - url(feed_pattern, feed_view, name=feed_reverse_name), - ) - urlpatterns += patterns('', - url(r"%s$" % base, self.page_view(get_items_attr, page_attr), name=reverse_name) - ) - return urlpatterns - - def get_object(self, request, **kwargs): - """By default, returns the object stored in the attribute named by :attr:`object_attr`. This can be overridden for subclasses that publish different data for different URL parameters. It is part of the :class:`django.contrib.syndication.views.Feed` API.""" - return getattr(self, self.object_attr) - - def feed_view(self, get_items_attr, reverse_name): - """ - Returns a view function that renders a list of items as a feed. - - :param get_items_attr: A callable or the name of a callable on the :class:`FeedView` that will return a (items, extra_context) tuple when called with view arguments. - :param reverse_name: The name which can be used reverse this feed using the :class:`FeedView` as the urlconf. - - :returns: A view function that renders a list of items as a feed. - - """ - get_items = callable(get_items_attr) and get_items_attr or getattr(self, get_items_attr) - - def inner(request, extra_context=None, *args, **kwargs): - obj = self.get_object(request, *args, **kwargs) - feed = self.get_feed(obj, request, reverse_name) - items, xxx = get_items(request, extra_context=extra_context, *args, **kwargs) - self.populate_feed(feed, items, request) - - response = HttpResponse(mimetype=feed.mime_type) - feed.write(response, 'utf-8') - return response - - return inner - - def page_view(self, get_items_attr, page_attr): - """ - :param get_items_attr: A callable or the name of a callable on the :class:`FeedView` that will return a (items, extra_context) tuple when called with view arguments. - :param page_attr: A :class:`.Page` instance or the name of an attribute on the :class:`FeedView` which contains a :class:`.Page` instance. This will be rendered with the items from ``get_items_attr``. - - :returns: A view function that renders a list of items as an :class:`HttpResponse`. - - """ - get_items = callable(get_items_attr) and get_items_attr or getattr(self, get_items_attr) - page = isinstance(page_attr, Page) and page_attr or getattr(self, page_attr) - - def inner(request, extra_context=None, *args, **kwargs): - items, extra_context = get_items(request, extra_context=extra_context, *args, **kwargs) - items, item_context = self.process_page_items(request, items) - - context = self.get_context() - context.update(extra_context or {}) - context.update(item_context or {}) - - return page.render_to_response(request, extra_context=context) - return inner - - def process_page_items(self, request, items): - """ - Hook for handling any extra processing of ``items`` based on an :class:`HttpRequest`, such as pagination or searching. This method is expected to return a list of items and a dictionary to be added to the page context. - - """ - item_context = { - self.item_context_var: items - } - return items, item_context - - def get_feed_type(self, request): - """ - Intelligently chooses a feed type for a given request. Tries to return :attr:`feed_type`, but if the Accept header does not include that mimetype, tries to return the best match from the feed types that are offered by the :class:`FeedView`. If none of the offered feed types are accepted by the :class:`HttpRequest`, then this method will raise :exc:`philo.contrib.penfield.exceptions.HttpNotAcceptable`. - - """ - feed_type = self.feed_type - if feed_type not in FEEDS: - feed_type = FEEDS.keys()[0] - accept = request.META.get('HTTP_ACCEPT') - if accept and feed_type not in accept and "*/*" not in accept and "%s/*" % feed_type.split("/")[0] not in accept: - # Wups! They aren't accepting the chosen format. Is there another format we can use? - if mimeparse: - feed_type = mimeparse.best_match(FEEDS.keys(), accept) - else: - for feed_type in FEEDS.keys(): - if feed_type in accept or "%s/*" % feed_type.split("/")[0] in accept: - break - else: - feed_type = None - if not feed_type: - raise HttpNotAcceptable - return FEEDS[feed_type] - - def get_feed(self, obj, request, reverse_name): - """ - Returns an unpopulated :class:`django.utils.feedgenerator.DefaultFeed` object for this object. - - """ - try: - current_site = Site.objects.get_current() - except Site.DoesNotExist: - current_site = RequestSite(request) - - feed_type = self.get_feed_type(request) - node = request.node - link = node.get_absolute_url(with_domain=True, request=request, secure=request.is_secure()) - - feed = feed_type( - title = self.__get_dynamic_attr('title', obj), - subtitle = self.__get_dynamic_attr('subtitle', obj), - link = link, - description = self.__get_dynamic_attr('description', obj), - language = settings.LANGUAGE_CODE.decode(), - feed_url = add_domain( - current_site.domain, - self.__get_dynamic_attr('feed_url', obj) or node.construct_url(node._subpath, with_domain=True, request=request, secure=request.is_secure()), - request.is_secure() - ), - author_name = self.__get_dynamic_attr('author_name', obj), - author_link = self.__get_dynamic_attr('author_link', obj), - author_email = self.__get_dynamic_attr('author_email', obj), - categories = self.__get_dynamic_attr('categories', obj), - feed_copyright = self.__get_dynamic_attr('feed_copyright', obj), - feed_guid = self.__get_dynamic_attr('feed_guid', obj), - ttl = self.__get_dynamic_attr('ttl', obj), - **self.feed_extra_kwargs(obj) - ) - return feed - - def populate_feed(self, feed, items, request): - """Populates a :class:`django.utils.feedgenerator.DefaultFeed` instance as is returned by :meth:`get_feed` with the passed-in ``items``.""" - if self.item_title_template: - title_template = DjangoTemplate(self.item_title_template.code) - else: - title_template = None - if self.item_description_template: - description_template = DjangoTemplate(self.item_description_template.code) - else: - description_template = None - - node = request.node - try: - current_site = Site.objects.get_current() - except Site.DoesNotExist: - current_site = RequestSite(request) - - if self.feed_length is not None: - items = items[:self.feed_length] - - for item in items: - if title_template is not None: - title = title_template.render(RequestContext(request, {'obj': item})) - else: - title = self.__get_dynamic_attr('item_title', item) - if description_template is not None: - description = description_template.render(RequestContext(request, {'obj': item})) - else: - description = self.__get_dynamic_attr('item_description', item) - - link = node.construct_url(self.reverse(obj=item), with_domain=True, request=request, secure=request.is_secure()) - - enc = None - enc_url = self.__get_dynamic_attr('item_enclosure_url', item) - if enc_url: - enc = feedgenerator.Enclosure( - url = smart_unicode(add_domain( - current_site.domain, - enc_url, - request.is_secure() - )), - length = smart_unicode(self.__get_dynamic_attr('item_enclosure_length', item)), - mime_type = smart_unicode(self.__get_dynamic_attr('item_enclosure_mime_type', item)) - ) - author_name = self.__get_dynamic_attr('item_author_name', item) - if author_name is not None: - author_email = self.__get_dynamic_attr('item_author_email', item) - author_link = self.__get_dynamic_attr('item_author_link', item) - else: - author_email = author_link = None - - pubdate = self.__get_dynamic_attr('item_pubdate', item) - if pubdate and not pubdate.tzinfo: - ltz = tzinfo.LocalTimezone(pubdate) - pubdate = pubdate.replace(tzinfo=ltz) - - feed.add_item( - title = title, - link = link, - description = description, - unique_id = self.__get_dynamic_attr('item_guid', item, link), - enclosure = enc, - pubdate = pubdate, - author_name = author_name, - author_email = author_email, - author_link = author_link, - categories = self.__get_dynamic_attr('item_categories', item), - item_copyright = self.__get_dynamic_attr('item_copyright', item), - **self.item_extra_kwargs(item) - ) - - def __get_dynamic_attr(self, attname, obj, default=None): - try: - attr = getattr(self, attname) - except AttributeError: - return default - if callable(attr): - # Check func_code.co_argcount rather than try/excepting the - # function and catching the TypeError, because something inside - # the function may raise the TypeError. This technique is more - # accurate. - if hasattr(attr, 'func_code'): - argcount = attr.func_code.co_argcount - else: - argcount = attr.__call__.func_code.co_argcount - if argcount == 2: # one argument is 'self' - return attr(obj) - else: - return attr() - return attr - - def feed_extra_kwargs(self, obj): - """Returns an extra keyword arguments dictionary that is used when initializing the feed generator.""" - return {} - - def item_extra_kwargs(self, item): - """Returns an extra keyword arguments dictionary that is used with the `add_item` call of the feed generator.""" - return {} - - def item_title(self, item): - return escape(force_unicode(item)) - - def item_description(self, item): - return force_unicode(item) - - class Meta: - abstract=True - class Blog(Entity): """Represents a blog which can be posted to.""" @@ -368,10 +57,10 @@ class BlogEntry(Entity): date = models.DateTimeField(default=None) #: The content of the :class:`BlogEntry`. - content = models.TextField() + content = TemplateField() #: An optional brief excerpt from the :class:`BlogEntry`. - excerpt = models.TextField(blank=True, null=True) + excerpt = TemplateField(blank=True, null=True) #: :class:`.Tag`\ s for this :class:`BlogEntry`. tags = models.ManyToManyField(Tag, related_name='blogentries', blank=True, null=True) @@ -395,7 +84,7 @@ register_value_model(BlogEntry) class BlogView(FeedView): """ - A subclass of :class:`FeedView` which handles patterns and feeds for a :class:`Blog` and its related :class:`entries `. + A subclass of :class:`.FeedView` which handles patterns and feeds for a :class:`Blog` and its related :class:`entries `. """ ENTRY_PERMALINK_STYLE_CHOICES = ( @@ -437,14 +126,13 @@ class BlogView(FeedView): tag_permalink_base = models.CharField(max_length=255, blank=False, default='tags') item_context_var = 'entries' - object_attr = 'blog' def __unicode__(self): return u'BlogView for %s' % self.blog.title def get_reverse_params(self, obj): if isinstance(obj, BlogEntry): - if obj.blog == self.blog: + if obj.blog_id == self.blog_id: kwargs = {'slug': obj.slug} if self.entry_permalink_style in 'DMY': kwargs.update({'year': str(obj.date.year).zfill(4)}) @@ -456,7 +144,7 @@ class BlogView(FeedView): elif isinstance(obj, Tag) or (isinstance(obj, models.query.QuerySet) and obj.model == Tag and obj): if isinstance(obj, Tag): obj = [obj] - slugs = [tag.slug for tag in obj if tag in self.get_tag_queryset()] + slugs = [tag.slug for tag in obj if tag in self.get_tag_queryset(self.blog)] if slugs: return 'entries_by_tag', [], {'tag_slugs': "/".join(slugs)} elif isinstance(obj, (date, datetime)): @@ -470,21 +158,21 @@ class BlogView(FeedView): @property def urlpatterns(self): - urlpatterns = self.feed_patterns(r'^', 'get_all_entries', 'index_page', 'index') +\ - self.feed_patterns(r'^%s/(?P[-\w]+[-+/\w]*)$' % self.tag_permalink_base, 'get_entries_by_tag', 'tag_page', 'entries_by_tag') + urlpatterns = self.feed_patterns(r'^', 'get_entries', 'index_page', 'index') +\ + self.feed_patterns(r'^%s/(?P[-\w]+[-+/\w]*)' % self.tag_permalink_base, 'get_entries', 'tag_page', 'entries_by_tag') - if self.tag_archive_page: + if self.tag_archive_page_id: urlpatterns += patterns('', url((r'^%s$' % self.tag_permalink_base), self.tag_archive_view, name='tag_archive') ) - if self.entry_archive_page: + if self.entry_archive_page_id: if self.entry_permalink_style in 'DMY': - urlpatterns += self.feed_patterns(r'^(?P\d{4})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_year') + urlpatterns += self.feed_patterns(r'^(?P\d{4})', 'get_entries', 'entry_archive_page', 'entries_by_year') if self.entry_permalink_style in 'DM': - urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_month') + urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})', 'get_entries', 'entry_archive_page', 'entries_by_month') if self.entry_permalink_style == 'D': - urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_day') + urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})', 'get_entries', 'entry_archive_page', 'entries_by_day') if self.entry_permalink_style == 'D': urlpatterns += patterns('', @@ -508,63 +196,76 @@ class BlogView(FeedView): ) return urlpatterns - def get_context(self): - return {'blog': self.blog} - - def get_entry_queryset(self): - """Returns the default :class:`QuerySet` of :class:`BlogEntry` instances for the :class:`BlogView`.""" - return self.blog.entries.all() + def get_entry_queryset(self, obj): + """Returns the default :class:`QuerySet` of :class:`BlogEntry` instances for the :class:`BlogView` - all entries that are considered posted in the past. This allows for scheduled posting of entries.""" + return obj.entries.filter(date__lte=datetime.now()) - def get_tag_queryset(self): + def get_tag_queryset(self, obj): """Returns the default :class:`QuerySet` of :class:`.Tag`\ s for the :class:`BlogView`'s :meth:`get_entries_by_tag` and :meth:`tag_archive_view`.""" - return self.blog.entry_tags - - def get_all_entries(self, request, extra_context=None): - """Used to generate :meth:`~FeedView.feed_patterns` for all entries.""" - return self.get_entry_queryset(), extra_context + return obj.entry_tags - def get_entries_by_ymd(self, request, year=None, month=None, day=None, extra_context=None): - """Used to generate :meth:`~FeedView.feed_patterns` for entries with a specific year, month, and day.""" - if not self.entry_archive_page: - raise Http404 - entries = self.get_entry_queryset() - if year: - entries = entries.filter(date__year=year) - if month: - entries = entries.filter(date__month=month) - if day: - entries = entries.filter(date__day=day) + def get_object(self, request, year=None, month=None, day=None, tag_slugs=None): + """Returns a dictionary representing the parameters for a feed which will be exposed.""" + if tag_slugs is None: + tags = None + else: + tag_slugs = tag_slugs.replace('+', '/').split('/') + tags = self.get_tag_queryset(self.blog).filter(slug__in=tag_slugs) + if not tags: + raise Http404 + + # Raise a 404 on an incorrect slug. + found_slugs = set([tag.slug for tag in tags]) + for slug in tag_slugs: + if slug and slug not in found_slugs: + raise Http404 - context = extra_context or {} - context.update({'year': year, 'month': month, 'day': day}) - return entries, context + try: + if year and month and day: + context_date = date(int(year), int(month), int(day)) + elif year and month: + context_date = date(int(year), int(month), 1) + elif year: + context_date = date(int(year), 1, 1) + else: + context_date = None + except TypeError, ValueError: + context_date = None + + return { + 'blog': self.blog, + 'tags': tags, + 'year': year, + 'month': month, + 'day': day, + 'date': context_date + } - def get_entries_by_tag(self, request, tag_slugs, extra_context=None): - """Used to generate :meth:`~FeedView.feed_patterns` for entries with all of the given tags.""" - tag_slugs = tag_slugs.replace('+', '/').split('/') - tags = self.get_tag_queryset().filter(slug__in=tag_slugs) + def get_entries(self, obj, request, year=None, month=None, day=None, tag_slugs=None, extra_context=None): + """Returns the :class:`BlogEntry` objects which will be exposed for the given object, as returned from :meth:`get_object`.""" + entries = self.get_entry_queryset(obj['blog']) - if not tags: - raise Http404 + if obj['tags'] is not None: + tags = obj['tags'] + for tag in tags: + entries = entries.filter(tags=tag) - # Raise a 404 on an incorrect slug. - found_slugs = [tag.slug for tag in tags] - for slug in tag_slugs: - if slug and slug not in found_slugs: - raise Http404 - - entries = self.get_entry_queryset() - for tag in tags: - entries = entries.filter(tags=tag) + if obj['date'] is not None: + if year: + entries = entries.filter(date__year=year) + if month: + entries = entries.filter(date__month=month) + if day: + entries = entries.filter(date__day=day) context = extra_context or {} - context.update({'tags': tags}) + context.update(obj) return entries, context def entry_view(self, request, slug, year=None, month=None, day=None, extra_context=None): """Renders :attr:`entry_page` with the entry specified by the given parameters.""" - entries = self.get_entry_queryset() + entries = self.get_entry_queryset(self.blog) if year: entries = entries.filter(date__year=year) if month: @@ -587,36 +288,12 @@ class BlogView(FeedView): context = self.get_context() context.update(extra_context or {}) context.update({ - 'tags': self.get_tag_queryset() + 'tags': self.get_tag_queryset(self.blog) }) return self.tag_archive_page.render_to_response(request, extra_context=context) - def feed_view(self, get_items_attr, reverse_name): - """Overrides :meth:`FeedView.feed_view` to add :class:`.Tag`\ s to the feed as categories.""" - get_items = callable(get_items_attr) and get_items_attr or getattr(self, get_items_attr) - - def inner(request, extra_context=None, *args, **kwargs): - obj = self.get_object(request, *args, **kwargs) - feed = self.get_feed(obj, request, reverse_name) - items, extra_context = get_items(request, extra_context=extra_context, *args, **kwargs) - self.populate_feed(feed, items, request) - - if 'tags' in extra_context: - tags = extra_context['tags'] - feed.feed['link'] = request.node.construct_url(self.reverse(obj=tags), with_domain=True, request=request, secure=request.is_secure()) - else: - tags = obj.entry_tags - - feed.feed['categories'] = [tag.name for tag in tags] - - response = HttpResponse(mimetype=feed.mime_type) - feed.write(response, 'utf-8') - return response - - return inner - def process_page_items(self, request, items): - """Overrides :meth:`FeedView.process_page_items` to add pagination.""" + """Overrides :meth:`.FeedView.process_page_items` to add pagination.""" if self.entries_per_page: page_num = request.GET.get('page', 1) paginator, paginated_page, items = paginate(items, self.entries_per_page, page_num) @@ -632,7 +309,25 @@ class BlogView(FeedView): return items, item_context def title(self, obj): - return obj.title + title = obj['blog'].title + if obj['tags']: + title += u" – %s" % u", ".join((tag.name for tag in obj['tags'])) + date = obj['date'] + if date: + if obj['day']: + datestr = date.strftime("%F %j, %Y") + elif obj['month']: + datestr = date.strftime("%F, %Y") + elif obj['year']: + datestr = date.strftime("%Y") + title += u" – %s" % datestr + return title + + def categories(self, obj): + tags = obj['tags'] + if tags: + return (tag.name for tag in tags) + return None def item_title(self, item): return item.title @@ -725,7 +420,7 @@ register_value_model(NewsletterIssue) class NewsletterView(FeedView): - """A subclass of :class:`FeedView` which handles patterns and feeds for a :class:`Newsletter` and its related :class:`articles `.""" + """A subclass of :class:`.FeedView` which handles patterns and feeds for a :class:`Newsletter` and its related :class:`articles `.""" ARTICLE_PERMALINK_STYLE_CHOICES = ( ('D', 'Year, month, and day'), ('M', 'Year and month'), @@ -767,7 +462,7 @@ class NewsletterView(FeedView): def get_reverse_params(self, obj): if isinstance(obj, NewsletterArticle): - if obj.newsletter == self.newsletter: + if obj.newsletter_id == self.newsletter_id: kwargs = {'slug': obj.slug} if self.article_permalink_style in 'DMY': kwargs.update({'year': str(obj.date.year).zfill(4)}) @@ -777,7 +472,7 @@ class NewsletterView(FeedView): kwargs.update({'day': str(obj.date.day).zfill(2)}) return self.article_view, [], kwargs elif isinstance(obj, NewsletterIssue): - if obj.newsletter == self.newsletter: + if obj.newsletter_id == self.newsletter_id: return 'issue', [], {'numbering': obj.numbering} elif isinstance(obj, (date, datetime)): kwargs = { @@ -793,14 +488,12 @@ class NewsletterView(FeedView): urlpatterns = self.feed_patterns(r'^', 'get_all_articles', 'index_page', 'index') + patterns('', url(r'^%s/(?P.+)$' % self.issue_permalink_base, self.page_view('get_articles_by_issue', 'issue_page'), name='issue') ) - if self.issue_archive_page: + if self.issue_archive_page_id: urlpatterns += patterns('', url(r'^%s$' % self.issue_permalink_base, self.issue_archive_view, 'issue_archive') ) - if self.article_archive_page: - urlpatterns += patterns('', - url(r'^%s' % self.article_permalink_base, include(self.feed_patterns('get_all_articles', 'article_archive_page', 'articles'))) - ) + if self.article_archive_page_id: + urlpatterns += self.feed_patterns(r'^%s' % self.article_permalink_base, 'get_all_articles', 'article_archive_page', 'articles') if self.article_permalink_style in 'DMY': urlpatterns += self.feed_patterns(r'^%s/(?P\d{4})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_year') if self.article_permalink_style in 'DM': @@ -830,40 +523,40 @@ class NewsletterView(FeedView): def get_context(self): return {'newsletter': self.newsletter} - def get_article_queryset(self): - """Returns the default :class:`QuerySet` of :class:`NewsletterArticle` instances for the :class:`NewsletterView`.""" - return self.newsletter.articles.all() + def get_article_queryset(self, obj): + """Returns the default :class:`QuerySet` of :class:`NewsletterArticle` instances for the :class:`NewsletterView` - all articles that are considered posted in the past. This allows for scheduled posting of articles.""" + return obj.articles.filter(date__lte=datetime.now()) - def get_issue_queryset(self): + def get_issue_queryset(self, obj): """Returns the default :class:`QuerySet` of :class:`NewsletterIssue` instances for the :class:`NewsletterView`.""" - return self.newsletter.issues.all() + return obj.issues.all() - def get_all_articles(self, request, extra_context=None): - """Used to generate :meth:`FeedView.feed_patterns` for all entries.""" - return self.get_article_queryset(), extra_context + def get_all_articles(self, obj, request, extra_context=None): + """Used to generate :meth:`~.FeedView.feed_patterns` for all entries.""" + return self.get_article_queryset(obj), extra_context - def get_articles_by_ymd(self, request, year, month=None, day=None, extra_context=None): - """Used to generate :meth:`FeedView.feed_patterns` for a specific year, month, and day.""" - articles = self.get_article_queryset().filter(date__year=year) + def get_articles_by_ymd(self, obj, request, year, month=None, day=None, extra_context=None): + """Used to generate :meth:`~.FeedView.feed_patterns` for a specific year, month, and day.""" + articles = self.get_article_queryset(obj).filter(date__year=year) if month: articles = articles.filter(date__month=month) if day: articles = articles.filter(date__day=day) return articles, extra_context - def get_articles_by_issue(self, request, numbering, extra_context=None): - """Used to generate :meth:`FeedView.feed_patterns` for articles from a certain issue.""" + def get_articles_by_issue(self, obj, request, numbering, extra_context=None): + """Used to generate :meth:`~.FeedView.feed_patterns` for articles from a certain issue.""" try: - issue = self.get_issue_queryset().get(numbering=numbering) + issue = self.get_issue_queryset(obj).get(numbering=numbering) except NewsletterIssue.DoesNotExist: raise Http404 context = extra_context or {} context.update({'issue': issue}) - return self.get_article_queryset().filter(issues=issue), context + return self.get_article_queryset(obj).filter(issues=issue), context def article_view(self, request, slug, year=None, month=None, day=None, extra_context=None): """Renders :attr:`article_page` with the article specified by the given parameters.""" - articles = self.get_article_queryset() + articles = self.get_article_queryset(self.newsletter) if year: articles = articles.filter(date__year=year) if month: @@ -886,7 +579,7 @@ class NewsletterView(FeedView): context = self.get_context() context.update(extra_context or {}) context.update({ - 'issues': self.get_issue_queryset() + 'issues': self.get_issue_queryset(self.newsletter) }) return self.issue_archive_page.render_to_response(request, extra_context=context)