class Group(View): """group page""" # pylint: disable=unused-argument def get(self, request, group_id, slug=None): """display a group""" group = get_object_or_404(models.Group, id=group_id) group.raise_visible_to_user(request.user) if redirect_local_path := maybe_redirect_local_path(request, group): return redirect_local_path lists = (models.List.privacy_filter( request.user).filter(group=group).order_by("-updated_date")) data = { "group": group, "lists": lists, "group_form": forms.GroupForm(instance=group, auto_id="group_form_id_%s"), "list_form": forms.ListForm(), "path": "/group", } return TemplateResponse(request, "groups/group.html", data)
def post(self, request, list_id): ''' edit a list ''' book_list = get_object_or_404(models.List, id=list_id) form = forms.ListForm(request.POST, instance=book_list) if not form.is_valid(): return redirect('list', book_list.id) book_list = form.save() return redirect(book_list.local_path)
def post(self, request): ''' create a book_list ''' form = forms.ListForm(request.POST) if not form.is_valid(): return redirect('lists') book_list = form.save() return redirect(book_list.local_path)
def post(self, request): """ create a book_list """ form = forms.ListForm(request.POST) if not form.is_valid(): return redirect("lists") book_list = form.save() return redirect(book_list.local_path)
def get(self, request): """display a book list""" lists = ListsStream().get_list_stream(request.user) paginated = Paginator(lists, 12) data = { "lists": paginated.get_page(request.GET.get("page")), "list_form": forms.ListForm(), "path": "/list", } return TemplateResponse(request, "lists/lists.html", data)
def get(self, request, list_id): """display a pending list""" book_list = get_object_or_404(models.List, id=list_id) book_list.raise_not_editable(request.user) data = { "list": book_list, "pending": book_list.listitem_set.filter(approved=False), "list_form": forms.ListForm(instance=book_list), } return TemplateResponse(request, "lists/curate.html", data)
def post(self, request): """create a book_list""" form = forms.ListForm(request.POST) if not form.is_valid(): return redirect("lists") book_list = form.save() # list should not have a group if it is not group curated if not book_list.curation == "group": book_list.group = None book_list.save(broadcast=False) return redirect(book_list.local_path)
def get(self, request): """display book lists""" # hide lists with no approved books lists = request.user.saved_lists.order_by("-updated_date") paginated = Paginator(lists, 12) data = { "lists": paginated.get_page(request.GET.get("page")), "list_form": forms.ListForm(), "path": "/list", } return TemplateResponse(request, "lists/lists.html", data)
def get(self, request, list_id): """ display a pending list """ book_list = get_object_or_404(models.List, id=list_id) if not book_list.user == request.user: # only the creater can curate the list return HttpResponseNotFound() data = { "list": book_list, "pending": book_list.listitem_set.filter(approved=False), "list_form": forms.ListForm(instance=book_list), } return TemplateResponse(request, "lists/curate.html", data)
def get(self, request, username): """display a book list""" user = get_user_from_username(request.user, username) lists = models.List.privacy_filter(request.user).filter(user=user) paginated = Paginator(lists, 12) data = { "user": user, "is_self": request.user.id == user.id, "lists": paginated.get_page(request.GET.get("page")), "list_form": forms.ListForm(), "path": user.local_path + "/lists", } return TemplateResponse(request, "user/lists.html", data)
def post(self, request, list_id): """edit a list""" book_list = get_object_or_404(models.List, id=list_id) book_list.raise_not_editable(request.user) form = forms.ListForm(request.POST, instance=book_list) if not form.is_valid(): return redirect("list", book_list.id) book_list = form.save() if not book_list.curation == "group": book_list.group = None book_list.save(broadcast=False) return redirect(book_list.local_path)
def get(self, request, list_id): ''' display a pending list ''' book_list = get_object_or_404(models.List, id=list_id) if not book_list.user == request.user: # only the creater can curate the list return HttpResponseNotFound() data = { 'title': 'Curate "%s" | Lists' % book_list.name, 'list': book_list, 'pending': book_list.listitem_set.filter(approved=False), 'list_form': forms.ListForm(instance=book_list), } return TemplateResponse(request, 'lists/curate.html', data)
def get(self, request, group_id): """basic profile info""" user_query = request.GET.get("user_query") group = get_object_or_404(models.Group, id=group_id) lists = (models.List.privacy_filter( request.user).filter(group=group).order_by("-updated_date")) if not group: return HttpResponseBadRequest() if not group.user == request.user: return HttpResponseBadRequest() user_results = ( models.User.viewer_aware_objects(request.user).exclude( memberships__in=group.memberships.all() ) # don't suggest users who are already members .annotate(similarity=Greatest( TrigramSimilarity("username", user_query), TrigramSimilarity("localname", user_query), )).filter(similarity__gt=0.5, local=True).order_by("-similarity")[:5]) no_results = not user_results if user_results.count() < 5: user_results = list(user_results) + list( suggested_users.get_suggestions(request.user, local=True)) data = { "suggested_users": user_results, "no_results": no_results, "group": group, "lists": lists, "group_form": forms.GroupForm(instance=group, auto_id="group_form_id_%s"), "list_form": forms.ListForm(), "user_query": user_query, "requestor_is_manager": request.user == group.user, } return TemplateResponse(request, "groups/find_users.html", data)
def get(self, request, username): """ display a book list """ try: page = int(request.GET.get("page", 1)) except ValueError: page = 1 user = get_user_from_username(request.user, username) lists = models.List.objects.filter(user=user).all() lists = privacy_filter(request.user, lists) paginated = Paginator(lists, 12) data = { "user": user, "is_self": request.user.id == user.id, "lists": paginated.page(page), "list_form": forms.ListForm(), "path": user.local_path + "/lists", } return TemplateResponse(request, "user/lists.html", data)
def get(self, request): """display a book list""" # hide lists with no approved books lists = (models.List.objects.annotate( item_count=Count("listitem", filter=Q( listitem__approved=True))).filter( item_count__gt=0).order_by("-updated_date").distinct()) lists = privacy_filter(request.user, lists, privacy_levels=["public", "followers"]) paginated = Paginator(lists, 12) data = { "lists": paginated.get_page(request.GET.get("page")), "list_form": forms.ListForm(), "path": "/list", } return TemplateResponse(request, "lists/lists.html", data)
def get(self, request, group_id): """display a group""" group = get_object_or_404(models.Group, id=group_id) group.raise_visible_to_user(request.user) lists = (models.List.privacy_filter( request.user).filter(group=group).order_by("-updated_date")) data = { "group": group, "lists": lists, "group_form": forms.GroupForm(instance=group, auto_id="group_form_id_%s"), "list_form": forms.ListForm(), "path": "/group", } return TemplateResponse(request, "groups/group.html", data)
def get(self, request, username): ''' display a book list ''' try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 user = get_user_from_username(username) lists = models.List.objects.filter(user=user).all() lists = privacy_filter(request.user, lists, ['public', 'followers', 'unlisted']) paginated = Paginator(lists, 12) data = { 'title': '%s: Lists' % user.name, 'user': user, 'is_self': request.user.id == user.id, 'lists': paginated.page(page), 'list_form': forms.ListForm(), 'path': user.local_path + '/lists', } return TemplateResponse(request, 'user/lists.html', data)
def get(self, request, list_id): ''' display a book list ''' book_list = get_object_or_404(models.List, id=list_id) if not object_visible_to_user(request.user, book_list): return HttpResponseNotFound() if is_api_request(request): return ActivitypubResponse(book_list.to_activity(**request.GET)) query = request.GET.get('q') suggestions = None if query and request.user.is_authenticated: # search for books suggestions = connector_manager.local_search(query, raw=True) elif request.user.is_authenticated: # just suggest whatever books are nearby suggestions = request.user.shelfbook_set.filter(~Q( book__in=book_list.books.all())) suggestions = [s.book for s in suggestions[:5]] if len(suggestions) < 5: suggestions += [ s.default_edition for s in \ models.Work.objects.filter( ~Q(editions__in=book_list.books.all()), ).order_by('-updated_date') ][:5 - len(suggestions)] data = { 'title': '%s | Lists' % book_list.name, 'list': book_list, 'items': book_list.listitem_set.filter(approved=True), 'pending_count': book_list.listitem_set.filter(approved=False).count(), 'suggested_books': suggestions, 'list_form': forms.ListForm(instance=book_list), 'query': query or '' } return TemplateResponse(request, 'lists/list.html', data)
def get(self, request): ''' display a book list ''' try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 user = request.user if request.user.is_authenticated else None # hide lists with no approved books lists = models.List.objects.filter(~Q(user=user), ).annotate( item_count=Count('listitem', filter=Q( listitem__approved=True))).filter( item_count__gt=0).distinct().all() lists = privacy_filter(request.user, lists, ['public', 'followers']) paginated = Paginator(lists, 12) data = { 'title': 'Lists', 'lists': paginated.page(page), 'list_form': forms.ListForm(), 'path': '/list', } return TemplateResponse(request, 'lists/lists.html', data)
def get(self, request): """ display a book list """ try: page = int(request.GET.get("page", 1)) except ValueError: page = 1 user = request.user if request.user.is_authenticated else None # hide lists with no approved books lists = (models.List.objects.filter(~Q(user=user), ).annotate( item_count=Count("listitem", filter=Q( listitem__approved=True))).filter( item_count__gt=0).distinct().all()) lists = privacy_filter(request.user, lists, privacy_levels=["public", "followers"]) paginated = Paginator(lists, 12) data = { "lists": paginated.page(page), "list_form": forms.ListForm(), "path": "/list", } return TemplateResponse(request, "lists/lists.html", data)
def get(self, request, list_id): """ display a book list """ book_list = get_object_or_404(models.List, id=list_id) if not object_visible_to_user(request.user, book_list): return HttpResponseNotFound() if is_api_request(request): return ActivitypubResponse(book_list.to_activity(**request.GET)) query = request.GET.get("q") suggestions = None if query and request.user.is_authenticated: # search for books suggestions = connector_manager.local_search(query, raw=True) elif request.user.is_authenticated: # just suggest whatever books are nearby suggestions = request.user.shelfbook_set.filter(~Q( book__in=book_list.books.all())) suggestions = [s.book for s in suggestions[:5]] if len(suggestions) < 5: suggestions += [ s.default_edition for s in models.Work.objects.filter( ~Q(editions__in=book_list.books.all()), ).order_by( "-updated_date") ][:5 - len(suggestions)] data = { "list": book_list, "items": book_list.listitem_set.filter(approved=True), "pending_count": book_list.listitem_set.filter(approved=False).count(), "suggested_books": suggestions, "list_form": forms.ListForm(instance=book_list), "query": query or "", } return TemplateResponse(request, "lists/list.html", data)
def get(self, request, list_id, add_failed=False, add_succeeded=False): """display a book list""" book_list = get_object_or_404(models.List, id=list_id) book_list.raise_visible_to_user(request.user) if is_api_request(request): return ActivitypubResponse(book_list.to_activity(**request.GET)) query = request.GET.get("q") suggestions = None items = book_list.listitem_set.filter(approved=True).prefetch_related( "user", "book", "book__authors") items = sort_list(request, items) paginated = Paginator(items, PAGE_LENGTH) if query and request.user.is_authenticated: # search for books suggestions = book_search.search( query, filters=[~Q(parent_work__editions__in=book_list.books.all())], ) elif request.user.is_authenticated: # just suggest whatever books are nearby suggestions = request.user.shelfbook_set.filter(~Q( book__in=book_list.books.all())) suggestions = [s.book for s in suggestions[:5]] if len(suggestions) < 5: suggestions += [ s.default_edition for s in models.Work.objects.filter( ~Q(editions__in=book_list.books.all()), ).order_by( "-updated_date") ][:5 - len(suggestions)] page = paginated.get_page(request.GET.get("page")) embed_key = str(book_list.embed_key.hex) embed_url = reverse("embed-list", args=[book_list.id, embed_key]) embed_url = request.build_absolute_uri(embed_url) if request.GET: embed_url = f"{embed_url}?{request.GET.urlencode()}" data = { "list": book_list, "items": page, "page_range": paginated.get_elided_page_range(page.number, on_each_side=2, on_ends=1), "pending_count": book_list.listitem_set.filter(approved=False).count(), "suggested_books": suggestions, "list_form": forms.ListForm(instance=book_list), "query": query or "", "sort_form": forms.SortListForm(request.GET), "embed_url": embed_url, "add_failed": add_failed, "add_succeeded": add_succeeded, } return TemplateResponse(request, "lists/list.html", data)
def get(self, request, list_id): """display a book list""" book_list = get_object_or_404(models.List, id=list_id) if not book_list.visible_to_user(request.user): return HttpResponseNotFound() if is_api_request(request): return ActivitypubResponse(book_list.to_activity(**request.GET)) query = request.GET.get("q") suggestions = None # sort_by shall be "order" unless a valid alternative is given sort_by = request.GET.get("sort_by", "order") if sort_by not in ("order", "title", "rating"): sort_by = "order" # direction shall be "ascending" unless a valid alternative is given direction = request.GET.get("direction", "ascending") if direction not in ("ascending", "descending"): direction = "ascending" directional_sort_by = { "order": "order", "title": "book__title", "rating": "average_rating", }[sort_by] if direction == "descending": directional_sort_by = "-" + directional_sort_by items = book_list.listitem_set if sort_by == "rating": items = items.annotate(average_rating=Avg( Coalesce("book__review__rating", 0.0), output_field=DecimalField(), )) items = items.filter(approved=True).order_by(directional_sort_by) paginated = Paginator(items, PAGE_LENGTH) if query and request.user.is_authenticated: # search for books suggestions = connector_manager.local_search( query, raw=True, filters=[~Q(parent_work__editions__in=book_list.books.all())], ) elif request.user.is_authenticated: # just suggest whatever books are nearby suggestions = request.user.shelfbook_set.filter(~Q( book__in=book_list.books.all())) suggestions = [s.book for s in suggestions[:5]] if len(suggestions) < 5: suggestions += [ s.default_edition for s in models.Work.objects.filter( ~Q(editions__in=book_list.books.all()), ).order_by( "-updated_date") ][:5 - len(suggestions)] page = paginated.get_page(request.GET.get("page")) data = { "list": book_list, "items": page, "page_range": paginated.get_elided_page_range(page.number, on_each_side=2, on_ends=1), "pending_count": book_list.listitem_set.filter(approved=False).count(), "suggested_books": suggestions, "list_form": forms.ListForm(instance=book_list), "query": query or "", "sort_form": forms.SortListForm({ "direction": direction, "sort_by": sort_by }), } return TemplateResponse(request, "lists/list.html", data)