option to turn off ssify just for api + some optimizations
[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 from social.utils import get_set, likes, set_sets
17
18
19 # ====================
20 # = Shelf management =
21 # ====================
22
23
24 @require_POST
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)
29     if not likes(request.user, book):
30         tag = get_set(request.user, '')
31         set_sets(request.user, book, [tag])
32
33     if request.is_ajax():
34         return JsonResponse({"success": True, "msg": "ok", "like": True})
35     else:
36         return redirect(book)
37
38
39 @login_required
40 def my_shelf(request):
41     books = Book.tagged.with_any(request.user.tag_set.all())
42     return render(request, 'social/my_shelf.html', locals())
43
44
45 class ObjectSetsFormView(AjaxableFormView):
46     form_class = forms.ObjectSetsForm
47     placeholdize = True
48     template = 'social/sets_form.html'
49     ajax_redirect = True
50     POST_login = True
51
52     def get_object(self, request, slug):
53         return get_object_or_404(Book, slug=slug)
54
55     def context_description(self, request, obj):
56         return obj.pretty_title()
57
58     def form_args(self, request, obj):
59         return (obj, request.user), {}
60
61
62 @require_POST
63 def unlike_book(request, slug):
64     if not request.user.is_authenticated():
65         return HttpResponseForbidden('Login required.')
66     book = get_object_or_404(Book, slug=slug)
67     if likes(request.user, book):
68         set_sets(request.user, book, [])
69
70     if request.is_ajax():
71         return JsonResponse({"success": True, "msg": "ok", "like": False})
72     else:
73         return redirect(book)
74
75
76 @ssi_included
77 def cite(request, pk, main=False):
78     cite = get_object_or_404(Cite, pk=pk)
79     return render(request, 'social/cite_promo.html', {
80         'main': main,
81         'cite': cite,
82     })
83
84
85 @ssi_included(use_lang=False)
86 def cite_info(request, pk):
87     cite = get_object_or_404(Cite, pk=pk)
88     return render(request, 'social/cite_info.html', {
89         'cite': cite,
90     })