lemoncurry/entries/urls.py

59 lines
1.6 KiB
Python
Raw Normal View History

from django.urls import path, register_converter, reverse
from . import kinds
from .views import feeds, lists, perma
from lemoncurry import breadcrumbs as crumbs
2023-08-10 02:52:37 -04:00
register_converter(kinds.EntryKindConverter, "kind")
def to_pat(*args):
2023-08-10 02:52:37 -04:00
return "^{0}$".format("".join(args))
def prefix(route):
2023-08-10 02:52:37 -04:00
return app_name + ":" + route
2023-08-10 02:52:37 -04:00
id = r"/(?P<id>\d+)"
kind = r"(?P<kind>{0})".format("|".join(k.plural for k in kinds.all))
page = r"(?:/page/(?P<page>\d+))?"
slug = r"/(?P<slug>[^/]+)"
2023-08-10 02:52:37 -04:00
slug_opt = "(?:" + slug + ")?"
2023-08-10 02:52:37 -04:00
app_name = "entries"
urlpatterns = (
2023-08-10 02:52:37 -04:00
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>", perma.entry, name="entry"),
path("<kind:kind>/<int:id>/<slug:slug>", perma.entry, name="entry"),
)
class IndexCrumb(crumbs.Crumb):
def __init__(self):
2023-08-10 02:52:37 -04:00
super().__init__(prefix("index"), parent="home:index")
@property
def kind(self):
2023-08-10 02:52:37 -04:00
return self.match.kwargs["kind"]
@property
def label(self):
return self.kind.plural
@property
def url(self):
2023-08-10 02:52:37 -04:00
return reverse(prefix("index"), kwargs={"kind": self.kind})
2023-08-10 02:52:37 -04:00
crumbs.add(prefix("cat"), parent="home:index")
crumbs.add(IndexCrumb())
2023-08-10 02:52:37 -04:00
crumbs.add(prefix("entry"), parent=prefix("index"))