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:
parent
cf0aea4f73
commit
c359b7640e
5 changed files with 36 additions and 32 deletions
0
entries/views/__init__.py
Normal file
0
entries/views/__init__.py
Normal file
84
entries/views/feeds.py
Normal file
84
entries/views/feeds.py
Normal 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
|
44
entries/views/lists.py
Normal file
44
entries/views/lists.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from annoying.decorators import render_to
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from ..models import Entry, Cat
|
||||
from ..pagination import paginate
|
||||
|
||||
|
||||
@render_to('entries/index.html')
|
||||
def by_kind(request, kind, page):
|
||||
def url(page):
|
||||
kwargs = {'page': page} if page > 1 else {}
|
||||
return reverse('entries:' + kind.index, kwargs=kwargs)
|
||||
|
||||
entries = Entry.objects.filter(kind=kind.id)
|
||||
entries = paginate(queryset=entries, reverse=url, page=page)
|
||||
if hasattr(entries, 'content'):
|
||||
return entries
|
||||
|
||||
return {
|
||||
'entries': entries,
|
||||
'atom': 'entries:' + kind.atom,
|
||||
'rss': 'entries:' + kind.rss,
|
||||
'title': kind.plural,
|
||||
}
|
||||
|
||||
|
||||
@render_to('entries/index.html')
|
||||
def by_cat(request, slug, page):
|
||||
def url(page):
|
||||
kwargs = {'slug': slug}
|
||||
if page > 1:
|
||||
kwargs['page'] = page
|
||||
return reverse('entries:cat', kwargs=kwargs)
|
||||
|
||||
cat = get_object_or_404(Cat, slug=slug)
|
||||
entries = cat.entries.all()
|
||||
entries = paginate(queryset=entries, reverse=url, page=page)
|
||||
if hasattr(entries, 'content'):
|
||||
return entries
|
||||
|
||||
return {
|
||||
'entries': entries,
|
||||
'title': '#' + cat.name,
|
||||
}
|
23
entries/views/perma.py
Normal file
23
entries/views/perma.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
from annoying.decorators import render_to
|
||||
from django.shortcuts import redirect
|
||||
from ..models import Entry
|
||||
|
||||
|
||||
@render_to('entries/entry.html')
|
||||
def entry(request, id, slug=None):
|
||||
entry = Entry.objects.get(pk=id)
|
||||
if request.path != entry.url:
|
||||
return redirect(entry.url, permanent=True)
|
||||
return {
|
||||
'entry': entry,
|
||||
'title': entry.title,
|
||||
'meta': entry.as_meta(request)
|
||||
}
|
||||
|
||||
|
||||
@render_to('entries/entry_amp.html')
|
||||
def entry_amp(request, id, slug=None):
|
||||
entry = Entry.objects.get(pk=id)
|
||||
if request.path != entry.amp_url:
|
||||
return redirect(entry.amp_url, permanent=True)
|
||||
return {'entry': entry}
|
Loading…
Add table
Add a link
Reference in a new issue