From e9c46f23db304b98c4527cf223505960c3761f30 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 4 May 2018 12:13:52 +1000 Subject: [PATCH] Switch home.urls and entries.urls over to modern path() definitions, with param conversion and such --- entries/kinds.py | 16 ++++++++-- entries/models.py | 14 ++++++--- entries/pagination.py | 2 +- entries/urls.py | 36 ++++++++++++++-------- entries/views/lists.py | 6 ++-- home/urls.py | 7 +++-- home/views.py | 2 +- lemoncurry/templatetags/lemoncurry_tags.py | 8 +++-- 8 files changed, 60 insertions(+), 31 deletions(-) diff --git a/entries/kinds.py b/entries/kinds.py index 1227b9d..5fb9528 100644 --- a/entries/kinds.py +++ b/entries/kinds.py @@ -14,7 +14,7 @@ class Entry: return self.index_page() def index_page(self, page=0): - kwargs = {'kind': self.plural} + kwargs = {'kind': self} if page > 1: kwargs['page'] = page return reverse('entries:index', kwargs=kwargs) @@ -29,11 +29,11 @@ class Entry: @property def atom(self): - return reverse('entries:atom_by_kind', kwargs={'kind': self.plural}) + return reverse('entries:atom_by_kind', kwargs={'kind': self}) @property def rss(self): - return reverse('entries:rss_by_kind', kwargs={'kind': self.plural}) + return reverse('entries:rss_by_kind', kwargs={'kind': self}) Note = Entry( @@ -80,3 +80,13 @@ all = (Note, Article, Photo) on_home = {k.id for k in all if k.on_home} from_id = {k.id: k for k in all} from_plural = {k.plural: k for k in all} + + +class EntryKindConverter: + regex = '|'.join(k.plural for k in all) + + def to_python(self, plural): + return from_plural[plural] + + def to_url(self, k): + return k.plural diff --git a/entries/models.py b/entries/models.py index 43582fb..93034b2 100644 --- a/entries/models.py +++ b/entries/models.py @@ -43,7 +43,9 @@ class Cat(models.Model): class EntryManager(models.Manager): def get_queryset(self): qs = super(EntryManager, self).get_queryset() - return qs.select_related('author').prefetch_related('cats', 'syndications') + return (qs + .select_related('author') + .prefetch_related('cats', 'syndications')) class Entry(ModelMeta, TimeStampedModel): @@ -99,7 +101,11 @@ class Entry(ModelMeta, TimeStampedModel): def title(self): if self.name: return self.name - return shorten(utils.to_plain(self.paragraphs[0]), width=100, placeholder='…') + return shorten( + utils.to_plain(self.paragraphs[0]), + width=100, + placeholder='…' + ) @property def excerpt(self): @@ -137,7 +143,7 @@ class Entry(ModelMeta, TimeStampedModel): @property def url(self): kind = kinds.from_id[self.kind] - args = [kind.plural, self.id] + args = [kind, self.id] if kind.slug: args.append(self.slug) return reverse('entries:entry', args=args) @@ -145,7 +151,7 @@ class Entry(ModelMeta, TimeStampedModel): @property def amp_url(self): kind = kinds.from_id[self.kind] - args = [kind.plural, self.id] + args = [kind, self.id] if kind.slug: args.append(self.slug) return reverse('entries:entry_amp', args=args) diff --git a/entries/pagination.py b/entries/pagination.py index 04023ef..ab3763e 100644 --- a/entries/pagination.py +++ b/entries/pagination.py @@ -17,7 +17,7 @@ def paginate(queryset, reverse, page): # If the first page was requested, redirect to the clean version of the URL # with no page suffix. - if page == '1': + if page == 1: return redirect(Page(1).url) paginator = Paginator(queryset, 10) diff --git a/entries/urls.py b/entries/urls.py index 0d8daf1..19952f3 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -1,9 +1,10 @@ -from django.conf.urls import url -from django.urls import reverse +from django.urls import path, register_converter, reverse from . import kinds from .views import feeds, lists, perma from lemoncurry import breadcrumbs as crumbs +register_converter(kinds.EntryKindConverter, 'kind') + def to_pat(*args): return '^{0}$'.format(''.join(args)) @@ -22,14 +23,21 @@ slug_opt = '(?:' + slug + ')?' app_name = 'entries' urlpatterns = ( - url('^atom$', feeds.AtomHomeEntries(), name='atom'), - url('^rss$', feeds.RssHomeEntries(), name='rss'), - url(to_pat('cats', slug, page), lists.by_cat, name='cat'), - url(to_pat(kind, page), lists.by_kind, name='index'), - url(to_pat(kind, '/atom'), feeds.AtomByKind(), name='atom_by_kind'), - url(to_pat(kind, '/rss'), feeds.RssByKind(), name='rss_by_kind'), - url(to_pat(kind, id, slug_opt, '/amp'), perma.entry_amp, name='entry_amp'), - url(to_pat(kind, id, slug_opt), perma.entry, name='entry'), + path('atom', feeds.AtomHomeEntries(), name='atom'), + path('rss', feeds.RssHomeEntries(), name='rss'), + path('cats/', lists.by_cat, name='cat'), + path('cats//page/', lists.by_cat, name='cat'), + path('', lists.by_kind, name='index'), + path('/page/', lists.by_kind, name='index'), + path('/atom', feeds.AtomByKind(), name='atom_by_kind'), + path('/rss', feeds.RssByKind(), name='rss_by_kind'), + + path('//amp', perma.entry_amp, name='entry_amp'), + path('///amp', + perma.entry_amp, name='entry_amp'), + + path('/', perma.entry, name='entry'), + path('//', perma.entry, name='entry'), ) @@ -38,12 +46,16 @@ class IndexCrumb(crumbs.Crumb): super().__init__(prefix('index'), parent='home:index') @property - def label(self): + def kind(self): return self.match.kwargs['kind'] + @property + def label(self): + return self.kind.plural + @property def url(self): - return reverse(prefix('index'), kwargs={'kind': self.label}) + return reverse(prefix('index'), kwargs={'kind': self.kind}) crumbs.add(prefix('cat'), parent='home:index') diff --git a/entries/views/lists.py b/entries/views/lists.py index 90d8bbf..35afd08 100644 --- a/entries/views/lists.py +++ b/entries/views/lists.py @@ -1,14 +1,12 @@ from annoying.decorators import render_to from django.shortcuts import get_object_or_404 from django.urls import reverse -from .. import kinds from ..models import Entry, Cat from ..pagination import paginate @render_to('entries/index.html') -def by_kind(request, kind, page): - kind = kinds.from_plural[kind] +def by_kind(request, kind, page=None): entries = Entry.objects.filter(kind=kind.id) entries = paginate(queryset=entries, reverse=kind.index_page, page=page) if hasattr(entries, 'content'): @@ -23,7 +21,7 @@ def by_kind(request, kind, page): @render_to('entries/index.html') -def by_cat(request, slug, page): +def by_cat(request, slug, page=None): def url(page): kwargs = {'slug': slug} if page > 1: diff --git a/home/urls.py b/home/urls.py index 14b4ced..1f14b68 100644 --- a/home/urls.py +++ b/home/urls.py @@ -1,9 +1,10 @@ -from django.conf.urls import url +from django.urls import path from . import views app_name = 'home' urlpatterns = [ - url(r'^(?:page/(?P\d+))?$', views.index, name='index'), - url(r'^robots.txt$', views.robots, name='robots.txt'), + path('', views.index, name='index'), + path('page/', views.index, name='index'), + path('robots.txt', views.robots, name='robots.txt'), ] diff --git a/home/views.py b/home/views.py index 2572c10..bb414e4 100644 --- a/home/views.py +++ b/home/views.py @@ -12,7 +12,7 @@ breadcrumbs.add('home:index', 'home') @render_to('home/index.html') -def index(request, page): +def index(request, page=None): def url(page): kwargs = {'page': page} if page != 1 else {} return reverse('home:index', kwargs=kwargs) diff --git a/lemoncurry/templatetags/lemoncurry_tags.py b/lemoncurry/templatetags/lemoncurry_tags.py index b5cacd2..0968f43 100644 --- a/lemoncurry/templatetags/lemoncurry_tags.py +++ b/lemoncurry/templatetags/lemoncurry_tags.py @@ -45,7 +45,7 @@ def nav_left(request): items = (MenuItem( label=k.plural, icon=k.icon, - url=('entries:index', (k.plural,)) + url=('entries:index', (k,)) ) for k in kinds.all) return {'items': items, 'request': request} @@ -55,11 +55,13 @@ def nav_right(request): if request.user.is_authenticated: items = ( MenuItem(label='admin', icon='fas fa-cog', url='admin:index'), - MenuItem(label='log out', icon='fas fa-sign-out-alt', url='lemonauth:logout'), + MenuItem(label='log out', icon='fas fa-sign-out-alt', + url='lemonauth:logout'), ) else: items = ( - MenuItem(label='log in', icon='fas fa-sign-in-alt', url='lemonauth:login'), + MenuItem(label='log in', icon='fas fa-sign-in-alt', + url='lemonauth:login'), ) return {'items': items, 'request': request}