update librarian
[wolnelektury.git] / src / social / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.shortcuts import render, get_object_or_404, redirect
6 from django.http import HttpResponseForbidden, JsonResponse
7 from django.contrib.auth.decorators import login_required
8 from django.views.decorators.http import require_POST
9
10 from ajaxable.utils import AjaxableFormView
11
12 from catalogue.models import Book
13 from ssify import ssi_included
14 from social import forms
15 from .models import Cite
16
17
18 # ====================
19 # = Shelf management =
20 # ====================
21
22
23 @require_POST
24 def like_book(request, slug):
25     if not request.user.is_authenticated():
26         return HttpResponseForbidden('Login required.')
27     book = get_object_or_404(Book, slug=slug)
28
29     book.like(request.user)
30
31     if request.is_ajax():
32         return JsonResponse({"success": True, "msg": "ok", "like": True})
33     else:
34         return redirect(book)
35
36
37 @require_POST
38 def unlike_book(request, slug):
39     if not request.user.is_authenticated():
40         return HttpResponseForbidden('Login required.')
41     book = get_object_or_404(Book, slug=slug)
42
43     book.unlike(request.user)
44
45     if request.is_ajax():
46         return JsonResponse({"success": True, "msg": "ok", "like": False})
47     else:
48         return redirect(book)
49
50
51 @login_required
52 def my_shelf(request):
53     return render(request, 'social/my_shelf.html', {
54         'books': Book.tagged.with_any(request.user.tag_set.all())
55     })
56
57
58 class ObjectSetsFormView(AjaxableFormView):
59     form_class = forms.ObjectSetsForm
60     placeholdize = True
61     template = 'social/sets_form.html'
62     ajax_redirect = True
63     POST_login = True
64
65     def get_object(self, request, slug):
66         return get_object_or_404(Book, slug=slug)
67
68     def context_description(self, request, obj):
69         return obj.pretty_title()
70
71     def form_args(self, request, obj):
72         return (obj, request.user), {}
73
74
75 @ssi_included
76 def cite(request, pk, main=False):
77     cite = get_object_or_404(Cite, pk=pk)
78     return render(request, 'social/cite_promo.html', {
79         'main': main,
80         'cite': cite,
81     })
82
83
84 @ssi_included(use_lang=False)
85 def cite_info(request, pk):
86     cite = get_object_or_404(Cite, pk=pk)
87     return render(request, 'social/cite_info.html', {
88         'cite': cite,
89     })