from collections import defaultdict from django.contrib.auth.decorators import login_required from django.db.models import Count from django.http import Http404, HttpResponseBadRequest from django.shortcuts import get_object_or_404, redirect from django.urls import reverse from django.views.decorators.http import require_POST from . import fans from .models import Chapter, Entry, FanNotice, FanReaction, Page ICONS = {1: 'thumb-up', 2: 'sage', -1: 'thumb-down', 3: 'wow', 4: 'sad', 5: 'laugh'} ORDER = [1, 2, -1, 3, 4, 5] def reaction_groups(request, kind, objects): field = f'{kind}_id' ids = [obj.pk for obj in objects] if not ids: return {} reactions = FanReaction.objects.filter(**{f'{field}__in': ids}) counts = defaultdict(dict) for row in reactions.values_list(field, 'value').annotate(count=Count('pk')): counts[row[0]][row[1]] = row[2] current = dict(reactions.filter(user=request.user).values_list(field, 'value')) if request.user.is_authenticated else {} return {pk: {'kind': kind, 'pk': pk, 'options': [ {'value': value, 'label': FanReaction.Value(value).label, 'icon': ICONS[value], 'count': counts[pk].get(value, 0), 'active': current.get(pk) == value} for value in ORDER ]} for pk in ids} def reaction_group(request, kind, obj): return reaction_groups(request, kind, [obj])[obj.pk] @require_POST @login_required def react(request, kind, pk): if kind not in FanReaction.TARGETS: raise Http404 chapter = entry = comment = None anchor = 'discussion' if kind in ('chapter', 'chapter_cover'): qs = fans.live(Chapter) if kind == 'chapter_cover': qs = qs.exclude(cover='') anchor = 'chapter-cover' target = chapter = get_object_or_404(qs, pk=pk) elif kind in ('entry', 'entry_cover'): qs = fans.live(Entry) if kind == 'entry_cover': qs = qs.exclude(art='') anchor = 'post-cover' target = entry = get_object_or_404(qs, pk=pk) elif kind == 'page': from .paypal import entitled_chapters target = get_object_or_404(Page.objects.select_related('chapter').filter( chapter__in=entitled_chapters(request.user).filter(publisher_url='')), pk=pk) chapter = target.chapter anchor = f'page-{target.number}' else: from django.db.models import Q comment = target = get_object_or_404(fans.checked_comments().select_related('chapter', 'entry').filter( Q(chapter__in=fans.live(Chapter)) | Q(entry__in=fans.live(Entry).filter(allow_comments=True))), pk=pk) chapter, entry = comment.chapter, comment.entry anchor = f'comment-{comment.pk}' requested = request.POST.get('value') if requested not in {str(i) for i in [0, *ORDER]}: return HttpResponseBadRequest('Choose a reaction.') value = int(requested) match = {'user': request.user, kind: target} if value == 0: FanReaction.objects.filter(**match).delete() else: FanReaction.objects.update_or_create(**match, defaults={'value': value}) if comment and value == FanReaction.Value.LIKE: fans.notify(request.user, comment, FanNotice.Kind.LIKE) path = fans.destination(chapter, entry) if chapter and not chapter.publisher_url and kind in ('page', 'chapter_cover', 'chapter', 'comment'): if kind in ('page', 'chapter_cover') or request.POST.get('surface') == 'reader': path = reverse('chapter', args=[chapter.number]) return redirect(path + '#' + anchor)