102 lines
2.9 KiB
Python
102 lines
2.9 KiB
Python
from django.shortcuts import render
|
|
from django.http import HttpResponse, HttpResponseBadRequest
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
from infoscreen.models import Rotation, InfoItem, InfoInstance
|
|
from infoscreen.hsl_fetcher import HSLFetcher
|
|
|
|
import json
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def index(request, idx, *args, **kwargs):
|
|
"""Render infoscreen index page."""
|
|
return render(request, 'infoscreen_index.html', {'rotation': idx})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def default(request, *args, **kwargs):
|
|
"""Try getting first rotation item."""
|
|
try:
|
|
first = Rotation.objects.all()[0].id
|
|
except:
|
|
first = 0
|
|
return index(request, first, *args, **kwargs)
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def get_apy_json(request):
|
|
"""Render APY diilikone page."""
|
|
return HttpResponse(
|
|
requests.get("https://api-diilikone.apy.fi/deals/top-groups").text)
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def rotation(request, idx, *args, **kwargs):
|
|
"""Get rotation."""
|
|
try:
|
|
rotation = Rotation.objects.get(pk=idx)
|
|
except Rotation.DoesNotExist:
|
|
resp = HttpResponse('{"error": "Rotation not found"}')
|
|
resp.status_code = 404
|
|
return resp
|
|
|
|
return HttpResponse(json.dumps(rotation.get_dict()))
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def rotations(request, *args, **kwargs):
|
|
"""Return rotation lists."""
|
|
rotations = list(map(lambda r: r.get_list(), Rotation.objects.all()))
|
|
return HttpResponse(json.dumps(rotations))
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def info_types(request, *args, **kwargs):
|
|
"""Return info item types."""
|
|
types = []
|
|
classes = InfoItem.get_subclasses()
|
|
for c in classes:
|
|
types.append({
|
|
"name": c.display_name,
|
|
"create_template_url": c.get_create_template_url(),
|
|
})
|
|
return HttpResponse(json.dumps(types))
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def info_items(request, *args, **kwargs):
|
|
"""Return Infoscreen items."""
|
|
items = []
|
|
classes = InfoItem.get_subclasses()
|
|
for c in classes:
|
|
for i in c.objects.all():
|
|
items.append(i.get_dict())
|
|
return HttpResponse(json.dumps(items))
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def hsl_timetable_settings(request, *args, **kwargs):
|
|
"""Set HSL timetable settings."""
|
|
d = {"departure_threshold": settings.HSL_DEPARTURE_THRESHOLD,
|
|
"hurry_threshold": settings.HSL_HURRY_THRESHOLD}
|
|
resp = json.dumps(d)
|
|
return HttpResponse(resp, status=200)
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def CurrentHSLView(request, *args, **kwargs):
|
|
"""Get HSL data and return it."""
|
|
fetcher = HSLFetcher()
|
|
fetcherThread = threading.Thread(target=fetcher.fetch_if_needed, args=[])
|
|
fetcherThread.setDaemon(False)
|
|
fetcherThread.start()
|
|
|
|
data = HSLDataModel.objects.all()
|
|
if len(data) < 1:
|
|
return HttpResponse(
|
|
'{"error" : "Could not find timetables from database."}',
|
|
status=500)
|
|
|
|
return HttpResponse(data[len(data) - 1].data, status=200)
|