Simplify transitional files a bit.
[wolnelektury.git] / src / social / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
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
8
9 from ajaxable.utils import AjaxableFormView
10
11 from catalogue.models import Book
12 from social import forms
13
14
15 # ====================
16 # = Shelf management =
17 # ====================
18
19
20 @require_POST
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)
25
26     book.like(request.user)
27
28     if request.is_ajax():
29         return JsonResponse({"success": True, "msg": "ok", "like": True})
30     else:
31         return redirect(book)
32
33
34 @require_POST
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)
39
40     book.unlike(request.user)
41
42     if request.is_ajax():
43         return JsonResponse({"success": True, "msg": "ok", "like": False})
44     else:
45         return redirect(book)
46
47
48 @login_required
49 def my_shelf(request):
50     return render(request, 'social/my_shelf.html', {
51         'books': Book.tagged.with_any(request.user.tag_set.all())
52     })
53
54
55 class ObjectSetsFormView(AjaxableFormView):
56     form_class = forms.ObjectSetsForm
57     placeholdize = True
58     template = 'social/sets_form.html'
59     ajax_redirect = True
60     POST_login = True
61
62     def get_object(self, request, slug):
63         return get_object_or_404(Book, slug=slug)
64
65     def context_description(self, request, obj):
66         return obj.pretty_title()
67
68     def form_args(self, request, obj):
69         return (obj, request.user), {}