+ context = self.get_context()
+ context.update(extra_context or {})
+ context.update({'entry': entry})
+ return self.entry_page.render_to_response(request, extra_context=context)
+
+ def tag_archive_view(self, request, extra_context=None):
+ if not self.tag_archive_page:
+ raise Http404
+ context = self.get_context()
+ context.update(extra_context or {})
+ context.update({
+ 'tags': self.get_tag_queryset()
+ })
+ return self.tag_archive_page.render_to_response(request, extra_context=context)
+
+ def feed_view(self, get_items_attr, reverse_name):
+ 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):
+ if self.entries_per_page:
+ page_num = request.GET.get('page', 1)
+ paginator, paginated_page, items = paginate(items, self.entries_per_page, page_num)
+ item_context = {
+ 'paginator': paginator,
+ 'paginated_page': paginated_page,
+ self.item_context_var: items
+ }
+ else:
+ item_context = {
+ self.item_context_var: items
+ }
+ return items, item_context
+
+ def title(self, obj):
+ return obj.title
+
+ def item_title(self, item):
+ return item.title
+
+ def item_description(self, item):
+ return item.content
+
+ def item_author_name(self, item):
+ return item.author.get_full_name()
+
+ def item_pubdate(self, item):
+ return item.date
+
+ def item_categories(self, item):
+ return [tag.name for tag in item.tags.all()]
+
+
+class Newsletter(Entity, Titled):
+ pass
+
+
+register_value_model(Newsletter)
+
+
+class NewsletterArticle(Entity, Titled):
+ newsletter = models.ForeignKey(Newsletter, related_name='articles')
+ authors = models.ManyToManyField(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='newsletterarticles')
+ date = models.DateTimeField(default=None)
+ lede = TemplateField(null=True, blank=True, verbose_name='Summary')
+ full_text = TemplateField(db_index=True)
+ tags = models.ManyToManyField(Tag, related_name='newsletterarticles', blank=True, null=True)
+
+ def save(self, *args, **kwargs):
+ if self.date is None:
+ self.date = datetime.now()
+ super(NewsletterArticle, self).save(*args, **kwargs)
+
+ class Meta:
+ get_latest_by = 'date'
+ ordering = ['-date']
+ unique_together = (('newsletter', 'slug'),)
+
+
+register_value_model(NewsletterArticle)
+
+
+class NewsletterIssue(Entity, Titled):
+ newsletter = models.ForeignKey(Newsletter, related_name='issues')
+ numbering = models.CharField(max_length=50, help_text='For example, 04.02 for volume 4, issue 2.')
+ articles = models.ManyToManyField(NewsletterArticle, related_name='issues')
+
+ class Meta:
+ ordering = ['-numbering']
+ unique_together = (('newsletter', 'numbering'),)
+
+
+register_value_model(NewsletterIssue)
+
+
+class NewsletterView(FeedView):
+ ARTICLE_PERMALINK_STYLE_CHOICES = (
+ ('D', 'Year, month, and day'),
+ ('M', 'Year and month'),
+ ('Y', 'Year'),
+ ('S', 'Slug only')
+ )
+
+ newsletter = models.ForeignKey(Newsletter, related_name='newsletterviews')
+
+ index_page = models.ForeignKey(Page, related_name='newsletter_index_related')
+ article_page = models.ForeignKey(Page, related_name='newsletter_article_related')
+ article_archive_page = models.ForeignKey(Page, related_name='newsletter_article_archive_related', null=True, blank=True)
+ issue_page = models.ForeignKey(Page, related_name='newsletter_issue_related')
+ issue_archive_page = models.ForeignKey(Page, related_name='newsletter_issue_archive_related', null=True, blank=True)
+
+ article_permalink_style = models.CharField(max_length=1, choices=ARTICLE_PERMALINK_STYLE_CHOICES)
+ article_permalink_base = models.CharField(max_length=255, blank=False, default='articles')
+ issue_permalink_base = models.CharField(max_length=255, blank=False, default='issues')
+
+ item_context_var = 'articles'
+ object_attr = 'newsletter'
+
+ def __unicode__(self):
+ return "NewsletterView for %s" % self.newsletter.__unicode__()
+
+ def get_reverse_params(self, obj):
+ if isinstance(obj, NewsletterArticle):
+ if obj.newsletter == self.newsletter:
+ kwargs = {'slug': obj.slug}
+ if self.article_permalink_style in 'DMY':
+ kwargs.update({'year': str(obj.date.year).zfill(4)})
+ if self.article_permalink_style in 'DM':
+ kwargs.update({'month': str(obj.date.month).zfill(2)})
+ if self.article_permalink_style == 'D':
+ kwargs.update({'day': str(obj.date.day).zfill(2)})
+ return self.article_view, [], kwargs
+ elif isinstance(obj, NewsletterIssue):
+ if obj.newsletter == self.newsletter:
+ return 'issue', [], {'numbering': obj.numbering}
+ elif isinstance(obj, (date, datetime)):
+ kwargs = {
+ 'year': str(obj.year).zfill(4),
+ 'month': str(obj.month).zfill(2),
+ 'day': str(obj.day).zfill(2)
+ }
+ return 'articles_by_day', [], kwargs
+ raise ViewCanNotProvideSubpath
+
+ @property
+ def urlpatterns(self):
+ urlpatterns = self.feed_patterns(r'^', 'get_all_articles', 'index_page', 'index') + patterns('',
+ url(r'^%s/(?P<numbering>.+)$' % self.issue_permalink_base, self.page_view('get_articles_by_issue', 'issue_page'), name='issue')
+ )
+ if self.issue_archive_page:
+ 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_permalink_style in 'DMY':
+ urlpatterns += self.feed_patterns(r'^%s/(?P<year>\d{4})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_year')
+ if self.article_permalink_style in 'DM':
+ urlpatterns += self.feed_patterns(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_month')
+ if self.article_permalink_style == 'D':
+ urlpatterns += self.feed_patterns(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_day')
+
+ if self.article_permalink_style == 'Y':
+ urlpatterns += patterns('',
+ url(r'^%s/(?P<year>\d{4})/(?P<slug>[\w-]+)$' % self.article_permalink_base, self.article_view)
+ )
+ elif self.article_permalink_style == 'M':
+ urlpatterns += patterns('',
+ url(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[\w-]+)$' % self.article_permalink_base, self.article_view)
+ )
+ elif self.article_permalink_style == 'D':
+ urlpatterns += patterns('',
+ url(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w-]+)$' % self.article_permalink_base, self.article_view)
+ )
+ else:
+ urlpatterns += patterns('',
+ url(r'^%s/(?P<slug>[-\w]+)$' % self.article_permalink_base, self.article_view)
+ )
+
+ return urlpatterns
+
+ def get_context(self):
+ return {'newsletter': self.newsletter}
+
+ def get_article_queryset(self):
+ return self.newsletter.articles.all()
+
+ def get_issue_queryset(self):
+ return self.newsletter.issues.all()
+
+ def get_all_articles(self, request, extra_context=None):
+ return self.get_article_queryset(), extra_context
+
+ def get_articles_by_ymd(self, request, year, month=None, day=None, extra_context=None):
+ articles = self.get_article_queryset().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):
+ try:
+ issue = self.get_issue_queryset().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
+
+ def article_view(self, request, slug, year=None, month=None, day=None, extra_context=None):
+ articles = self.get_article_queryset()
+ if year:
+ articles = articles.filter(date__year=year)
+ if month:
+ articles = articles.filter(date__month=month)
+ if day:
+ articles = articles.filter(date__day=day)
+ try:
+ article = articles.get(slug=slug)
+ except NewsletterArticle.DoesNotExist:
+ raise Http404
+ context = self.get_context()
+ context.update(extra_context or {})
+ context.update({'article': article})
+ return self.article_page.render_to_response(request, extra_context=context)
+
+ def issue_archive_view(self, request, extra_context):
+ if not self.issue_archive_page:
+ raise Http404
+ context = self.get_context()
+ context.update(extra_context or {})
+ context.update({
+ 'issues': self.get_issue_queryset()
+ })
+ return self.issue_archive_page.render_to_response(request, extra_context=context)
+
+ def title(self, obj):
+ return obj.title
+
+ def item_title(self, item):
+ return item.title
+
+ def item_description(self, item):
+ return item.full_text
+
+ def item_author_name(self, item):
+ authors = list(item.authors.all())
+ if len(authors) > 1:
+ return "%s and %s" % (", ".join([author.get_full_name() for author in authors[:-1]]), authors[-1].get_full_name())
+ elif authors:
+ return authors[0].get_full_name()
+ else:
+ return ''
+
+ def item_pubdate(self, item):
+ return item.date
+
+ def item_categories(self, item):
+ return [tag.name for tag in item.tags.all()]
\ No newline at end of file