moviewyrm/bookwyrm/views/invite.py

80 lines
2.5 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" invites when registration is closed """
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-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
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import forms, models
from bookwyrm.settings import PAGE_LENGTH
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-03-08 16:49:10 +00:00
""" create invites """
2021-01-12 18:19:58 +00:00
def get(self, request):
2021-03-08 16:49:10 +00:00
""" invite management page """
try:
2021-03-08 16:49:10 +00:00
page = int(request.GET.get("page", 1))
except ValueError:
page = 1
2021-03-08 16:49:10 +00:00
paginated = Paginator(
models.SiteInvite.objects.filter(user=request.user).order_by(
"-created_date"
),
PAGE_LENGTH,
)
2021-01-12 18:19:58 +00:00
data = {
2021-03-08 16:49:10 +00:00
"invites": paginated.page(page),
"form": forms.CreateInviteForm(),
2021-01-12 18:19:58 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "settings/manage_invites.html", data)
2021-01-12 18:19:58 +00:00
def post(self, request):
2021-03-08 16:49:10 +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():
return HttpResponseBadRequest("ERRORS : %s" % (form.errors,))
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}
return TemplateResponse(request, "settings/manage_invites.html", data)
2021-01-12 18:19:58 +00:00
class Invite(View):
2021-03-08 16:49:10 +00:00
""" use an invite to register """
2021-01-12 18:19:58 +00:00
def get(self, request, code):
2021-03-08 16:49:10 +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.authentication.Register