Byrokracie Sklené 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.1 KiB

import qrcode
import base64
import io
from django.contrib.auth import get_user
from django.shortcuts import redirect, get_object_or_404
from django.urls import reverse
from django.views.generic import DetailView
from django.views.generic.base import TemplateView
from django.core.exceptions import PermissionDenied
from django.db import transaction
from . import logika
from .models import Stanoviste, Tym
from .utils.hash import get_hash_now, get_hash_previous, get_hash_preprevious
from .utils.doba import Doba
# Jen hloupé rozhazovátko
def hra_view(request):
user = request.user
if not user.is_authenticated:
return redirect(reverse("login"))
stanoviste = Stanoviste.objects.filter(
if stanoviste is not None:
return redirect(reverse("stanoviste"))
tym = Tym.objects.filter(
if tym is not None:
return redirect(reverse("tym"))
raise PermissionDenied
class StatickeStanovisteView(DetailView):
template_name = "hra/staticke_stanoviste.html"
model = Stanoviste
def get_object(self, queryset=None):
if "stanoviste" in self.kwargs:
return get_object_or_404(Stanoviste, id=self.kwargs["stanoviste"])
user = get_user(self.request)
stanoviste = Stanoviste.objects.filter(
if stanoviste is None:
raise PermissionDenied
return stanoviste
def get_url(self):
return self.request.build_absolute_uri(reverse(
"my_hash": get_hash_now(,
def get_base64_qr_code(url: str):
buffer = io.BytesIO()
img_str = base64.b64encode(buffer.getvalue())
return img_str.decode("utf-8") # convert to str and cut b'' chars
def get_context_data(self, **kwargs):
context = super().get_context_data()
url = self.get_url()
context["url"] = url
context["image64"] = self.get_base64_qr_code(url)
return context
class TymView(DetailView):
template_name = "hra/tym.html"
model = Tym
def get_object(self, queryset=None):
user = get_user(self.request)
tym = Tym.objects.filter(
if tym is None:
raise PermissionDenied
return tym
def get_context_data(self, **kwargs):
context = super().get_context_data()
context["doba"] = self.object.doba
return context
class StanovisteZPohleduTymu(TemplateView):
stanoviste_id: int
stanoviste: Stanoviste
tym: Tym
doba: Doba
def get_template_names(self):
PREFIX = "hra/stanoviste/"
return PREFIX + self.stanoviste.template
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.stanoviste_id = self.kwargs["stanoviste"]
self.stanoviste = get_object_or_404(Stanoviste, id=self.stanoviste_id)
self.tym = get_object_or_404(Tym,
self.doba = self.tym.doba
def dispatch(self, request, *args, **kwargs):
if not self.pristupne():
return redirect(reverse("timeout"))
return super().dispatch(request, *args, **kwargs)
def pristupne(self):
my_hash = self.kwargs["my_hash"]
return my_hash == get_hash_now(self.stanoviste_id) or my_hash == get_hash_previous(self.stanoviste_id) or my_hash == get_hash_preprevious(self.stanoviste_id)
def get_posible_choices(self) -> list[str]:
return logika.get_posible_choices[](self.doba, self.tym)
def get_context_data(self, **kwargs):
context = super().get_context_data()
context["stanoviste"] = self.stanoviste
context["tym"] = self.tym
context["doba"] = self.doba
context["choices"] = self.get_posible_choices()
return context
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
self.template_name = "hra/staticke_stanoviste.html"
choice = self.request.POST.get("choice", None)
with transaction.atomic():
if (choice is None) or (choice not in self.get_posible_choices()):
# raise PermissionDenied
context["zadosti"] = logika.apply_choice[][choice](self.doba, self.tym)
context["choice"] = choice
return self.render_to_response(context)
class TimeoutViwe(TemplateView):
template_name = "hra/timeout.html"