From 375ff9365fc87cf49ffe1b2879302899d4731a90 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 21 Feb 2020 09:10:27 -0800 Subject: [PATCH] Adds tag pages --- .../migrations/0005_auto_20200221_1645.py | 32 +++++++++++++++++++ .../migrations/0006_auto_20200221_1702.py | 18 +++++++++++ fedireads/models/activity.py | 10 +++++- fedireads/templates/snippets/tag.html | 2 +- fedireads/templates/tag.html | 16 ++++++++++ fedireads/urls.py | 1 + fedireads/views.py | 13 +++++++- 7 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 fedireads/migrations/0005_auto_20200221_1645.py create mode 100644 fedireads/migrations/0006_auto_20200221_1702.py create mode 100644 fedireads/templates/tag.html diff --git a/fedireads/migrations/0005_auto_20200221_1645.py b/fedireads/migrations/0005_auto_20200221_1645.py new file mode 100644 index 00000000..6682d12a --- /dev/null +++ b/fedireads/migrations/0005_auto_20200221_1645.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.3 on 2020-02-21 16:45 +import re +from django.db import migrations, models + + +def populate_identifiers(app_registry, schema_editor): + db_alias = schema_editor.connection.alias + tags = app_registry.get_model('fedireads', 'Tag') + for tag in tags.objects.using(db_alias): + tag.identifier = re.sub(r'\W+', '-', tag.name).lower() + tag.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('fedireads', '0004_tag'), + ] + + operations = [ + migrations.AddField( + model_name='tag', + name='identifier', + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name='tag', + name='name', + field=models.CharField(max_length=100), + ), + migrations.RunPython(populate_identifiers), + ] diff --git a/fedireads/migrations/0006_auto_20200221_1702.py b/fedireads/migrations/0006_auto_20200221_1702.py new file mode 100644 index 00000000..9b02a43f --- /dev/null +++ b/fedireads/migrations/0006_auto_20200221_1702.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.3 on 2020-02-21 17:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fedireads', '0005_auto_20200221_1645'), + ] + + operations = [ + migrations.AlterField( + model_name='tag', + name='identifier', + field=models.CharField(max_length=100), + ), + ] diff --git a/fedireads/models/activity.py b/fedireads/models/activity.py index bff660ad..4e3daf04 100644 --- a/fedireads/models/activity.py +++ b/fedireads/models/activity.py @@ -3,6 +3,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django.dispatch import receiver from model_utils.managers import InheritanceManager +import re from fedireads.utils.models import FedireadsModel @@ -60,7 +61,14 @@ class Tag(FedireadsModel): ''' freeform tags for books ''' user = models.ForeignKey('User', on_delete=models.PROTECT) book = models.ForeignKey('Book', on_delete=models.PROTECT) - name = models.CharField(max_length=140) + name = models.CharField(max_length=100) + identifier = models.CharField(max_length=100) + + def save(self, *args, **kwargs): + if not self.id: + # add identifiers to new tags + self.identifier = re.sub(r'\W+', '-', self.name).lower() + super().save(*args, **kwargs) class Meta: unique_together = ('user', 'book', 'name') diff --git a/fedireads/templates/snippets/tag.html b/fedireads/templates/snippets/tag.html index c3465b5e..e298039c 100644 --- a/fedireads/templates/snippets/tag.html +++ b/fedireads/templates/snippets/tag.html @@ -1,5 +1,5 @@
- {{ tag.name }} + {{ tag.name }} {% if tag.name in user_tags %}
{% csrf_token %} diff --git a/fedireads/templates/tag.html b/fedireads/templates/tag.html new file mode 100644 index 00000000..39887f91 --- /dev/null +++ b/fedireads/templates/tag.html @@ -0,0 +1,16 @@ +{% extends 'layout.html' %} +{% load fr_display %} +{% block content %} +
+
+

Books tagged "{{ tag.name }}"

+ {% for book in books.all %} +
+ {% include 'snippets/book.html' with book=book size=large %} +
+ {% endfor %} +
+
+{% endblock %} + + diff --git a/fedireads/urls.py b/fedireads/urls.py index 1489ac1d..4d552ac3 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -48,6 +48,7 @@ urlpatterns = [ re_path(r'^user/(?P\w+)/edit/?$', views.user_profile_edit), re_path(r'^book/(?P\w+)/?$', views.book_page), re_path(r'^author/(?P\w+)/?$', views.author_page), + re_path(r'^tag/(?P[\w-]+)/?$', views.tag_page), # internal action endpoints re_path(r'^review/?$', views.review), diff --git a/fedireads/views.py b/fedireads/views.py index 94ba36f9..f0c9c179 100644 --- a/fedireads/views.py +++ b/fedireads/views.py @@ -205,7 +205,7 @@ def book_page(request, book_identifier): tags = models.Tag.objects.filter( book=book ).values( - 'book', 'name' + 'book', 'name', 'identifier' ).distinct().all() user_tags = models.Tag.objects.filter( book=book, user=request.user @@ -241,6 +241,17 @@ def author_page(request, author_identifier): return TemplateResponse(request, 'author.html', data) +def tag_page(request, tag_id): + ''' books related to a tag ''' + tag = models.Tag.objects.filter(identifier=tag_id).first() + books = models.Book.objects.filter(tag=tag).all() + data = { + 'books': books, + 'tag': tag, + } + return TemplateResponse(request, 'tag.html', data) + + @login_required def shelve(request, username, shelf_id, book_id, reshelve=True): ''' put a book on a user's shelf '''