Browse Source

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

tags/v1.9.7
Danielle McLean 1 year ago
parent
commit
e9c46f23db
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240
8 changed files with 60 additions and 31 deletions
  1. 13
    3
      entries/kinds.py
  2. 10
    4
      entries/models.py
  3. 1
    1
      entries/pagination.py
  4. 24
    12
      entries/urls.py
  5. 2
    4
      entries/views/lists.py
  6. 4
    3
      home/urls.py
  7. 1
    1
      home/views.py
  8. 5
    3
      lemoncurry/templatetags/lemoncurry_tags.py

+ 13
- 3
entries/kinds.py View File

@@ -14,7 +14,7 @@ class Entry:
14 14
         return self.index_page()
15 15
 
16 16
     def index_page(self, page=0):
17
-        kwargs = {'kind': self.plural}
17
+        kwargs = {'kind': self}
18 18
         if page > 1:
19 19
             kwargs['page'] = page
20 20
         return reverse('entries:index', kwargs=kwargs)
@@ -29,11 +29,11 @@ class Entry:
29 29
 
30 30
     @property
31 31
     def atom(self):
32
-        return reverse('entries:atom_by_kind', kwargs={'kind': self.plural})
32
+        return reverse('entries:atom_by_kind', kwargs={'kind': self})
33 33
 
34 34
     @property
35 35
     def rss(self):
36
-        return reverse('entries:rss_by_kind', kwargs={'kind': self.plural})
36
+        return reverse('entries:rss_by_kind', kwargs={'kind': self})
37 37
 
38 38
 
39 39
 Note = Entry(
@@ -80,3 +80,13 @@ all = (Note, Article, Photo)
80 80
 on_home = {k.id for k in all if k.on_home}
81 81
 from_id = {k.id: k for k in all}
82 82
 from_plural = {k.plural: k for k in all}
83
+
84
+
85
+class EntryKindConverter:
86
+    regex = '|'.join(k.plural for k in all)
87
+
88
+    def to_python(self, plural):
89
+        return from_plural[plural]
90
+
91
+    def to_url(self, k):
92
+        return k.plural

+ 10
- 4
entries/models.py View File

@@ -43,7 +43,9 @@ class Cat(models.Model):
43 43
 class EntryManager(models.Manager):
44 44
     def get_queryset(self):
45 45
         qs = super(EntryManager, self).get_queryset()
46
-        return qs.select_related('author').prefetch_related('cats', 'syndications')
46
+        return (qs
47
+                .select_related('author')
48
+                .prefetch_related('cats', 'syndications'))
47 49
 
48 50
 
49 51
 class Entry(ModelMeta, TimeStampedModel):
@@ -99,7 +101,11 @@ class Entry(ModelMeta, TimeStampedModel):
99 101
     def title(self):
100 102
         if self.name:
101 103
             return self.name
102
-        return shorten(utils.to_plain(self.paragraphs[0]), width=100, placeholder='…')
104
+        return shorten(
105
+            utils.to_plain(self.paragraphs[0]),
106
+            width=100,
107
+            placeholder='…'
108
+        )
103 109
 
104 110
     @property
105 111
     def excerpt(self):
@@ -137,7 +143,7 @@ class Entry(ModelMeta, TimeStampedModel):
137 143
     @property
138 144
     def url(self):
139 145
         kind = kinds.from_id[self.kind]
140
-        args = [kind.plural, self.id]
146
+        args = [kind, self.id]
141 147
         if kind.slug:
142 148
             args.append(self.slug)
143 149
         return reverse('entries:entry', args=args)
@@ -145,7 +151,7 @@ class Entry(ModelMeta, TimeStampedModel):
145 151
     @property
146 152
     def amp_url(self):
147 153
         kind = kinds.from_id[self.kind]
148
-        args = [kind.plural, self.id]
154
+        args = [kind, self.id]
149 155
         if kind.slug:
150 156
             args.append(self.slug)
151 157
         return reverse('entries:entry_amp', args=args)

+ 1
- 1
entries/pagination.py View File

@@ -17,7 +17,7 @@ def paginate(queryset, reverse, page):
17 17
 
18 18
     # If the first page was requested, redirect to the clean version of the URL
19 19
     # with no page suffix.
20
-    if page == '1':
20
+    if page == 1:
21 21
         return redirect(Page(1).url)
22 22
 
23 23
     paginator = Paginator(queryset, 10)

+ 24
- 12
entries/urls.py View File

@@ -1,9 +1,10 @@
1
-from django.conf.urls import url
2
-from django.urls import reverse
1
+from django.urls import path, register_converter, reverse
3 2
 from . import kinds
4 3
 from .views import feeds, lists, perma
5 4
 from lemoncurry import breadcrumbs as crumbs
6 5
 
6
+register_converter(kinds.EntryKindConverter, 'kind')
7
+
7 8
 
8 9
 def to_pat(*args):
9 10
     return '^{0}$'.format(''.join(args))
@@ -22,14 +23,21 @@ slug_opt = '(?:' + slug + ')?'
22 23
 
23 24
 app_name = 'entries'
24 25
 urlpatterns = (
25
-    url('^atom$', feeds.AtomHomeEntries(), name='atom'),
26
-    url('^rss$', feeds.RssHomeEntries(), name='rss'),
27
-    url(to_pat('cats', slug, page), lists.by_cat, name='cat'),
28
-    url(to_pat(kind, page), lists.by_kind, name='index'),
29
-    url(to_pat(kind, '/atom'), feeds.AtomByKind(), name='atom_by_kind'),
30
-    url(to_pat(kind, '/rss'), feeds.RssByKind(), name='rss_by_kind'),
31
-    url(to_pat(kind, id, slug_opt, '/amp'), perma.entry_amp, name='entry_amp'),
32
-    url(to_pat(kind, id, slug_opt), perma.entry, name='entry'),
26
+    path('atom', feeds.AtomHomeEntries(), name='atom'),
27
+    path('rss', feeds.RssHomeEntries(), name='rss'),
28
+    path('cats/<slug:slug>', lists.by_cat, name='cat'),
29
+    path('cats/<slug:slug>/page/<int:page>', lists.by_cat, name='cat'),
30
+    path('<kind:kind>', lists.by_kind, name='index'),
31
+    path('<kind:kind>/page/<int:page>', lists.by_kind, name='index'),
32
+    path('<kind:kind>/atom', feeds.AtomByKind(), name='atom_by_kind'),
33
+    path('<kind:kind>/rss', feeds.RssByKind(), name='rss_by_kind'),
34
+
35
+    path('<kind:kind>/<int:id>/amp', perma.entry_amp, name='entry_amp'),
36
+    path('<kind:kind>/<int:id>/<slug:slug>/amp',
37
+         perma.entry_amp, name='entry_amp'),
38
+
39
+    path('<kind:kind>/<int:id>', perma.entry, name='entry'),
40
+    path('<kind:kind>/<int:id>/<slug:slug>', perma.entry, name='entry'),
33 41
 )
34 42
 
35 43
 
@@ -38,12 +46,16 @@ class IndexCrumb(crumbs.Crumb):
38 46
         super().__init__(prefix('index'), parent='home:index')
39 47
 
40 48
     @property
41
-    def label(self):
49
+    def kind(self):
42 50
         return self.match.kwargs['kind']
43 51
 
52
+    @property
53
+    def label(self):
54
+        return self.kind.plural
55
+
44 56
     @property
45 57
     def url(self):
46
-        return reverse(prefix('index'), kwargs={'kind': self.label})
58
+        return reverse(prefix('index'), kwargs={'kind': self.kind})
47 59
 
48 60
 
49 61
 crumbs.add(prefix('cat'), parent='home:index')

+ 2
- 4
entries/views/lists.py View File

@@ -1,14 +1,12 @@
1 1
 from annoying.decorators import render_to
2 2
 from django.shortcuts import get_object_or_404
3 3
 from django.urls import reverse
4
-from .. import kinds
5 4
 from ..models import Entry, Cat
6 5
 from ..pagination import paginate
7 6
 
8 7
 
9 8
 @render_to('entries/index.html')
10
-def by_kind(request, kind, page):
11
-    kind = kinds.from_plural[kind]
9
+def by_kind(request, kind, page=None):
12 10
     entries = Entry.objects.filter(kind=kind.id)
13 11
     entries = paginate(queryset=entries, reverse=kind.index_page, page=page)
14 12
     if hasattr(entries, 'content'):
@@ -23,7 +21,7 @@ def by_kind(request, kind, page):
23 21
 
24 22
 
25 23
 @render_to('entries/index.html')
26
-def by_cat(request, slug, page):
24
+def by_cat(request, slug, page=None):
27 25
     def url(page):
28 26
         kwargs = {'slug': slug}
29 27
         if page > 1:

+ 4
- 3
home/urls.py View File

@@ -1,9 +1,10 @@
1
-from django.conf.urls import url
1
+from django.urls import path
2 2
 
3 3
 from . import views
4 4
 
5 5
 app_name = 'home'
6 6
 urlpatterns = [
7
-    url(r'^(?:page/(?P<page>\d+))?$', views.index, name='index'),
8
-    url(r'^robots.txt$', views.robots, name='robots.txt'),
7
+    path('', views.index, name='index'),
8
+    path('page/<int:page>', views.index, name='index'),
9
+    path('robots.txt', views.robots, name='robots.txt'),
9 10
 ]

+ 1
- 1
home/views.py View File

@@ -12,7 +12,7 @@ breadcrumbs.add('home:index', 'home')
12 12
 
13 13
 
14 14
 @render_to('home/index.html')
15
-def index(request, page):
15
+def index(request, page=None):
16 16
     def url(page):
17 17
         kwargs = {'page': page} if page != 1 else {}
18 18
         return reverse('home:index', kwargs=kwargs)

+ 5
- 3
lemoncurry/templatetags/lemoncurry_tags.py View File

@@ -45,7 +45,7 @@ def nav_left(request):
45 45
     items = (MenuItem(
46 46
         label=k.plural,
47 47
         icon=k.icon,
48
-        url=('entries:index', (k.plural,))
48
+        url=('entries:index', (k,))
49 49
     ) for k in kinds.all)
50 50
     return {'items': items, 'request': request}
51 51
 
@@ -55,11 +55,13 @@ def nav_right(request):
55 55
     if request.user.is_authenticated:
56 56
         items = (
57 57
             MenuItem(label='admin', icon='fas fa-cog', url='admin:index'),
58
-            MenuItem(label='log out', icon='fas fa-sign-out-alt', url='lemonauth:logout'),
58
+            MenuItem(label='log out', icon='fas fa-sign-out-alt',
59
+                     url='lemonauth:logout'),
59 60
         )
60 61
     else:
61 62
         items = (
62
-            MenuItem(label='log in', icon='fas fa-sign-in-alt', url='lemonauth:login'),
63
+            MenuItem(label='log in', icon='fas fa-sign-in-alt',
64
+                     url='lemonauth:login'),
63 65
         )
64 66
     return {'items': items, 'request': request}
65 67
 

Loading…
Cancel
Save