Adds tag pages

This commit is contained in:
Mouse Reeve 2020-02-21 09:10:27 -08:00
parent bc4e65ae81
commit 375ff9365f
7 changed files with 89 additions and 3 deletions

View file

@ -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),
]

View file

@ -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),
),
]

View file

@ -3,6 +3,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models from django.db import models
from django.dispatch import receiver from django.dispatch import receiver
from model_utils.managers import InheritanceManager from model_utils.managers import InheritanceManager
import re
from fedireads.utils.models import FedireadsModel from fedireads.utils.models import FedireadsModel
@ -60,7 +61,14 @@ class Tag(FedireadsModel):
''' freeform tags for books ''' ''' freeform tags for books '''
user = models.ForeignKey('User', on_delete=models.PROTECT) user = models.ForeignKey('User', on_delete=models.PROTECT)
book = models.ForeignKey('Book', 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: class Meta:
unique_together = ('user', 'book', 'name') unique_together = ('user', 'book', 'name')

View file

@ -1,5 +1,5 @@
<div class="tag"> <div class="tag">
{{ tag.name }} <a href="/tag/{{ tag.identifier }}">{{ tag.name }}</a>
{% if tag.name in user_tags %} {% if tag.name in user_tags %}
<form class="tag-form" name="tag" action="/untag/" method="post"> <form class="tag-form" name="tag" action="/untag/" method="post">
{% csrf_token %} {% csrf_token %}

View file

@ -0,0 +1,16 @@
{% extends 'layout.html' %}
{% load fr_display %}
{% block content %}
<div id="content">
<div>
<h2>Books tagged "{{ tag.name }}"</h2>
{% for book in books.all %}
<div class="book-preview">
{% include 'snippets/book.html' with book=book size=large %}
</div>
{% endfor %}
</div>
</div>
{% endblock %}

View file

@ -48,6 +48,7 @@ urlpatterns = [
re_path(r'^user/(?P<username>\w+)/edit/?$', views.user_profile_edit), re_path(r'^user/(?P<username>\w+)/edit/?$', views.user_profile_edit),
re_path(r'^book/(?P<book_identifier>\w+)/?$', views.book_page), re_path(r'^book/(?P<book_identifier>\w+)/?$', views.book_page),
re_path(r'^author/(?P<author_identifier>\w+)/?$', views.author_page), re_path(r'^author/(?P<author_identifier>\w+)/?$', views.author_page),
re_path(r'^tag/(?P<tag_id>[\w-]+)/?$', views.tag_page),
# internal action endpoints # internal action endpoints
re_path(r'^review/?$', views.review), re_path(r'^review/?$', views.review),

View file

@ -205,7 +205,7 @@ def book_page(request, book_identifier):
tags = models.Tag.objects.filter( tags = models.Tag.objects.filter(
book=book book=book
).values( ).values(
'book', 'name' 'book', 'name', 'identifier'
).distinct().all() ).distinct().all()
user_tags = models.Tag.objects.filter( user_tags = models.Tag.objects.filter(
book=book, user=request.user book=book, user=request.user
@ -241,6 +241,17 @@ def author_page(request, author_identifier):
return TemplateResponse(request, 'author.html', data) 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 @login_required
def shelve(request, username, shelf_id, book_id, reshelve=True): def shelve(request, username, shelf_id, book_id, reshelve=True):
''' put a book on a user's shelf ''' ''' put a book on a user's shelf '''