74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
from django.conf import settings
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.core.exceptions import ViewDoesNotExist
|
|
from django.http import Http404, JsonResponse
|
|
from django.views import View
|
|
from django.views.generic.base import TemplateView
|
|
|
|
from .utils import craft_token
|
|
|
|
|
|
def get_context_from_proj(kind, chans):
|
|
print(kind, chans)
|
|
match kind:
|
|
case "blinder":
|
|
return [
|
|
{
|
|
"id": chans[i],
|
|
"position_y": ((15 - i) // 4) * 25 + 15,
|
|
"position_x": ((15 - i) % 4) * 25 + 15,
|
|
}
|
|
for i in range(len(chans))
|
|
]
|
|
case "led_tub":
|
|
return [
|
|
{
|
|
"id": chans[i],
|
|
"position": i * 10,
|
|
}
|
|
for i in range(len(chans))
|
|
]
|
|
case "spot":
|
|
return {
|
|
"id": chans[0],
|
|
}
|
|
|
|
case _:
|
|
raise ViewDoesNotExist()
|
|
|
|
|
|
class TokenView(LoginRequiredMixin, View):
|
|
def get(self, request, *arg, **kwargs):
|
|
return JsonResponse(craft_token(self.request.user.username, self.request.user.groups.filter(name="cof").exists()))
|
|
|
|
|
|
class LightView(TemplateView):
|
|
def get_template_names(self):
|
|
lights = settings.LIGHTS["lights"][self.kwargs["light"]]
|
|
return [f"frontend/{lights['kind']}.html"]
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
if self.request.user.is_authenticated:
|
|
context["jwt"] = craft_token(self.request.user.username, self.request.user.groups.filter(name="cof").exists())["token"]
|
|
context["websocket_endpoint"] = settings.WEBSOCKET_ENDPOINT
|
|
light = self.kwargs["light"]
|
|
if light not in settings.LIGHTS["lights"]:
|
|
raise Http404("Light does not exist")
|
|
lights = settings.LIGHTS["lights"][light]
|
|
context["lights"] = get_context_from_proj(lights["kind"], lights["channels"])
|
|
context["light_name"] = lights["name"]
|
|
|
|
return context
|
|
|
|
|
|
class HomeView(TemplateView):
|
|
template_name = "frontend/home.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["websocket_endpoint"] = settings.WEBSOCKET_ENDPOINT
|
|
lights = settings.LIGHTS["lights"]
|
|
context["lights"] = lights
|
|
|
|
return context
|