moviewyrm/bookwyrm/views/admin/invite.py

192 lines
6.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" invites when registration is closed """
2021-04-01 21:14:17 +00:00
from functools import reduce
import operator
2021-04-01 20:55:10 +00:00
from urllib.parse import urlencode
2021-01-12 18:19:58 +00:00
from django.contrib.auth.decorators import login_required, permission_required
from django.core.paginator import Paginator
2021-04-01 21:14:17 +00:00
from django.db.models import Q
2021-01-12 18:19:58 +00:00
from django.http import HttpResponseBadRequest
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
2021-04-01 20:55:10 +00:00
from django.urls import reverse
2021-01-12 18:19:58 +00:00
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.http import require_POST
2021-01-12 18:19:58 +00:00
2021-03-21 16:13:21 +00:00
from bookwyrm import emailing, forms, models
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views import helpers
2021-01-12 18:19:58 +00:00
# pylint: disable= no-self-use
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-12 18:19:58 +00:00
@method_decorator(
2021-03-08 16:49:10 +00:00
permission_required("bookwyrm.create_invites", raise_exception=True),
name="dispatch",
)
2021-01-12 18:19:58 +00:00
class ManageInvites(View):
2021-04-26 16:15:42 +00:00
"""create invites"""
2021-03-08 16:49:10 +00:00
2021-01-12 18:19:58 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""invite management page"""
2021-03-08 16:49:10 +00:00
paginated = Paginator(
models.SiteInvite.objects.filter(user=request.user).order_by(
"-created_date"
),
PAGE_LENGTH,
)
2021-06-14 22:39:54 +00:00
page = paginated.get_page(request.GET.get("page"))
2021-01-12 18:19:58 +00:00
data = {
2021-06-14 22:39:54 +00:00
"invites": page,
"page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
),
2021-03-08 16:49:10 +00:00
"form": forms.CreateInviteForm(),
2021-01-12 18:19:58 +00:00
}
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/invites/manage_invites.html", data)
2021-01-12 18:19:58 +00:00
def post(self, request):
2021-04-26 16:15:42 +00:00
"""creates an invite database entry"""
2021-01-12 18:19:58 +00:00
form = forms.CreateInviteForm(request.POST)
if not form.is_valid():
2021-09-27 17:17:16 +00:00
return HttpResponseBadRequest(f"ERRORS: {form.errors}")
2021-01-12 18:19:58 +00:00
invite = form.save(commit=False)
invite.user = request.user
invite.save()
2021-03-08 16:49:10 +00:00
paginated = Paginator(
models.SiteInvite.objects.filter(user=request.user).order_by(
"-created_date"
),
PAGE_LENGTH,
)
data = {"invites": paginated.page(1), "form": form}
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/invites/manage_invites.html", data)
2021-01-12 18:19:58 +00:00
class Invite(View):
2021-04-26 16:15:42 +00:00
"""use an invite to register"""
2021-03-08 16:49:10 +00:00
2021-01-12 18:19:58 +00:00
def get(self, request, code):
2021-04-26 16:15:42 +00:00
"""endpoint for using an invites"""
2021-01-12 18:19:58 +00:00
if request.user.is_authenticated:
2021-03-08 16:49:10 +00:00
return redirect("/")
2021-01-12 18:19:58 +00:00
invite = get_object_or_404(models.SiteInvite, code=code)
data = {
2021-03-08 16:49:10 +00:00
"register_form": forms.RegisterForm(),
"invite": invite,
"valid": invite.valid() if invite else True,
2021-01-12 18:19:58 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "invite.html", data)
2021-01-12 18:19:58 +00:00
# post handling is in views.register.Register
2021-03-21 02:14:41 +00:00
2021-03-21 02:39:08 +00:00
class ManageInviteRequests(View):
2021-04-26 16:15:42 +00:00
"""grant invites like the benevolent lord you are"""
2021-03-21 02:39:08 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""view a list of requests"""
ignored = request.GET.get("ignored", False)
2021-04-01 20:41:08 +00:00
sort = request.GET.get("sort")
sort_fields = [
"created_date",
"invite__times_used",
"invite__invitees__created_date",
]
2021-09-27 17:17:16 +00:00
# pylint: disable=consider-using-f-string
2021-04-01 20:41:08 +00:00
if not sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
sort = "-created_date"
2021-04-01 21:14:17 +00:00
requests = models.InviteRequest.objects.filter(ignored=ignored).order_by(sort)
status_filters = [
s
for s in request.GET.getlist("status")
if s in ["requested", "sent", "accepted"]
]
filters = []
if "requested" in status_filters:
filters.append({"invite__isnull": True})
if "sent" in status_filters:
filters.append({"invite__isnull": False, "invite__times_used": 0})
2021-04-01 21:14:17 +00:00
if "accepted" in status_filters:
filters.append({"invite__isnull": False, "invite__times_used__gte": 1})
if filters:
requests = requests.filter(
reduce(operator.or_, (Q(**f) for f in filters))
).distinct()
2021-04-01 21:14:17 +00:00
2021-06-14 22:39:54 +00:00
paginated = Paginator(requests, PAGE_LENGTH)
2021-03-21 02:39:08 +00:00
2021-06-14 22:39:54 +00:00
page = paginated.get_page(request.GET.get("page"))
2021-03-21 02:39:08 +00:00
data = {
"ignored": ignored,
2021-03-28 18:15:26 +00:00
"count": paginated.count,
2021-06-14 22:39:54 +00:00
"requests": page,
"page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
),
2021-04-01 20:41:08 +00:00
"sort": sort,
2021-03-21 02:39:08 +00:00
}
2021-09-28 19:39:52 +00:00
return TemplateResponse(
request, "settings/invites/manage_invite_requests.html", data
)
2021-03-21 02:39:08 +00:00
def post(self, request):
2021-04-26 16:15:42 +00:00
"""send out an invite"""
2021-03-21 16:13:21 +00:00
invite_request = get_object_or_404(
models.InviteRequest, id=request.POST.get("invite-request")
)
2021-03-29 20:28:30 +00:00
# only create a new invite if one doesn't exist already (resending)
if not invite_request.invite:
invite_request.invite = models.SiteInvite.objects.create(
use_limit=1,
user=request.user,
)
invite_request.save()
2021-03-21 16:13:21 +00:00
emailing.invite_email(invite_request)
2021-09-27 17:17:16 +00:00
# pylint: disable=consider-using-f-string
2021-04-01 20:55:10 +00:00
return redirect(
"{:s}?{:s}".format(
reverse("settings-invite-requests"), urlencode(request.GET.dict())
)
)
2021-03-21 02:14:41 +00:00
class InviteRequest(View):
2021-04-26 16:15:42 +00:00
"""prospective users sign up here"""
2021-03-21 02:14:41 +00:00
def post(self, request):
2021-04-26 16:15:42 +00:00
"""create a request"""
2021-03-21 02:14:41 +00:00
form = forms.InviteRequestForm(request.POST)
received = False
if form.is_valid():
received = True
form.save()
data = {
"request_form": form,
"request_received": received,
2021-08-07 22:10:09 +00:00
"books": helpers.get_landing_books(),
2021-03-21 02:14:41 +00:00
}
2021-08-07 22:10:09 +00:00
return TemplateResponse(request, "landing/landing.html", data)
@require_POST
def ignore_invite_request(request):
2021-04-26 16:15:42 +00:00
"""hide an invite request"""
2021-03-21 15:17:48 +00:00
invite_request = get_object_or_404(
models.InviteRequest, id=request.POST.get("invite-request")
)
invite_request.ignored = not invite_request.ignored
invite_request.save()
return redirect("settings-invite-requests")