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 untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 36 additions and 32 deletions

View file

@ -1,5 +1,6 @@
from django.conf.urls import url from django.conf.urls import url
from . import feeds, kinds, views from . import kinds
from .views import feeds, lists, perma
from lemoncurry import breadcrumbs as crumbs from lemoncurry import breadcrumbs as crumbs
@ -18,7 +19,7 @@ app_name = 'entries'
urlpatterns = [ urlpatterns = [
url('^atom$', feeds.AtomHomeEntries(), name='atom'), url('^atom$', feeds.AtomHomeEntries(), name='atom'),
url('^rss$', feeds.RssHomeEntries(), name='rss'), url('^rss$', feeds.RssHomeEntries(), name='rss'),
url(to_pat('cats', slug, page), views.cat, name='cat'), url(to_pat('cats', slug, page), lists.by_cat, name='cat'),
] ]
crumbs.add(prefix('cat'), parent='home:index') crumbs.add(prefix('cat'), parent='home:index')
@ -28,11 +29,11 @@ for k in kinds.all:
kind = k.plural kind = k.plural
id = r'/(?P<id>\d+)' id = r'/(?P<id>\d+)'
urlpatterns += ( urlpatterns += (
url(to_pat(kind, page), views.index, name=k.index, kwargs={'kind': k}), url(to_pat(kind, page), lists.by_kind, name=k.index, kwargs={'kind': k}),
url(to_pat(kind, '/atom'), feeds.AtomByKind(k), name=k.atom), url(to_pat(kind, '/atom'), feeds.AtomByKind(k), name=k.atom),
url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss), url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss),
url(to_pat(kind, id, slug, '/amp'), views.entry_amp, name=k.entry_amp), url(to_pat(kind, id, slug, '/amp'), perma.entry_amp, name=k.entry_amp),
url(to_pat(kind, id, slug), views.entry, name=k.entry), url(to_pat(kind, id, slug), perma.entry, name=k.entry),
) )
crumbs.add(prefix(k.index), label=k.plural, parent='home:index') crumbs.add(prefix(k.index), label=k.plural, parent='home:index')

View file

View file

@ -4,8 +4,8 @@ from django.urls import reverse
from django.utils.feedgenerator import Atom1Feed from django.utils.feedgenerator import Atom1Feed
from urllib.parse import urljoin from urllib.parse import urljoin
from lemoncurry.templatetags.markdown import markdown from lemoncurry.templatetags.markdown import markdown
from .kinds import on_home from ..kinds import on_home
from .models import Entry from ..models import Entry
class EntriesFeed(Feed): class EntriesFeed(Feed):

View file

@ -1,12 +1,12 @@
from annoying.decorators import render_to from annoying.decorators import render_to
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404
from django.urls import reverse from django.urls import reverse
from .models import Entry, Cat from ..models import Entry, Cat
from .pagination import paginate from ..pagination import paginate
@render_to('entries/index.html') @render_to('entries/index.html')
def index(request, kind, page): def by_kind(request, kind, page):
def url(page): def url(page):
kwargs = {'page': page} if page > 1 else {} kwargs = {'page': page} if page > 1 else {}
return reverse('entries:' + kind.index, kwargs=kwargs) return reverse('entries:' + kind.index, kwargs=kwargs)
@ -25,7 +25,7 @@ def index(request, kind, page):
@render_to('entries/index.html') @render_to('entries/index.html')
def cat(request, slug, page): def by_cat(request, slug, page):
def url(page): def url(page):
kwargs = {'slug': slug} kwargs = {'slug': slug}
if page > 1: if page > 1:
@ -42,23 +42,3 @@ def cat(request, slug, page):
'entries': entries, 'entries': entries,
'title': '#' + cat.name, 'title': '#' + cat.name,
} }
@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}

23
entries/views/perma.py Normal file
View 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}