Switch home.urls and entries.urls over to modern path() definitions, with param conversion and such

This commit is contained in:
Danielle McLean 2018-05-04 12:13:52 +10:00
parent 0b43aad50e
commit e9c46f23db
Signed by untrusted user: 00dani
GPG key ID: 8EB789DDF3ABD240
8 changed files with 60 additions and 31 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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/<slug:slug>', lists.by_cat, name='cat'),
path('cats/<slug:slug>/page/<int:page>', lists.by_cat, name='cat'),
path('<kind:kind>', lists.by_kind, name='index'),
path('<kind:kind>/page/<int:page>', lists.by_kind, name='index'),
path('<kind:kind>/atom', feeds.AtomByKind(), name='atom_by_kind'),
path('<kind:kind>/rss', feeds.RssByKind(), name='rss_by_kind'),
path('<kind:kind>/<int:id>/amp', perma.entry_amp, name='entry_amp'),
path('<kind:kind>/<int:id>/<slug:slug>/amp',
perma.entry_amp, name='entry_amp'),
path('<kind:kind>/<int:id>', perma.entry, name='entry'),
path('<kind:kind>/<int:id>/<slug:slug>', 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')

View file

@ -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:

View file

@ -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<page>\d+))?$', views.index, name='index'),
url(r'^robots.txt$', views.robots, name='robots.txt'),
path('', views.index, name='index'),
path('page/<int:page>', views.index, name='index'),
path('robots.txt', views.robots, name='robots.txt'),
]

View file

@ -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)

View file

@ -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}