gmba_django/app/views.py

66 lines
1.5 KiB
Python
Raw Normal View History

2021-07-27 08:13:27 +00:00
import os
from django.views.generic.base import TemplateView
2021-07-27 08:13:27 +00:00
from django.http import FileResponse
import os.path as ospath
from os import makedirs
from tempfile import gettempdir
from .formats import *
# Get temporary file storage
UPLOAD_PATH = gettempdir()
DATA_PATH = ospath.join(ospath.dirname(__file__), '..')
if not ospath.exists(DATA_PATH):
makedirs(DATA_PATH)
def get_datafile(fmt):
return ospath.join(
DATA_PATH,
fmt['folder'],
fmt['filename'] + '.' + fmt['extension']
)
class HomePageView(TemplateView):
template_name = "app/index.html"
def get_context_data(self, **kwargs):
fmts = DATAFORMATS
for f in fmts:
f['ready'] = ospath.isfile(get_datafile(f))
context = super().get_context_data(**kwargs)
return context
2021-07-27 08:13:27 +00:00
class OfflinePageView(TemplateView):
template_name = "app/offline.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
2021-07-27 21:53:02 +00:00
class DemoPageView(TemplateView):
template_name = "app/demo.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
2021-07-27 08:13:27 +00:00
def send_from_file(request, path):
request_for = ''
if request.path.startswith('/geodata'):
request_for = 'geodata'
elif request.path.startswith('/data'):
request_for = 'data'
file_path = ospath.join(DATA_PATH, request_for, path)
file_to_send = open(file_path, 'rb')
response = FileResponse(file_to_send)
return response