X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/174cf7969627acdc1642f2eda012499b218abac8..0a7090f11131631647db366ff87976407e788412:/src/social/views.py?ds=sidebyside diff --git a/src/social/views.py b/src/social/views.py index 89661590e..cff7c8e6a 100644 --- a/src/social/views.py +++ b/src/social/views.py @@ -4,11 +4,14 @@ 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 @@ -32,6 +35,23 @@ def like_book(request, slug): 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: @@ -48,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