1
0
clubhaus-schornbach/clubhaus/homepage/views.py

105 lines
3.7 KiB
Python
Raw Normal View History

import django.utils.timezone
2022-07-30 18:02:05 +02:00
from django.core.cache import cache
from django.http import HttpRequest, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
2022-08-03 02:06:55 +02:00
import clubhaus.settings as django_settings
from .exceptions import RateLimitHit, ProxyUsageDetected
from .models import Tobacco, ClubhausEvent, EventDate, EventDateVote, VotingUser
def bad_request(request, exception, template_name="homepage/400.html"):
return render(request, template_name, context={"exception": exception}, status=400)
def index(request: HttpRequest) -> django.http.HttpResponse:
return render(request, 'homepage/index.html')
2022-08-05 23:19:46 +02:00
def impressum(request: HttpRequest) -> django.http.HttpResponse:
return render(request, 'homepage/impressum.html')
2022-08-06 19:16:52 +02:00
def cookies(request: HttpRequest) -> django.http.HttpResponse:
return render(request, 'homepage/cookies.html')
2022-07-30 18:02:05 +02:00
def tobacco(request: HttpRequest) -> django.http.HttpResponse:
context = {'tobaccos': Tobacco.objects.all().order_by('-in_stock', "category__name", "name")}
return render(request, 'homepage/tobacco.html', context)
2022-07-30 18:02:05 +02:00
def events(request: HttpRequest) -> django.http.HttpResponse:
next_events = ClubhausEvent.objects.filter(active=True).order_by('-id')
if len(next_events) == 1:
next_event = next_events[0]
dates = EventDate.objects.filter(event=next_event).order_by("date")
2022-09-04 00:37:02 +02:00
votes = EventDateVote.objects.filter(date__event=next_event).order_by("voter_id", "date")
unique_voters = list(set(map(lambda v: v.voter, votes)))
vote_map = {}
for voter in unique_voters:
vote_map[voter] = {date.date.isoformat(): None for date in dates}
for vote in votes:
voter = vote.voter
v_date = vote.date.date.isoformat()
if voter in vote_map and v_date in vote_map[voter]:
vote_map[voter][v_date] = vote.available
return render(request, 'homepage/events.html', context={
'next_event': next_event,
"dates": dates,
'votes': vote_map
})
else:
return render(request, 'homepage/coming-soon.html')
2022-07-30 18:02:05 +02:00
def voting(request: HttpRequest) -> django.http.HttpResponse:
request.session.clear_expired()
2022-07-31 18:53:53 +02:00
2022-08-03 02:06:55 +02:00
# Proxy use is forbidden
2022-08-07 23:06:45 +02:00
if proxy := request.META.get("HTTP_X_FORWARDED_FOR"):
raise ProxyUsageDetected(proxy)
2022-08-03 02:06:55 +02:00
if request.method != "POST":
return HttpResponseRedirect(reverse("events"))
# Check rate limit
ip = request.META.get("HTTP_X_REAL_IP") or ""
cache_key = f"voting_block_{ip}"
2022-07-30 18:02:05 +02:00
rate_cache: django.core.cache.BaseCache = cache
if ip not in rate_cache:
2022-08-03 02:06:55 +02:00
rate_cache.add(cache_key, 0, django_settings.IP_RATE_LIMIT_TIME)
rate_cache.incr(cache_key)
if rate_cache.get(cache_key) > django_settings.IP_RATE_LIMIT_COUNT:
2022-08-07 23:06:45 +02:00
raise RateLimitHit(ip)
# check params
if not request.POST["name"]:
return HttpResponseRedirect(reverse("events"))
name = request.POST["name"]
2022-08-03 02:07:44 +02:00
modify_key = request.POST["modifyKey"] or ""
request.session["name"] = name
request.session["modifyKey"] = modify_key
# handle delete
if request.POST["deleteClicked"] == "1":
VotingUser.objects.filter(name=name, modify_key=modify_key).delete()
return HttpResponseRedirect(reverse("events"))
# handle normal voting
user, _ = VotingUser.objects.get_or_create(name=name, modify_key=modify_key)
event_dates = EventDate.objects.filter(event__active=True).order_by("date")
for date in event_dates:
available = date.date.isoformat() in request.POST
2022-09-04 00:37:02 +02:00
EventDateVote.objects.update_or_create(voter=user, date=date, defaults={"available": available})
return HttpResponseRedirect(reverse("events"))