moviewyrm/bookwyrm/views/tag.py

71 lines
2.2 KiB
Python
Raw Normal View History

2021-01-13 18:24:24 +00:00
''' tagging views'''
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import models
from bookwyrm.activitypub import ActivitypubResponse
from .helpers import is_api_request
# pylint: disable= no-self-use
class Tag(View):
''' tag page '''
def get(self, request, tag_id):
''' see books related to a tag '''
2021-02-24 01:18:25 +00:00
tag_obj = get_object_or_404(models.Tag, identifier=tag_id)
2021-01-13 18:24:24 +00:00
if is_api_request(request):
2021-02-24 01:18:25 +00:00
return ActivitypubResponse(
tag_obj.to_activity(**request.GET))
2021-01-13 18:24:24 +00:00
books = models.Edition.objects.filter(
usertag__tag__identifier=tag_id
).distinct()
data = {
'books': books,
'tag': tag_obj,
}
return TemplateResponse(request, 'tag.html', data)
@method_decorator(login_required, name='dispatch')
class AddTag(View):
''' add a tag to a book '''
def post(self, request):
''' tag a book '''
# I'm not using a form here because sometimes "name" is sent as a hidden
# field which doesn't validate
name = request.POST.get('name')
book_id = request.POST.get('book')
book = get_object_or_404(models.Edition, id=book_id)
2021-02-04 21:21:55 +00:00
tag_obj, _ = models.Tag.objects.get_or_create(
2021-01-13 18:24:24 +00:00
name=name,
)
2021-02-04 21:21:55 +00:00
models.UserTag.objects.get_or_create(
2021-01-13 18:24:24 +00:00
user=request.user,
book=book,
tag=tag_obj,
)
return redirect('/book/%s' % book_id)
@method_decorator(login_required, name='dispatch')
class RemoveTag(View):
''' remove a user's tag from a book '''
def post(self, request):
''' untag a book '''
name = request.POST.get('name')
tag_obj = get_object_or_404(models.Tag, name=name)
book_id = request.POST.get('book')
book = get_object_or_404(models.Edition, id=book_id)
user_tag = get_object_or_404(
models.UserTag, tag=tag_obj, book=book, user=request.user)
user_tag.delete()
return redirect('/book/%s' % book_id)