9087d7bba8f0b01de3f7b5305ea855bb6f7d5a75
[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.cache import never_cache
8 from django.views.decorators.http import require_POST
9 from django.views.generic.edit import FormView
10
11 from catalogue.models import Book, Tag
12 import catalogue.models.tag
13 from social import forms
14 from wolnelektury.utils import is_ajax
15
16
17 # ====================
18 # = Shelf management =
19 # ====================
20
21
22 @require_POST
23 def like_book(request, slug):
24     if not request.user.is_authenticated:
25         return HttpResponseForbidden('Login required.')
26     book = get_object_or_404(Book, slug=slug)
27
28     book.like(request.user)
29
30     if is_ajax(request):
31         return JsonResponse({"success": True, "msg": "ok", "like": True})
32     else:
33         return redirect(book)
34
35
36 class AddSetView(FormView):
37     form_class = forms.AddSetForm
38     template_name = 'forms/form_detail.html'
39
40     def form_valid(self, form):
41         book, tag = form.save(self.request.user)
42
43         if is_ajax(self.request):
44             return JsonResponse(get_sets_for_book_ids([book.id], self.request.user))
45         else:
46             return redirect(book)
47
48
49 class RemoveSetView(AddSetView):
50     form_class = forms.RemoveSetForm
51
52
53 @require_POST
54 def unlike_book(request, slug):
55     if not request.user.is_authenticated:
56         return HttpResponseForbidden('Login required.')
57     book = get_object_or_404(Book, slug=slug)
58
59     book.unlike(request.user)
60
61     if is_ajax(request):
62         return JsonResponse({"success": True, "msg": "ok", "like": False})
63     else:
64         return redirect(book)
65
66
67 @login_required
68 def my_shelf(request):
69     template_name = 'social/2022/my_shelf.html'
70     tags = list(request.user.tag_set.all())
71     suggest = [t for t in tags if t.name]
72     print(suggest)
73         
74     return render(request, template_name, {
75         'tags': tags,
76         'books': Book.tagged.with_any(tags),
77         'suggest': suggest,
78     })
79
80
81 def get_sets_for_book_ids(book_ids, user):
82     data = {}
83     tagged = catalogue.models.tag.TagRelation.objects.filter(
84         tag__user=user,
85         #content_type= # for books,
86         object_id__in=book_ids
87     ).order_by('tag__sort_key')
88     for t in tagged:
89         # related?
90         item = data.setdefault(t.object_id, [])
91         if t.tag.name:
92             item.append({
93                 "slug": t.tag.slug,
94                 "url": t.tag.get_absolute_url(),
95                 "name": t.tag.name,
96             })
97     for b in book_ids:
98         if b not in data:
99             data[b] = None
100     return data
101     
102     
103
104
105 @never_cache
106 def my_liked(request):
107     if not request.user.is_authenticated:
108         return JsonResponse({})
109     try:
110         ids = [int(x) for x in request.GET.get('ids', '').split(',')]
111     except:
112         return JsonResponse({})
113     return JsonResponse(get_sets_for_book_ids(ids, request.user))
114
115
116 @never_cache
117 @login_required
118 def my_tags(request):
119     term = request.GET.get('term', '')
120     tags =             Tag.objects.filter(user=request.user).order_by('sort_key')
121     if term:
122         tags = tags.filter(name__icontains=term)
123     return JsonResponse(
124         [
125             t.name for t in tags
126         ], safe=False
127     )