85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
import random
|
|
|
|
from django.http import JsonResponse
|
|
from django.shortcuts import render
|
|
from django.views import View
|
|
from django.views.generic import ListView, TemplateView
|
|
|
|
from .forms import ContactForm
|
|
from .models import ContactSubmission, DownloadItem, FAQEntry
|
|
|
|
|
|
class HomeView(TemplateView):
|
|
template_name = "pages/home.html"
|
|
|
|
|
|
class AboutView(TemplateView):
|
|
template_name = "pages/about.html"
|
|
|
|
|
|
class DownloadsView(ListView):
|
|
template_name = "pages/downloads.html"
|
|
context_object_name = "download_items"
|
|
|
|
def get_queryset(self):
|
|
return DownloadItem.objects.filter(is_active=True).order_by("order", "platform")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
all_items = DownloadItem.objects.order_by("order", "platform")
|
|
context["windows_items"] = all_items.filter(
|
|
platform=DownloadItem.PLATFORM_WINDOWS
|
|
)
|
|
context["macos_items"] = all_items.filter(platform=DownloadItem.PLATFORM_MACOS)
|
|
context["linux_items"] = all_items.filter(platform=DownloadItem.PLATFORM_LINUX)
|
|
return context
|
|
|
|
|
|
class FAQView(ListView):
|
|
model = FAQEntry
|
|
template_name = "pages/faq.html"
|
|
context_object_name = "faq_entries"
|
|
queryset = FAQEntry.objects.filter(is_active=True)
|
|
|
|
|
|
class ContactView(View):
|
|
template_name = "pages/contact.html"
|
|
|
|
def _new_captcha(self, request):
|
|
a, b = random.randint(1, 9), random.randint(1, 9)
|
|
request.session["captcha_answer"] = a + b
|
|
return f"{a} + {b}"
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
return render(
|
|
request,
|
|
self.template_name,
|
|
{"form": ContactForm(), "captcha_question": self._new_captcha(request)},
|
|
)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
form = ContactForm(request.POST)
|
|
expected = request.session.get("captcha_answer")
|
|
captcha_question = self._new_captcha(request)
|
|
|
|
captcha_ok = False
|
|
try:
|
|
captcha_ok = int(request.POST.get("captcha_answer", "")) == expected
|
|
except (ValueError, TypeError):
|
|
pass
|
|
|
|
if form.is_valid() and captcha_ok:
|
|
form.save()
|
|
return JsonResponse({"success": True})
|
|
|
|
errors: dict = {}
|
|
if not captcha_ok:
|
|
errors["captcha"] = ["Incorrect answer — please try again."]
|
|
errors.update(
|
|
{field: [str(e) for e in errs] for field, errs in form.errors.items()}
|
|
)
|
|
return JsonResponse(
|
|
{"success": False, "errors": errors, "captcha_question": captcha_question},
|
|
status=400,
|
|
)
|