X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/141f3bea77fdb337069c198ffeee870696f4554e..ec546ced288878c12c9977f7b2218f76c34d352f:/src/social/views.py diff --git a/src/social/views.py b/src/social/views.py index 757b5a26d..cff7c8e6a 100644 --- a/src/social/views.py +++ b/src/social/views.py @@ -1,18 +1,19 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.shortcuts import render, get_object_or_404, redirect from django.http import HttpResponseForbidden, JsonResponse from django.contrib.auth.decorators import login_required +from django.views.decorators.cache import never_cache from django.views.decorators.http import require_POST +from django.views.generic.edit import FormView from ajaxable.utils import AjaxableFormView -from catalogue.models import Book -from ssify import ssi_included +from catalogue.models import Book, Tag +import catalogue.models.tag from social import forms -from .models import Cite +from wolnelektury.utils import is_ajax # ==================== @@ -22,27 +23,44 @@ from .models import Cite @require_POST def like_book(request, slug): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return HttpResponseForbidden('Login required.') book = get_object_or_404(Book, slug=slug) book.like(request.user) - if request.is_ajax(): + if is_ajax(request): return JsonResponse({"success": True, "msg": "ok", "like": True}) else: return redirect(book) +class AddSetView(FormView): + form_class = forms.AddSetForm + template_name = 'forms/form_detail.html' + + def form_valid(self, form): + book, tag = form.save(self.request.user) + + if is_ajax(self.request): + return JsonResponse(get_sets_for_book_ids([book.id], self.request.user)) + else: + return redirect(book) + + +class RemoveSetView(AddSetView): + form_class = forms.RemoveSetForm + + @require_POST def unlike_book(request, slug): - if not request.user.is_authenticated(): + if not request.user.is_authenticated: return HttpResponseForbidden('Login required.') book = get_object_or_404(Book, slug=slug) book.unlike(request.user) - if request.is_ajax(): + if is_ajax(request): return JsonResponse({"success": True, "msg": "ok", "like": False}) else: return redirect(book) @@ -50,11 +68,71 @@ def unlike_book(request, slug): @login_required def my_shelf(request): - return render(request, 'social/my_shelf.html', { - 'books': Book.tagged.with_any(request.user.tag_set.all()) + if request.EXPERIMENTS['layout'].value: + template_name = 'social/2022/my_shelf.html' + else: + template_name = 'social/my_shelf.html' + + tags = list(request.user.tag_set.all()) + suggest = [t for t in tags if t.name] + print(suggest) + + return render(request, template_name, { + 'tags': tags, + 'books': Book.tagged.with_any(tags), + 'suggest': suggest, }) +def get_sets_for_book_ids(book_ids, user): + data = {} + tagged = catalogue.models.tag.TagRelation.objects.filter( + tag__user=user, + #content_type= # for books, + object_id__in=book_ids + ).order_by('tag__sort_key') + for t in tagged: + # related? + item = data.setdefault(t.object_id, []) + if t.tag.name: + item.append({ + "slug": t.tag.slug, + "url": t.tag.get_absolute_url(), + "name": t.tag.name, + }) + for b in book_ids: + if b not in data: + data[b] = None + return data + + + + +@never_cache +def my_liked(request): + if not request.user.is_authenticated: + return JsonResponse({}) + try: + ids = [int(x) for x in request.GET.get('ids', '').split(',')] + except: + return JsonResponse({}) + return JsonResponse(get_sets_for_book_ids(ids, request.user)) + + +@never_cache +@login_required +def my_tags(request): + term = request.GET.get('term', '') + tags = Tag.objects.filter(user=request.user).order_by('sort_key') + if term: + tags = tags.filter(name__icontains=term) + return JsonResponse( + [ + t.name for t in tags + ], safe=False + ) + + class ObjectSetsFormView(AjaxableFormView): form_class = forms.ObjectSetsForm placeholdize = True @@ -70,20 +148,3 @@ class ObjectSetsFormView(AjaxableFormView): def form_args(self, request, obj): return (obj, request.user), {} - - -@ssi_included -def cite(request, pk, main=False): - cite = get_object_or_404(Cite, pk=pk) - return render(request, 'social/cite_promo.html', { - 'main': main, - 'cite': cite, - }) - - -@ssi_included(use_lang=False) -def cite_info(request, pk): - cite = get_object_or_404(Cite, pk=pk) - return render(request, 'social/cite_info.html', { - 'cite': cite, - })