moviewyrm/bookwyrm/views/directory.py

61 lines
1.8 KiB
Python
Raw Normal View History

2021-03-21 23:37:52 +00:00
""" who all's here? """
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
2021-03-25 20:36:35 +00:00
from django.db.models import Count, Q
2021-03-25 15:39:45 +00:00
from django.shortcuts import redirect
2021-03-21 23:37:52 +00:00
from django.template.response import TemplateResponse
from django.views import View
from django.utils.decorators import method_decorator
from bookwyrm import models
# pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch")
class Directory(View):
""" display of known bookwyrm users """
def get(self, request):
""" lets see your cute faces """
try:
page = int(request.GET.get("page", 1))
except ValueError:
page = 1
2021-03-25 20:36:35 +00:00
# filters
filters = {}
software = request.GET.get("software")
if not software or software == "bookwyrm":
2021-03-25 20:36:35 +00:00
filters["bookwyrm_user"] = True
scope = request.GET.get("scope")
if scope == "local":
filters["local"] = True
2021-03-21 23:37:52 +00:00
users = models.User.objects.filter(
2021-03-25 20:36:35 +00:00
discoverable=True, is_active=True, **filters
).annotate(
mutuals=Count(
"following",
filter=Q(
~Q(id=request.user.id), following__in=request.user.following.all()
),
)
)
sort = request.GET.get("sort")
if sort == "recent":
users = users.order_by("-last_active_date")
else:
users = users.order_by("-mutuals", "-last_active_date")
2021-03-21 23:37:52 +00:00
paginated = Paginator(users, 12)
data = {
"users": paginated.page(page),
}
return TemplateResponse(request, "directory.html", data)
2021-03-25 15:39:45 +00:00
def post(self, request):
""" join the directory """
request.user.discoverable = True
request.user.save()
2021-03-25 19:04:26 +00:00
return redirect("directory")