import os from django.views.generic.base import TemplateView 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 class OfflinePageView(TemplateView): template_name = "app/offline.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) return context class DemoPageView(TemplateView): template_name = "app/demo.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) return context 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