X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/75957f735219259d3b4bc361f80ccd3d7b92a0e9..bde2a16ae2669bea94ec0f23c60213a26d4881f4:/src/social/views.py diff --git a/src/social/views.py b/src/social/views.py index 2d95cfece..d9627eb9e 100644 --- a/src/social/views.py +++ b/src/social/views.py @@ -4,12 +4,16 @@ 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 catalogue.models import Book, Tag +import catalogue.models.tag from social import forms +from wolnelektury.utils import is_ajax # ==================== @@ -19,27 +23,44 @@ from social import forms @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) @@ -47,11 +68,64 @@ def unlike_book(request, slug): @login_required def my_shelf(request): - return render(request, 'social/my_shelf.html', { + if request.EXPERIMENTS['layout'].value: + template_name = 'social/2022/my_shelf.html' + else: + template_name = 'social/my_shelf.html' + return render(request, template_name, { 'books': Book.tagged.with_any(request.user.tag_set.all()) }) +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