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.http import require_POST
9 from ajaxable.utils import AjaxableFormView
11 from catalogue.models import Book
12 from social import forms
15 # ====================
16 # = Shelf management =
17 # ====================
21 def like_book(request, slug):
22 if not request.user.is_authenticated():
23 return HttpResponseForbidden('Login required.')
24 book = get_object_or_404(Book, slug=slug)
26 book.like(request.user)
29 return JsonResponse({"success": True, "msg": "ok", "like": True})
35 def unlike_book(request, slug):
36 if not request.user.is_authenticated():
37 return HttpResponseForbidden('Login required.')
38 book = get_object_or_404(Book, slug=slug)
40 book.unlike(request.user)
43 return JsonResponse({"success": True, "msg": "ok", "like": False})
49 def my_shelf(request):
50 return render(request, 'social/my_shelf.html', {
51 'books': Book.tagged.with_any(request.user.tag_set.all())
55 class ObjectSetsFormView(AjaxableFormView):
56 form_class = forms.ObjectSetsForm
58 template = 'social/sets_form.html'
62 def get_object(self, request, slug):
63 return get_object_or_404(Book, slug=slug)
65 def context_description(self, request, obj):
66 return obj.pretty_title()
68 def form_args(self, request, obj):
69 return (obj, request.user), {}