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.
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
10 from ajaxable.utils import AjaxableFormView
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
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)
29 if not likes(request.user, book):
30 tag = get_set(request.user, '')
31 set_sets(request.user, book, [tag])
34 return JsonResponse({"success": True, "msg": "ok", "like": True})
40 def my_shelf(request):
41 return render(request, 'social/my_shelf.html', {
42 'books': Book.tagged.with_any(request.user.tag_set.all())
46 class ObjectSetsFormView(AjaxableFormView):
47 form_class = forms.ObjectSetsForm
49 template = 'social/sets_form.html'
53 def get_object(self, request, slug):
54 return get_object_or_404(Book, slug=slug)
56 def context_description(self, request, obj):
57 return obj.pretty_title()
59 def form_args(self, request, obj):
60 return (obj, request.user), {}
64 def unlike_book(request, slug):
65 if not request.user.is_authenticated():
66 return HttpResponseForbidden('Login required.')
67 book = get_object_or_404(Book, slug=slug)
68 if likes(request.user, book):
69 set_sets(request.user, book, [])
72 return JsonResponse({"success": True, "msg": "ok", "like": False})
78 def cite(request, pk, main=False):
79 cite = get_object_or_404(Cite, pk=pk)
80 return render(request, 'social/cite_promo.html', {
86 @ssi_included(use_lang=False)
87 def cite_info(request, pk):
88 cite = get_object_or_404(Cite, pk=pk)
89 return render(request, 'social/cite_info.html', {