gmba_django/app/views.py

90 lines
2.3 KiB
Python
Raw Normal View History

2021-07-27 08:13:27 +00:00
import os
2021-07-28 04:19:13 +00:00
from django.contrib import messages
from django.conf import settings
from django.views.generic.base import TemplateView
2021-07-28 04:19:13 +00:00
from django.http import FileResponse , HttpResponsePermanentRedirect
from django.urls import reverse
import os.path as ospath
from os import makedirs
from tempfile import gettempdir
from .formats import *
2021-07-28 04:19:13 +00:00
from .convert import reindex_data
# 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']
)
2021-07-28 04:19:13 +00:00
# Data update tracking
c_progress = 0
c_filename = ""
2021-07-28 04:19:13 +00:00
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):
2021-07-28 04:19:13 +00:00
template_name = "app/demo.html"
2021-07-27 21:53:02 +00:00
2021-07-28 04:19:13 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
class ReindexPageView(TemplateView):
2021-07-27 21:53:02 +00:00
template_name = "app/demo.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
2021-07-28 04:19:13 +00:00
def post(self, request, **kwargs):
global c_progress
c_progress = 0
global c_filename
c_filename = ""
reindex_data()
messages.add_message(request, messages.INFO, 'Search engine refresh complete')
print("Search engine reindexed")
return HttpResponsePermanentRedirect(reverse('admin:index'))
2021-07-27 21:53:02 +00:00
2021-07-28 04:19:30 +00:00
def send_from_file(request, filename):
2021-07-27 08:13:27 +00:00
request_for = ''
if request.path.startswith('/geodata'):
request_for = 'geodata'
elif request.path.startswith('/data'):
request_for = 'data'
2021-07-28 04:19:30 +00:00
file_path = ospath.join(DATA_PATH, request_for, filename)
2021-07-27 08:13:27 +00:00
file_to_send = open(file_path, 'rb')
response = FileResponse(file_to_send)
return response