Refactor the 'entries' views into a package rather than just one module, so more views can be added without clutter

This commit is contained in:
Danielle McLean 2018-03-07 15:46:21 +11:00
parent cf0aea4f73
commit c359b7640e
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 36 additions and 32 deletions

84
entries/views/feeds.py Normal file
View file

@ -0,0 +1,84 @@
from django.contrib.sites.models import Site
from django.contrib.syndication.views import Feed
from django.urls import reverse
from django.utils.feedgenerator import Atom1Feed
from urllib.parse import urljoin
from lemoncurry.templatetags.markdown import markdown
from ..kinds import on_home
from ..models import Entry
class EntriesFeed(Feed):
def item_title(self, entry):
return entry.title
def item_description(self, entry):
return markdown(entry.content)
def item_author_name(self, entry):
return entry.author.name
def item_author_email(self, entry):
return entry.author.email
def item_author_link(self, entry):
base = 'https://' + Site.objects.get_current().domain
return urljoin(base, entry.author.url)
def item_pubdate(self, entry):
return entry.published
def item_updatedate(self, entry):
return entry.updated
def item_categories(self, entry):
return (cat.name for cat in entry.cats.all())
class RssByKind(EntriesFeed):
def __init__(self, kind):
self.kind = kind
def title(self):
return "{0} ~ {1}".format(
self.kind.plural,
Site.objects.get_current().name,
)
def link(self):
return reverse('entries:' + self.kind.index)
def description(self):
return "all {0} at {1}".format(
self.kind.plural,
Site.objects.get_current().name,
)
def items(self):
return Entry.objects.filter(kind=self.kind.id)
class AtomByKind(RssByKind):
feed_type = Atom1Feed
subtitle = RssByKind.description
class RssHomeEntries(EntriesFeed):
def title(self):
return Site.objects.get_current().name
def link(self):
return reverse('home:index')
def description(self):
return "content from {0}".format(
Site.objects.get_current().name,
)
def items(self):
return Entry.objects.filter(kind__in=on_home)
class AtomHomeEntries(RssHomeEntries):
feed_type = Atom1Feed
subtitle = RssHomeEntries.description