From 7412d5f4d9dc363d9b50a602642f63b417452a76 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 30 Jul 2022 18:02:05 +0200 Subject: [PATCH] Add voting sample with ip cache --- clubhaus/.idea/dataSources.xml | 2 +- .../homepage/templates/homepage/events.html | 2 +- .../templates/homepage/landingpage.html | 6 ++ .../homepage/templates/homepage/voting.html | 68 +++++++++++++++++++ clubhaus/homepage/urls.py | 1 + clubhaus/homepage/views.py | 30 ++++++-- clubhaus/requirements.txt | 1 + 7 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 clubhaus/homepage/templates/homepage/voting.html create mode 100644 clubhaus/requirements.txt diff --git a/clubhaus/.idea/dataSources.xml b/clubhaus/.idea/dataSources.xml index f9cdf5d..4ff918c 100644 --- a/clubhaus/.idea/dataSources.xml +++ b/clubhaus/.idea/dataSources.xml @@ -2,7 +2,7 @@ - sqlite.xerial + d9daba00-9242-4b73-bac9-74aa32dd0fa2 true org.sqlite.JDBC jdbc:sqlite:clubhaus.db diff --git a/clubhaus/homepage/templates/homepage/events.html b/clubhaus/homepage/templates/homepage/events.html index 30f7e8f..03747f7 100644 --- a/clubhaus/homepage/templates/homepage/events.html +++ b/clubhaus/homepage/templates/homepage/events.html @@ -96,7 +96,7 @@ - {% endif %} diff --git a/clubhaus/homepage/templates/homepage/landingpage.html b/clubhaus/homepage/templates/homepage/landingpage.html index 92bcab5..6f7b14f 100644 --- a/clubhaus/homepage/templates/homepage/landingpage.html +++ b/clubhaus/homepage/templates/homepage/landingpage.html @@ -1,5 +1,11 @@ {% load static %} +
+ + + +
+ + + + +

Hello Voting

+ +{% if items %} +

{{ items }}

+{% endif %} + +{% if ip and request_count %} +

Your IP is: {{ ip }} with {{ request_count }} requests

+{% endif %} + +{% if session %} +

Your session value is: {{ session.items }}

+{% endif %} + + + + + + + + + \ No newline at end of file diff --git a/clubhaus/homepage/urls.py b/clubhaus/homepage/urls.py index 1de459a..2cbb0d0 100644 --- a/clubhaus/homepage/urls.py +++ b/clubhaus/homepage/urls.py @@ -6,4 +6,5 @@ urlpatterns = [ path('', views.landing, name='index'), path('event', views.events, name='event'), path('tobacco', views.tobacco, name='tobacco'), + path('voting', views.voting, name='voting'), ] \ No newline at end of file diff --git a/clubhaus/homepage/views.py b/clubhaus/homepage/views.py index ccade88..a6f8595 100644 --- a/clubhaus/homepage/views.py +++ b/clubhaus/homepage/views.py @@ -1,24 +1,46 @@ import datetime import django.utils.timezone +from django.http import HttpRequest, HttpResponseRedirect from django.shortcuts import render +from django.urls import reverse +from django.core.cache import cache from .models import Tobacco, TobaccoCategory, ClubhausEvent -# Create your views here. -def tobacco(request): +def tobacco(request: HttpRequest) -> django.http.HttpResponse: test_image = Tobacco.objects.first().picture context = {'tobaccos': Tobacco.objects.all(), 'categories': TobaccoCategory.objects.all(), 'pic': test_image} return render(request, 'homepage/index.html', context) -def events(request): +def events(request: HttpRequest) -> django.http.HttpResponse: yesterday = django.utils.timezone.now() - datetime.timedelta(days=1) next_events = ClubhausEvent.objects.filter(date__gte=yesterday).order_by('-date') next_event = next_events[0] if next_events else None return render(request, 'homepage/events.html', {'next_event': next_event}) -def landing(request): +def landing(request: HttpRequest) -> django.http.HttpResponse: return render(request, 'homepage/landingpage.html', {}) + + +def voting(request: HttpRequest) -> django.http.HttpResponse: + request.session.clear_expired() + ip = request.META.get("REMOTE_ADDR") + rate_cache: django.core.cache.BaseCache = cache + if ip not in rate_cache: + rate_cache.add(f"voting_block_{ip}", 0, 15) + rate_cache.incr(f"voting_block_{ip}") + + if request.method == "GET": + return render(request, 'homepage/voting.html', + {"ip": ip, "request_count": rate_cache.get(f"voting_block_{ip}"), "session": request.session}) + + elif request.method == "POST": + key = request.POST["key"] + value = request.POST["value"] + request.session[key] = value + return render(request, 'homepage/voting.html', {"ip": ip, "session": request.session}) + # return HttpResponseRedirect(reverse("index")) diff --git a/clubhaus/requirements.txt b/clubhaus/requirements.txt new file mode 100644 index 0000000..ebe268e --- /dev/null +++ b/clubhaus/requirements.txt @@ -0,0 +1 @@ +django~=4.0.5 \ No newline at end of file