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

97 lines
3.5 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
2022-08-03 02:07:44 +02:00
from .models import Tobacco, ClubhausEvent, EventDate, EventDateVotes, 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-07-30 18:02:05 +02:00
def tobacco(request: HttpRequest) -> django.http.HttpResponse:
2022-07-31 18:53:53 +02:00
context = {'tobaccos': Tobacco.objects.all().order_by('-in_stock')}
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-08-03 02:07:44 +02:00
votes = EventDateVotes.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', {'next_event': next_event, "dates": dates, 'votes': vote_map})
else:
return HttpResponseRedirect(reverse("index"))
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
if request.META.get("X-Forwarded-For"):
raise ProxyUsageDetected()
2022-08-03 02:06:55 +02:00
if request.method != "POST":
return HttpResponseRedirect(reverse("events"))
# Check rate limit
2022-07-30 18:02:05 +02:00
ip = request.META.get("REMOTE_ADDR")
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:
raise RateLimitHit()
# 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
EventDateVotes.objects.update_or_create(voter=user, date=date, defaults={"available": available})
return HttpResponseRedirect(reverse("events"))