2021-03-08 11:49:10 -05:00
|
|
|
""" non-interactive pages """
|
2022-01-06 12:04:59 -05:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2022-01-06 12:39:51 -05:00
|
|
|
from django.db.models import Avg, StdDev, Count, F, Q
|
2021-01-12 13:44:17 -05:00
|
|
|
from django.template.response import TemplateResponse
|
2022-01-06 12:04:59 -05:00
|
|
|
from django.utils import timezone
|
2021-01-12 13:44:17 -05:00
|
|
|
from django.views import View
|
2022-01-06 00:42:41 -05:00
|
|
|
from django.views.decorators.http import require_GET
|
2021-01-12 13:44:17 -05:00
|
|
|
|
2022-01-06 12:04:59 -05:00
|
|
|
from bookwyrm import forms, models, settings
|
2021-10-16 14:45:55 -04:00
|
|
|
from bookwyrm.views import helpers
|
|
|
|
from bookwyrm.views.feed import Feed
|
2021-01-12 13:44:17 -05:00
|
|
|
|
|
|
|
|
2022-01-06 00:42:41 -05:00
|
|
|
@require_GET
|
|
|
|
def about(request):
|
|
|
|
"""more information about the instance"""
|
2022-01-06 12:04:59 -05:00
|
|
|
six_months_ago = timezone.now() - relativedelta(months=6)
|
|
|
|
six_month_count = models.User.objects.filter(
|
|
|
|
is_active=True, local=True, last_active_date__gt=six_months_ago
|
|
|
|
).count()
|
2022-01-06 00:42:41 -05:00
|
|
|
data = {
|
2022-01-06 12:04:59 -05:00
|
|
|
"active_users": six_month_count,
|
|
|
|
"status_count": models.Status.objects.filter(
|
|
|
|
user__local=True, deleted=False
|
|
|
|
).count(),
|
2022-01-06 00:42:41 -05:00
|
|
|
"admins": models.User.objects.filter(groups__name__in=["admin", "moderator"]),
|
2022-01-06 12:04:59 -05:00
|
|
|
"version": settings.VERSION,
|
2022-01-06 00:42:41 -05:00
|
|
|
}
|
2022-01-06 12:04:59 -05:00
|
|
|
|
|
|
|
books = models.Edition.objects.exclude(cover__exact="")
|
|
|
|
|
2022-01-06 12:48:36 -05:00
|
|
|
total_ratings = models.Review.objects.filter(
|
|
|
|
user__local=True, deleted=False
|
|
|
|
).count()
|
|
|
|
data["top_rated"] = (
|
|
|
|
books.annotate(
|
|
|
|
rating=Avg(
|
|
|
|
"review__rating",
|
|
|
|
filter=Q(review__user__local=True, review__deleted=False),
|
|
|
|
),
|
|
|
|
rating_count=Count(
|
|
|
|
"review__rating",
|
|
|
|
filter=Q(review__user__local=True, review__deleted=False),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.annotate(weighted=F("rating") * F("rating_count") / total_ratings)
|
|
|
|
.filter(weighted__gt=0)
|
|
|
|
.order_by("-weighted")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
data["controversial"] = (
|
|
|
|
books.annotate(
|
|
|
|
deviation=StdDev(
|
|
|
|
"review__rating",
|
|
|
|
filter=Q(review__user__local=True, review__deleted=False),
|
|
|
|
),
|
|
|
|
rating_count=Count(
|
|
|
|
"review__rating",
|
|
|
|
filter=Q(review__user__local=True, review__deleted=False),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.annotate(weighted=F("deviation") * F("rating_count") / total_ratings)
|
|
|
|
.filter(weighted__gt=0)
|
|
|
|
.order_by("-weighted")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
data["wanted"] = (
|
|
|
|
books.annotate(
|
|
|
|
shelf_count=Count("shelves", filter=Q(shelves__identifier="to-read"))
|
|
|
|
)
|
|
|
|
.order_by("-shelf_count")
|
|
|
|
.first()
|
|
|
|
)
|
2022-01-06 12:04:59 -05:00
|
|
|
|
2022-01-06 00:42:41 -05:00
|
|
|
return TemplateResponse(request, "about/about.html", data)
|
2021-03-08 11:49:10 -05:00
|
|
|
|
|
|
|
|
2022-01-06 00:42:41 -05:00
|
|
|
@require_GET
|
|
|
|
def conduct(request):
|
|
|
|
"""more information about the instance"""
|
|
|
|
return TemplateResponse(request, "about/conduct.html")
|
2021-01-12 13:44:17 -05:00
|
|
|
|
2022-01-06 00:42:41 -05:00
|
|
|
|
|
|
|
@require_GET
|
|
|
|
def privacy(request):
|
|
|
|
"""more information about the instance"""
|
|
|
|
return TemplateResponse(request, "about/privacy.html")
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
2021-01-12 13:44:17 -05:00
|
|
|
class Home(View):
|
2021-08-07 14:15:02 -04:00
|
|
|
"""landing page or home feed depending on auth"""
|
2021-03-08 11:49:10 -05:00
|
|
|
|
2021-01-12 13:44:17 -05:00
|
|
|
def get(self, request):
|
2021-04-26 12:15:42 -04:00
|
|
|
"""this is the same as the feed on the home tab"""
|
2021-01-12 13:44:17 -05:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
feed_view = Feed.as_view()
|
2021-03-08 11:49:10 -05:00
|
|
|
return feed_view(request, "home")
|
2021-08-07 14:15:02 -04:00
|
|
|
landing_view = Landing.as_view()
|
|
|
|
return landing_view(request)
|
2021-01-12 13:44:17 -05:00
|
|
|
|
2021-03-08 11:49:10 -05:00
|
|
|
|
2021-08-07 14:15:02 -04:00
|
|
|
class Landing(View):
|
2021-04-26 12:15:42 -04:00
|
|
|
"""preview of recently reviewed books"""
|
2021-03-08 11:49:10 -05:00
|
|
|
|
2021-01-12 13:44:17 -05:00
|
|
|
def get(self, request):
|
2021-04-26 12:15:42 -04:00
|
|
|
"""tiled book activity page"""
|
2021-01-12 13:44:17 -05:00
|
|
|
data = {
|
2021-03-08 11:49:10 -05:00
|
|
|
"register_form": forms.RegisterForm(),
|
2021-03-20 22:14:41 -04:00
|
|
|
"request_form": forms.InviteRequestForm(),
|
2021-08-07 14:15:02 -04:00
|
|
|
"books": helpers.get_landing_books(),
|
2021-01-12 13:44:17 -05:00
|
|
|
}
|
2021-08-07 14:15:02 -04:00
|
|
|
return TemplateResponse(request, "landing/landing.html", data)
|