1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.shortcuts import render, get_object_or_404, redirect
5 from django.http import HttpResponseForbidden, JsonResponse
6 from django.contrib.auth.decorators import login_required
7 from django.views.decorators.cache import never_cache
8 from django.views.decorators.http import require_POST
9 from django.views.generic.edit import FormView
11 from ajaxable.utils import AjaxableFormView
13 from catalogue.models import Book, Tag
14 import catalogue.models.tag
15 from social import forms
16 from wolnelektury.utils import is_ajax
19 # ====================
20 # = Shelf management =
21 # ====================
25 def like_book(request, slug):
26 if not request.user.is_authenticated:
27 return HttpResponseForbidden('Login required.')
28 book = get_object_or_404(Book, slug=slug)
30 book.like(request.user)
33 return JsonResponse({"success": True, "msg": "ok", "like": True})
38 class AddSetView(FormView):
39 form_class = forms.AddSetForm
40 template_name = 'forms/form_detail.html'
42 def form_valid(self, form):
43 book, tag = form.save(self.request.user)
45 if is_ajax(self.request):
46 return JsonResponse(get_sets_for_book_ids([book.id], self.request.user))
51 class RemoveSetView(AddSetView):
52 form_class = forms.RemoveSetForm
56 def unlike_book(request, slug):
57 if not request.user.is_authenticated:
58 return HttpResponseForbidden('Login required.')
59 book = get_object_or_404(Book, slug=slug)
61 book.unlike(request.user)
64 return JsonResponse({"success": True, "msg": "ok", "like": False})
70 def my_shelf(request):
71 if request.EXPERIMENTS['layout'].value:
72 template_name = 'social/2022/my_shelf.html'
74 template_name = 'social/my_shelf.html'
76 tags = list(request.user.tag_set.all())
77 suggest = [t for t in tags if t.name]
80 return render(request, template_name, {
82 'books': Book.tagged.with_any(tags),
87 def get_sets_for_book_ids(book_ids, user):
89 tagged = catalogue.models.tag.TagRelation.objects.filter(
91 #content_type= # for books,
92 object_id__in=book_ids
93 ).order_by('tag__sort_key')
96 item = data.setdefault(t.object_id, [])
100 "url": t.tag.get_absolute_url(),
112 def my_liked(request):
113 if not request.user.is_authenticated:
114 return JsonResponse({})
116 ids = [int(x) for x in request.GET.get('ids', '').split(',')]
118 return JsonResponse({})
119 return JsonResponse(get_sets_for_book_ids(ids, request.user))
124 def my_tags(request):
125 term = request.GET.get('term', '')
126 tags = Tag.objects.filter(user=request.user).order_by('sort_key')
128 tags = tags.filter(name__icontains=term)
136 class ObjectSetsFormView(AjaxableFormView):
137 form_class = forms.ObjectSetsForm
139 template = 'social/sets_form.html'
143 def get_object(self, request, slug):
144 return get_object_or_404(Book, slug=slug)
146 def context_description(self, request, obj):
147 return obj.pretty_title()
149 def form_args(self, request, obj):
150 return (obj, request.user), {}