1 from django.db import models
2 from django.conf import settings
3 from philo.models import Tag, Titled, Entity, MultiView, Page, register_value_model, TemplateField
4 from philo.exceptions import ViewCanNotProvideSubpath
5 from django.conf.urls.defaults import url, patterns, include
6 from django.http import Http404
7 from datetime import date, datetime
8 from philo.utils import paginate
9 from philo.contrib.penfield.validators import validate_pagination_count
10 from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
11 from philo.contrib.penfield.utils import FeedMultiViewMixin
14 class Blog(Entity, Titled):
17 """ Returns a QuerySet of Tags that are used on any entries in this blog. """
18 return Tag.objects.filter(blogentries__blog=self).distinct()
21 def entry_dates(self):
22 dates = {'year': self.entries.dates('date', 'year', order='DESC'), 'month': self.entries.dates('date', 'month', order='DESC'), 'day': self.entries.dates('date', 'day', order='DESC')}
26 register_value_model(Blog)
29 class BlogEntry(Entity, Titled):
30 blog = models.ForeignKey(Blog, related_name='entries', blank=True, null=True)
31 author = models.ForeignKey(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='blogentries')
32 date = models.DateTimeField(default=datetime.now)
33 content = models.TextField()
34 excerpt = models.TextField(blank=True, null=True)
35 tags = models.ManyToManyField(Tag, related_name='blogentries', blank=True, null=True)
39 verbose_name_plural = "blog entries"
42 register_value_model(BlogEntry)
45 class BlogView(MultiView, FeedMultiViewMixin):
46 ENTRY_PERMALINK_STYLE_CHOICES = (
47 ('D', 'Year, month, and day'),
48 ('M', 'Year and month'),
54 blog = models.ForeignKey(Blog, related_name='blogviews')
56 index_page = models.ForeignKey(Page, related_name='blog_index_related')
57 entry_page = models.ForeignKey(Page, related_name='blog_entry_related')
58 entry_archive_page = models.ForeignKey(Page, related_name='blog_entry_archive_related', null=True, blank=True)
59 tag_page = models.ForeignKey(Page, related_name='blog_tag_related')
60 tag_archive_page = models.ForeignKey(Page, related_name='blog_tag_archive_related', null=True, blank=True)
61 entries_per_page = models.IntegerField(blank=True, validators=[validate_pagination_count], null=True)
63 entry_permalink_style = models.CharField(max_length=1, choices=ENTRY_PERMALINK_STYLE_CHOICES)
64 entry_permalink_base = models.CharField(max_length=255, blank=False, default='entries')
65 tag_permalink_base = models.CharField(max_length=255, blank=False, default='tags')
66 feed_suffix = models.CharField(max_length=255, blank=False, default=FeedMultiViewMixin.feed_suffix)
67 feeds_enabled = models.BooleanField()
70 def __unicode__(self):
71 return u'BlogView for %s' % self.blog.title
75 return self.entries_per_page
77 def get_reverse_params(self, obj):
78 if isinstance(obj, BlogEntry):
79 if obj.blog == self.blog:
80 kwargs = {'slug': obj.slug}
81 if self.entry_permalink_style in 'DMY':
82 kwargs.update({'year': str(obj.date.year).zfill(4)})
83 if self.entry_permalink_style in 'DM':
84 kwargs.update({'month': str(obj.date.month).zfill(2)})
85 if self.entry_permalink_style == 'D':
86 kwargs.update({'day': str(obj.date.day).zfill(2)})
87 return self.entry_view, [], kwargs
88 elif isinstance(obj, Tag):
89 if obj in self.blog.entry_tags:
90 return 'entries_by_tag', [], {'tag_slugs': obj.slug}
91 elif isinstance(obj, (date, datetime)):
93 'year': str(obj.year).zfill(4),
94 'month': str(obj.month).zfill(2),
95 'day': str(obj.day).zfill(2)
97 return 'entries_by_day', [], kwargs
98 raise ViewCanNotProvideSubpath
100 def get_context(self):
101 return {'blog': self.blog}
104 def urlpatterns(self):
105 urlpatterns = patterns('',
106 url(r'^', include(self.feed_patterns(self.get_all_entries, self.index_page, 'index'))),
107 url(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)/%s/' % (self.tag_permalink_base, self.feed_suffix), self.feed_view(self.get_entries_by_tag, 'entries_by_tag_feed'), name='entries_by_tag_feed'),
108 url(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)/' % self.tag_permalink_base, self.page_view(self.get_entries_by_tag, self.tag_page), name='entries_by_tag')
110 if self.tag_archive_page:
111 urlpatterns += patterns('',
112 url((r'^(?:%s)/?$' % self.tag_permalink_base), self.tag_archive_view)
115 if self.entry_archive_page:
116 if self.entry_permalink_style in 'DMY':
117 urlpatterns += patterns('',
118 url(r'^(?P<year>\d{4})/', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_year')))
120 if self.entry_permalink_style in 'DM':
121 urlpatterns += patterns('',
122 url(r'^(?P<year>\d{4})/(?P<month>\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_month'))),
124 if self.entry_permalink_style == 'D':
125 urlpatterns += patterns('',
126 url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_day')))
129 if self.entry_permalink_style == 'D':
130 urlpatterns += patterns('',
131 url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/?$', self.entry_view)
133 elif self.entry_permalink_style == 'M':
134 urlpatterns += patterns('',
135 url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[-\w]+)/?$', self.entry_view)
137 elif self.entry_permalink_style == 'Y':
138 urlpatterns += patterns('',
139 url(r'^(?P<year>\d{4})/(?P<slug>[-\w]+)/?$', self.entry_view)
141 elif self.entry_permalink_style == 'B':
142 urlpatterns += patterns('',
143 url((r'^(?:%s)/(?P<slug>[-\w]+)/?$' % self.entry_permalink_base), self.entry_view)
146 urlpatterns = patterns('',
147 url(r'^(?P<slug>[-\w]+)/?$', self.entry_view)
151 def get_all_entries(self, request, node=None, extra_context=None):
152 return self.blog.entries.all(), extra_context
154 def get_entries_by_ymd(self, request, year=None, month=None, day=None, node=None, extra_context=None):
155 if not self.entry_archive_page:
157 entries = self.blog.entries.all()
159 entries = entries.filter(date__year=year)
161 entries = entries.filter(date__month=month)
163 entries = entries.filter(date__day=day)
165 context = extra_context or {}
166 context.update({'year': year, 'month': month, 'day': day})
167 return entries, context
169 def get_entries_by_tag(self, request, tag_slugs, node=None, extra_context=None):
171 for tag_slug in tag_slugs.replace('+', '/').split('/'):
172 if tag_slug: # ignore blank slugs, handles for multiple consecutive separators (+ or /)
174 tag = self.blog.entry_tags.get(slug=tag_slug)
181 entries = self.blog.entries.all()
183 entries = entries.filter(tags=tag)
185 context = self.get_context()
186 context.update(extra_context or {})
187 context.update({'tags': tags})
189 return entries, context
191 def add_item(self, feed, obj, kwargs=None):
194 'description': obj.excerpt or obj.content,
195 'author_name': obj.author.get_full_name(),
198 defaults.update(kwargs or {})
199 super(BlogView, self).add_item(feed, obj, defaults)
201 def get_feed(self, feed_type, extra_context, kwargs=None):
202 tags = (extra_context or {}).get('tags', None)
203 title = self.blog.title
206 title += " - %s" % ', '.join([tag.name for tag in tags])
211 defaults.update(kwargs or {})
212 return super(BlogView, self).get_feed(feed_type, extra_context, defaults)
214 def entry_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None):
215 entries = self.blog.entries.all()
217 entries = entries.filter(date__year=year)
219 entries = entries.filter(date__month=month)
221 entries = entries.filter(date__day=day)
223 entry = entries.get(slug=slug)
226 context = self.get_context()
227 context.update(extra_context or {})
228 context.update({'entry': entry})
229 return self.entry_page.render_to_response(node, request, extra_context=context)
231 def tag_archive_view(self, request, node=None, extra_context=None):
232 if not self.tag_archive_page:
235 context.update(extra_context or {})
236 context.update({'blog': self.blog})
237 return self.tag_archive_page.render_to_response(node, request, extra_context=context)
240 class Newsletter(Entity, Titled):
244 register_value_model(Newsletter)
247 class NewsletterArticle(Entity, Titled):
248 newsletter = models.ForeignKey(Newsletter, related_name='articles')
249 authors = models.ManyToManyField(getattr(settings, 'PHILO_PERSON_MODULE', 'auth.User'), related_name='newsletterarticles')
250 date = models.DateTimeField(default=datetime.now)
251 lede = TemplateField(null=True, blank=True, verbose_name='Summary')
252 full_text = TemplateField()
253 tags = models.ManyToManyField(Tag, related_name='newsletterarticles', blank=True, null=True)
256 get_latest_by = 'date'
258 unique_together = (('newsletter', 'slug'),)
261 register_value_model(NewsletterArticle)
264 class NewsletterIssue(Entity, Titled):
265 newsletter = models.ForeignKey(Newsletter, related_name='issues')
266 numbering = models.CharField(max_length=50, help_text='For example, 04.02 for volume 4, issue 2.')
267 articles = models.ManyToManyField(NewsletterArticle, related_name='issues')
270 ordering = ['-numbering']
271 unique_together = (('newsletter', 'numbering'),)
274 register_value_model(NewsletterIssue)
277 class NewsletterView(MultiView, FeedMultiViewMixin):
278 ARTICLE_PERMALINK_STYLE_CHOICES = (
279 ('D', 'Year, month, and day'),
280 ('M', 'Year and month'),
285 newsletter = models.ForeignKey(Newsletter, related_name='newsletterviews')
287 index_page = models.ForeignKey(Page, related_name='newsletter_index_related')
288 article_page = models.ForeignKey(Page, related_name='newsletter_article_related')
289 article_archive_page = models.ForeignKey(Page, related_name='newsletter_article_archive_related', null=True, blank=True)
290 issue_page = models.ForeignKey(Page, related_name='newsletter_issue_related')
291 issue_archive_page = models.ForeignKey(Page, related_name='newsletter_issue_archive_related', null=True, blank=True)
293 article_permalink_style = models.CharField(max_length=1, choices=ARTICLE_PERMALINK_STYLE_CHOICES)
294 article_permalink_base = models.CharField(max_length=255, blank=False, default='articles')
295 issue_permalink_base = models.CharField(max_length=255, blank=False, default='issues')
297 feed_suffix = models.CharField(max_length=255, blank=False, default=FeedMultiViewMixin.feed_suffix)
298 feeds_enabled = models.BooleanField()
299 list_var = 'articles'
301 def __unicode__(self):
302 return self.newsletter.__unicode__()
304 def get_reverse_params(self, obj):
305 if isinstance(obj, NewsletterArticle):
306 if obj.newsletter == self.newsletter:
307 kwargs = {'slug': obj.slug}
308 if self.article_permalink_style in 'DMY':
309 kwargs.update({'year': str(obj.date.year).zfill(4)})
310 if self.article_permalink_style in 'DM':
311 kwargs.update({'month': str(obj.date.month).zfill(2)})
312 if self.article_permalink_style == 'D':
313 kwargs.update({'day': str(obj.date.day).zfill(2)})
314 return self.article_view, [], kwargs
315 elif isinstance(obj, NewsletterIssue):
316 if obj.newsletter == self.newsletter:
317 return 'issue', [], {'numbering': obj.numbering}
318 elif isinstance(obj, (date, datetime)):
320 'year': str(obj.year).zfill(4),
321 'month': str(obj.month).zfill(2),
322 'day': str(obj.day).zfill(2)
324 return 'articles_by_day', [], kwargs
325 raise ViewCanNotProvideSubpath
328 def urlpatterns(self):
329 urlpatterns = patterns('',
330 url(r'^', include(self.feed_patterns(self.get_all_articles, self.index_page, 'index'))),
331 url(r'^(?:%s)/(?P<numbering>.+)/' % self.issue_permalink_base, include(self.feed_patterns(self.get_articles_by_issue, self.issue_page, 'issue')))
333 if self.issue_archive_page:
334 urlpatterns += patterns('',
335 url(r'^(?:%s)/$' % self.issue_permalink_base, self.issue_archive_view)
337 if self.article_archive_page:
338 urlpatterns += patterns('',
339 url(r'^(?:%s)/' % self.article_permalink_base, include(self.feed_patterns(self.get_all_articles, self.article_archive_page, 'articles')))
341 if self.article_permalink_style in 'DMY':
342 urlpatterns += patterns('',
343 url(r'^(?:%s)/(?P<year>\d{4})/' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_year')))
345 if self.article_permalink_style in 'DM':
346 urlpatterns += patterns('',
347 url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_month')))
349 if self.article_permalink_style == 'D':
350 urlpatterns += patterns('',
351 url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_day')))
354 if self.article_permalink_style == 'Y':
355 urlpatterns += patterns('',
356 url(r'^(?:%s)/(?P<year>\d{4})/(?P<slug>[\w-]+)/$' % self.article_permalink_base, self.article_view)
358 elif self.article_permalink_style == 'M':
359 urlpatterns += patterns('',
360 url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[\w-]+)/$' % self.article_permalink_base, self.article_view)
362 elif self.article_permalink_style == 'D':
363 urlpatterns += patterns('',
364 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)
367 urlpatterns += patterns('',
368 url(r'^(?:%s)/(?P<slug>[-\w]+)/?$' % self.article_permalink_base, self.article_view)
373 def get_context(self):
374 return {'newsletter': self.newsletter}
376 def get_all_articles(self, request, node, extra_context=None):
377 return self.newsletter.articles.all(), extra_context
379 def get_articles_by_ymd(self, request, year, month=None, day=None, node=None, extra_context=None):
380 articles = self.newsletter.articles.filter(dat__year=year)
382 articles = articles.filter(date__month=month)
384 articles = articles.filter(date__day=day)
387 def get_articles_by_issue(self, request, numbering, node=None, extra_context=None):
389 issue = self.newsletter.issues.get(numbering=numbering)
392 context = extra_context or {}
393 context.update({'issue': issue})
394 return issue.articles.all(), context
396 def article_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None):
397 articles = self.newsletter.articles.all()
399 articles = articles.filter(date__year=year)
401 articles = articles.filter(date__month=month)
403 articles = articles.filter(date__day=day)
405 article = articles.get(slug=slug)
408 context = self.get_context()
409 context.update(extra_context or {})
410 context.update({'article': article})
411 return self.article_page.render_to_response(node, request, extra_context=context)
413 def issue_archive_view(self, request, node=None, extra_context=None):
414 if not self.issue_archive_page:
417 context.update(extra_context or {})
418 context.update({'newsletter': self.newsletter})
419 return self.issue_archive_page.render_to_response(node, request, extra_context=context)
421 def add_item(self, feed, obj, kwargs=None):
424 'author_name': ', '.join(obj.authors),
426 'description': obj.lede or obj.full_text,
427 'categories': [tag.name for tag in obj.tags.all()]
429 defaults.update(kwargs or {})
430 super(NewsletterView, self).add_item(feed, obj, defaults)
432 def get_feed(self, feed_type, extra_context, kwargs=None):
433 title = self.newsletter.title
438 defaults.update(kwargs or {})
439 return super(NewsletterView, self).get_feed(feed_type, extra_context, defaults)