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