Rudimentary but fully-functional entry permalink pages :3

This commit is contained in:
Danielle McLean 2017-10-25 12:31:08 +11:00
parent d267df337f
commit 2b6691f8a5
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
13 changed files with 133 additions and 73 deletions

View file

@ -1,13 +1,22 @@
class Note:
class Entry:
fields = ()
@classmethod
def has(cls, field):
return field in cls.fields
class Note(Entry):
id = 'note'
icon = 'fa fa-paper-plane'
plural = 'notes'
class Article:
class Article(Entry):
id = 'article'
icon = 'fa fa-file-text'
plural = 'articles'
fields = ('slug', 'name')
all = (Note, Article)

View file

@ -1,5 +1,7 @@
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
from slugify import slugify
from . import kinds
ENTRY_KINDS = [(k.id, k.__name__) for k in kinds.all]
@ -28,6 +30,20 @@ class Entry(models.Model):
content=self.content
)
@property
def url(self):
kind = kinds.from_id[self.kind]
route = 'entries:{kind}_entry'.format(kind=kind.plural)
args = [self.id]
if kind.has('slug'):
route += '_slug'
args.append(self.slug)
return reverse(route, args=args)
@property
def slug(self):
return slugify(self.name)
class Meta:
verbose_name_plural = 'entries'
ordering = ['-published']

View file

@ -1,24 +1,4 @@
{% load humanize %}<article class="card h-entry">
<div class="card-body">
{% if entry.name %}<h4 class="card-title p-name">{{ entry.name }}</h4>{% endif %}
<div class="e-content{% if not entry.name %} p-name{% endif %}">{{ entry.content }}</div>
</div>
<div class="card-footer">
<a class="p-author h-card" href="{{ entry.author.url }}">
<img class="u-photo" src="{{ entry.author.avatar.url }}" />
{{ entry.author.first_name }} {{ entry.author.last_name }}
</a>
<a class="u-url" href="{{ entry.url }}">
<time class="dt-published" datetime="{{ entry.published.isoformat }}">
<i class="fa fa-calendar"></i>
{{ entry.published | naturaltime }}
</time>
</a>
{% if entry.updated != entry.published %}
<time class="dt-updated" datetime="{{ entry.updated.isoformat }}">
<i class="fa fa-pencil"></i>
{{ entry.updated | naturaltime }}
</time>
{% endif %}
</div>
</article>
{% extends 'lemoncurry/layout.html' %}
{% block main %}
{% include 'entries/h-entry.html' %}
{% endblock %}

View file

@ -0,0 +1,24 @@
{% load humanize %}<article class="card h-entry">
<div class="card-body">
{% if entry.name %}<h4 class="card-title p-name">{{ entry.name }}</h4>{% endif %}
<div class="e-content{% if not entry.name %} p-name{% endif %}">{{ entry.content }}</div>
</div>
<div class="card-footer">
<a class="p-author h-card" href="{{ entry.author.url }}">
<img class="u-photo" src="{{ entry.author.avatar.url }}" />
{{ entry.author.first_name }} {{ entry.author.last_name }}
</a>
<a class="u-url" href="{{ entry.url }}">
<time class="dt-published" datetime="{{ entry.published.isoformat }}">
<i class="fa fa-calendar"></i>
{{ entry.published | naturaltime }}
</time>
</a>
{% if entry.updated != entry.published %}
<time class="dt-updated" datetime="{{ entry.updated.isoformat }}">
<i class="fa fa-pencil"></i>
{{ entry.updated | naturaltime }}
</time>
{% endif %}
</div>
</article>

View file

@ -4,7 +4,7 @@
<ol class="list-unstyled">
{% for entry in entries %}
<li>
{% include 'entries/entry.html' %}
{% include 'entries/h-entry.html' %}
</li>
{% endfor %}
</ol>

View file

@ -6,7 +6,19 @@ app_name = 'entries'
urlpatterns = []
for k in kinds.all:
index = k.plural + '_index'
urlpatterns.extend((
url(k.plural, views.index, name=index, kwargs={'kind': k}),
))
urlpatterns.append(
url(r'^{k}$'.format(k=k.plural), views.index, name=index, kwargs={'kind': k})
)
breadcrumbs.add(app_name + ':' + index, label=k.plural, parent='home:index')
entry = k.plural + '_entry'
pattern = r'^{k}/(?P<id>\d+)'.format(k=k.plural)
urlpatterns.append(
url(pattern + '$', views.entry, name=entry)
)
breadcrumbs.add(app_name + ':' + entry, parent=app_name + ':' + index)
if k.has('slug'):
urlpatterns.append(
url(pattern + r'/(?P<slug>.+)$', views.entry, name=entry + '_slug')
)
breadcrumbs.add(app_name + ':' + entry + '_slug', parent=app_name + ':' + index)

View file

@ -8,3 +8,11 @@ def index(request, kind):
'entries': entries,
'title': kind.plural
})
def entry(request, id, slug=None):
entry = Entry.objects.get(pk=id)
return render(request, 'entries/entry.html', {
'entry': entry,
'title': entry.name or entry.content
})