From 1a6ec026641532bcc66d29a81503f9a4e7ab1ffa Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Mon, 20 Nov 2017 10:59:03 +1100 Subject: [PATCH] Add basic support for tags on entries :3 cool beans --- entries/migrations/0009_tag.py | 26 ++++++++++++++++++++++++++ entries/migrations/0010_entry_tags.py | 20 ++++++++++++++++++++ entries/models.py | 21 +++++++++++++++++++++ entries/templates/entries/h-entry.html | 18 ++++++++++++++++++ entries/urls.py | 3 +++ entries/views.py | 13 +++++++++++-- 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 entries/migrations/0009_tag.py create mode 100644 entries/migrations/0010_entry_tags.py diff --git a/entries/migrations/0009_tag.py b/entries/migrations/0009_tag.py new file mode 100644 index 0000000..aa61053 --- /dev/null +++ b/entries/migrations/0009_tag.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2017-11-19 23:43 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('entries', '0008_auto_20171116_2116'), + ] + + operations = [ + migrations.CreateModel( + name='Tag', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ('slug', models.CharField(max_length=255, unique=True)), + ], + options={ + 'ordering': ('name',), + }, + ), + ] diff --git a/entries/migrations/0010_entry_tags.py b/entries/migrations/0010_entry_tags.py new file mode 100644 index 0000000..bd36f61 --- /dev/null +++ b/entries/migrations/0010_entry_tags.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2017-11-19 23:46 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('entries', '0009_tag'), + ] + + operations = [ + migrations.AddField( + model_name='entry', + name='tags', + field=models.ManyToManyField(related_name='entries', to='entries.Tag'), + ), + ] diff --git a/entries/models.py b/entries/models.py index 36e0fcb..51ed872 100644 --- a/entries/models.py +++ b/entries/models.py @@ -17,6 +17,25 @@ from lemoncurry import requests ENTRY_KINDS = [(k.id, k.id) for k in kinds.all] +class TagManager(models.Manager): + def from_name(self, name): + tag, created = self.get_or_create(name=name, slug=slugify(name)) + return tag + + +class Tag(models.Model): + objects = TagManager() + name = models.CharField(max_length=255, unique=True) + slug = models.CharField(max_length=255, unique=True) + + @property + def url(self): + return reverse('entries:tagged', args=(self.slug,)) + + class Meta: + ordering = ('name',) + + class EntryManager(models.Manager): def get_queryset(self): qs = super(EntryManager, self).get_queryset() @@ -36,6 +55,8 @@ class Entry(ModelMeta, TimeStampedModel): photo = models.ImageField(blank=True) content = models.TextField() + tags = models.ManyToManyField(Tag, related_name='entries') + in_reply_to = models.CharField(max_length=255, blank=True) like_of = models.CharField(max_length=255, blank=True) repost_of = models.CharField(max_length=255, blank=True) diff --git a/entries/templates/entries/h-entry.html b/entries/templates/entries/h-entry.html index e1aac22..782bff3 100644 --- a/entries/templates/entries/h-entry.html +++ b/entries/templates/entries/h-entry.html @@ -17,6 +17,7 @@ {% if entry.name %}

{{ entry.name }}

{% endif %}
{{ entry.content | markdown }}
+ + + {% if entry.tags.exists %} + + {% endif %} + + {% if entry.syndications.exists %} + + {% endif %} + diff --git a/entries/urls.py b/entries/urls.py index 2fff993..8e481c0 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -15,7 +15,10 @@ app_name = 'entries' urlpatterns = [ url('^atom$', feeds.AtomHomeEntries(), name='atom'), url('^rss$', feeds.RssHomeEntries(), name='rss'), + url('^tags/(?P.+)$', views.tagged, name='tagged'), ] +crumbs.add(prefix('tagged'), parent='home:index') + for k in kinds.all: kind = k.plural id = r'/(?P\d+)' diff --git a/entries/views.py b/entries/views.py index eadba83..912839b 100644 --- a/entries/views.py +++ b/entries/views.py @@ -1,6 +1,6 @@ from annoying.decorators import render_to -from django.shortcuts import redirect -from .models import Entry +from django.shortcuts import get_object_or_404, redirect +from .models import Entry, Tag @render_to('entries/index.html') @@ -14,6 +14,15 @@ def index(request, kind): } +@render_to('entries/index.html') +def tagged(request, slug): + tag = get_object_or_404(Tag, slug=slug) + return { + 'entries': tag.entries.all(), + 'title': '#' + tag.name, + } + + @render_to('entries/entry.html') def entry(request, id, slug=None): entry = Entry.objects.get(pk=id)