2022-12-21 23:03:21 -08:00
|
|
|
import datetime
|
|
|
|
|
2022-12-20 00:51:53 -08:00
|
|
|
from django import forms
|
2022-12-21 23:03:21 -08:00
|
|
|
from django.conf import settings
|
2022-12-20 00:51:53 -08:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2022-12-21 23:03:21 -08:00
|
|
|
from django.utils import timezone
|
2022-12-20 00:51:53 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.generic import FormView, ListView
|
|
|
|
|
2022-12-20 01:01:54 -08:00
|
|
|
from users.decorators import moderator_required
|
2022-12-20 00:51:53 -08:00
|
|
|
from users.models import Invite
|
|
|
|
|
|
|
|
|
2022-12-20 01:01:54 -08:00
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
2022-12-20 00:51:53 -08:00
|
|
|
class InvitesRoot(ListView):
|
|
|
|
template_name = "admin/invites.html"
|
|
|
|
paginate_by = 30
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.extra_context = {
|
|
|
|
"section": "invites",
|
|
|
|
}
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Invite.objects.order_by("created")
|
|
|
|
|
|
|
|
|
2022-12-20 01:01:54 -08:00
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
2022-12-20 00:51:53 -08:00
|
|
|
class InviteCreate(FormView):
|
|
|
|
template_name = "admin/invite_create.html"
|
|
|
|
extra_context = {
|
|
|
|
"section": "invites",
|
|
|
|
}
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
2022-12-21 23:03:21 -08:00
|
|
|
uses = forms.IntegerField(
|
|
|
|
required=False,
|
|
|
|
help_text="Number of times this can be used. Leave blank for infinite uses.",
|
|
|
|
)
|
|
|
|
expires_days = forms.IntegerField(
|
2022-12-20 00:51:53 -08:00
|
|
|
required=False,
|
2022-12-21 23:03:21 -08:00
|
|
|
help_text="Number of days until this expires. Leave blank to make it last forever.",
|
2022-12-20 00:51:53 -08:00
|
|
|
)
|
|
|
|
notes = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
help_text="Notes for other admins",
|
|
|
|
)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2022-12-21 23:03:21 -08:00
|
|
|
expires_days = form.cleaned_data.get("expires_days")
|
2022-12-20 06:38:42 -08:00
|
|
|
invite = Invite.create_random(
|
2022-12-21 23:03:21 -08:00
|
|
|
uses=form.cleaned_data.get("uses") or None,
|
|
|
|
expires=(
|
|
|
|
timezone.now() + datetime.timedelta(days=expires_days)
|
|
|
|
if expires_days is not None
|
|
|
|
else None
|
|
|
|
),
|
2022-12-20 06:38:42 -08:00
|
|
|
note=form.cleaned_data.get("notes"),
|
|
|
|
)
|
|
|
|
return redirect(invite.urls.admin_view)
|
2022-12-20 00:51:53 -08:00
|
|
|
|
|
|
|
|
2022-12-20 01:01:54 -08:00
|
|
|
@method_decorator(moderator_required, name="dispatch")
|
2022-12-20 00:51:53 -08:00
|
|
|
class InviteView(FormView):
|
|
|
|
template_name = "admin/invite_view.html"
|
|
|
|
extra_context = {
|
|
|
|
"section": "invites",
|
|
|
|
}
|
|
|
|
|
|
|
|
class form_class(InviteCreate.form_class):
|
2022-12-21 23:03:21 -08:00
|
|
|
link = forms.CharField(disabled=True, required=False)
|
2022-12-20 00:51:53 -08:00
|
|
|
|
|
|
|
def dispatch(self, request, id, *args, **kwargs):
|
|
|
|
self.invite = get_object_or_404(Invite, id=id)
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if "delete" in request.POST:
|
|
|
|
self.invite.delete()
|
|
|
|
return redirect(self.invite.urls.admin)
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
return {
|
|
|
|
"notes": self.invite.note,
|
2022-12-21 23:03:21 -08:00
|
|
|
"uses": self.invite.uses,
|
|
|
|
"link": f"https://{settings.MAIN_DOMAIN}/auth/signup/{self.invite.token}/",
|
2022-12-20 00:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2022-12-21 23:03:21 -08:00
|
|
|
expires_days = form.cleaned_data.get("expires_days")
|
2022-12-20 00:51:53 -08:00
|
|
|
self.invite.note = form.cleaned_data.get("notes") or ""
|
2022-12-21 23:03:21 -08:00
|
|
|
self.invite.uses = form.cleaned_data.get("uses") or None
|
|
|
|
self.invite.expires = (
|
|
|
|
timezone.now() + datetime.timedelta(days=expires_days)
|
|
|
|
if expires_days is not None
|
|
|
|
else None
|
|
|
|
)
|
2022-12-20 00:51:53 -08:00
|
|
|
self.invite.save()
|
|
|
|
return redirect(self.invite.urls.admin)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["invite"] = self.invite
|
2023-06-14 10:15:29 -07:00
|
|
|
context["page"] = self.request.GET.get("page")
|
2022-12-20 00:51:53 -08:00
|
|
|
return context
|