summaryrefslogtreecommitdiffstats
path: root/core/views/caves.py
diff options
context:
space:
mode:
Diffstat (limited to 'core/views/caves.py')
-rw-r--r--core/views/caves.py141
1 files changed, 101 insertions, 40 deletions
diff --git a/core/views/caves.py b/core/views/caves.py
index b3c3c57..a971edc 100644
--- a/core/views/caves.py
+++ b/core/views/caves.py
@@ -10,7 +10,7 @@ from PIL import Image, ImageDraw, ImageFont
from django import forms
from django.conf import settings
from django.urls import reverse
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
@@ -120,10 +120,10 @@ def getnotablecaves():
cave = Cave.objects.get(kataster_number=kataster_number)
notablecaves.append(cave)
except:
- print(" ! FAILED to get only one cave per kataster_number OR invalid number for: "+kataster_number)
+ #print(" ! FAILED to get only one cave per kataster_number OR invalid number for: "+kataster_number)
caves = Cave.objects.all().filter(kataster_number=kataster_number)
for c in caves:
- print(c.kataster_number, c.slug())
+ #print(c.kataster_number, c.slug())
if c.slug() != None:
notablecaves.append(c)
return notablecaves
@@ -140,6 +140,9 @@ def cave3d(request, cave_id=''):
'''This is used to create a download url in templates/cave.html if anyone wants to download the .3d file
The caller template tries kataster first, then unofficial_number if that kataster number does not exist
but only if Cave.survex_file is non-empty
+
+ But the template file cave.html has its own ideas about the name of the file and thus the href. Ouch.
+ /cave/3d/<cave_id>
'''
try:
cave = getCave(cave_id)
@@ -157,49 +160,114 @@ def cave3d(request, cave_id=''):
def file3d(request, cave, cave_id):
'''Produces a .3d file directly for download.
- survex_file should be in format 'caves-1623/264/264.svx' but it might be mis-entered as simply '2012-ns-10.svx'
- Here we only use the stem of the last part anyway.
+ survex_file should be in valid path format 'caves-1623/264/264.svx' but it might be mis-entered as simply '2012-ns-10.svx'
+
+ Also the cave.survex_file may well not match the cave description path:
+ e.g. it might be to the whole system 'smk-system.svx' instead of just for the specific cave.
- TO DO properly decide whether we want to use the stem of the .svx file or the cave slug . This assumes they are the same...
+ - If the expected .3d file corresponding to cave.survex_file is present, return it.
+ - If the cave.survex_file exists, generate the 3d file, cache it and return it
+ - Use the cave_id to guess what the 3d file might be and, if in the cache, return it
+ - Use the cave_id to guess what the .svx file might be and generate the .3d file and return it
+ - (Use the incomplete cave.survex_file and a guess at the missing directories to guess the real .svx file location ?)
'''
- survexfilename = Path(settings.SURVEX_DATA, cave.survex_file)
- threedfilename = Path(settings.THREEDCACHEDIR, cave_id +'.3d') # assumes cave_id is stem of survex_file. oops.
- threedcachedir = Path(settings.THREEDCACHEDIR)
-
- if not threedfilename.is_file() or os.path.getmtime(survexfilename) > os.path.getmtime(threedfilename):
+ def runcavern(survexpath):
+ #print(" - Regenerating cavern .log and .3d for '{}'".format(survexpath))
+ if not survexpath.is_file():
+ #print(" - - Regeneration ABORT\n - - from '{}'".format(survexpath))
+
try:
- op = subprocess.check_output([settings.CAVERN, "--log", "--output={}".format(threedcachedir), "{}".format(survexfilename)])
+ completed_process = subprocess.run([settings.CAVERN, "--log", "--output={}".format(settings.THREEDCACHEDIR), "{}".format(survexpath)])
except OSError as ex:
- # propagate this to caller
- raise OSError(op) from ex
+ # propagate this to caller.
+ raise OSError(completed_process.stdout) from ex
+
+ op3d = (Path(settings.THREEDCACHEDIR) / Path(survexpath).name).with_suffix('.3d')
+ op3dlog = Path(op3d.with_suffix('.log'))
+
+ if not op3d.is_file():
+ print(" - - Regeneration FAILED\n - - from '{}'\n - - to '{}'".format(survexpath, op3d))
+ print(" - - Regeneration stdout: ", completed_process.stdout)
+ print(" - - Regeneration cavern log output: ", op3dlog.read_text())
+
+
+ def return3d(threedpath):
+ if threedpath.is_file():
+ response = HttpResponse(content=open(threedpath, 'rb'), content_type='application/3d')
+ response['Content-Disposition'] = 'attachment; filename={}'.format(threedpath.name)
+ return response
+ else:
+ message = '<h1>Path provided does not correspond to any actual 3d file.</h1><p>path: "{}"'.format(threedpath)
+ #print(message)
+ return HttpResponseNotFound(message)
+
+ survexname = Path(cave.survex_file).name # removes directories
+ survexpath = Path(settings.SURVEX_DATA, cave.survex_file)
+ threedname = Path(survexname).with_suffix('.3d') # removes .svx, replaces with .3d
+ threedpath = Path(settings.THREEDCACHEDIR, threedname)
+ threedcachedir = Path(settings.THREEDCACHEDIR)
- if threedfilename.is_file():
- response = HttpResponse(content=open(threedfilename, 'rb'), content_type='application/3d')
- response['Content-Disposition'] = 'attachment; filename={}.3d'.format(cave_id)
- # response['X-Sendfile'] = "%s.3d" % cave_id
- # It's usually a good idea to set the 'Content-Length' header too.
- # You can also set any other required headers: Cache-Control, etc.
- return response
+ # These if statements need refactoring more cleanly
+ if cave.survex_file:
+ #print(" - cave.survex_file '{}'".format(cave.survex_file))
+ if threedpath.is_file():
+ #print(" - threedpath '{}'".format(threedpath))
+ # possible error here as several .svx files of same names in different directories will overwrite in /3d/
+ if survexpath.is_file():
+ if os.path.getmtime(survexpath) > os.path.getmtime(threedpath):
+ runcavern(survexpath)
+ return return3d(threedpath)
+ else:
+ #print(" - - survexpath '{}'".format(survexpath))
+ if survexpath.is_file():
+ #print(" - - - survexpath '{}'".format(survexpath))
+ runcavern(survexpath)
+ return return3d(threedpath)
+
+ # Get here if cave.survex_file was set but did not correspond to a valid svx file
+ if survexpath.is_file():
+ # a file, but invalid format
+ message='<h1>File is not valid .svx format.</h1><p>Could not generate 3d file from "{}"'.format(survexpath)
+ else:
+ # we could try to guess that 'caves-1623/' is missing,... nah.
+ message = '<h1>Path provided does not correspond to any actual file.</h1><p>path: "{}"'.format(survexpath)
+
+ return HttpResponseNotFound(message)
+
+
+def rendercave(request, cave, slug, cave_id=''):
+ '''Gets the data and files ready and then triggers Django to render the template.
+ The resulting html contains urls which are dispatched independently, e.g. the 'download' link
+ '''
+ #print(" ! rendercave:'{}' slug:'{}' cave_id:'{}'".format(cave, slug, cave_id))
+
+ if cave.non_public and settings.PUBLIC_SITE and not request.user.is_authenticated():
+ return render(request, 'nonpublic.html', {'instance': cave, 'cavepage': True, 'cave_id': cave_id})
else:
- return None
+ svxstem = Path(cave.survex_file).parent / Path(cave.survex_file).stem
+ svx3d = Path(cave.survex_file).stem
+ # NOTE the template itself loads the 3d file using javascript before it loads anything else.
+ # Django cannot see what this javascript is doing, so we need to ensure that the 3d file exists first.
+ # run this just for the side-effect of generating the 3d file? Nope, crashes.
+ # TO DO - restructure this so that the regeneration is callable form here.
+ #discard_response = cave3d(request, cave_id=cave_id)
+ return render(request,'cave.html', {'settings': settings, 'cave': cave, 'cavepage': True,
+ 'cave_id': cave_id, 'svxstem': svxstem, 'svx3d':svx3d})
def cavepage(request, karea, subpath):
'''Displays a cave description page
+ accessed by kataster area number specifically
There are A LOT OF URLS to e.g. /1623/161/l/rl89a.htm which are IMAGES and html files
in cave descriptions. These need to be handled HERE
'''
path = karea + subpath
- print(" ! cavepage:'{}' kataster area:'{}' rest of path:'{}'".format(path, karea, subpath))
+ #print(" ! cavepage:'{}' kataster area:'{}' rest of path:'{}'".format(path, karea, subpath))
try:
cave = Cave.objects.get(url = path) # ideally this will be unique
- slug = cave.slug()
- print(" - cavepage:'{}' cave:'{}' cave-slug:'{}'".format(path, str(cave), slug))
- if cave.non_public and settings.PUBLIC_SITE and not request.user.is_authenticated():
- return render(request,'nonpublic.html', {'instance': cave, 'cave_editable': slug})
- else:
- return render(request,'cave.html', {'cave': cave, 'cave_editable': slug})
+ return rendercave(request, cave, cave.slug())
+
except Cave.DoesNotExist:
# probably a link to text or an image e.g. 1623/161/l/rl89a.htm i.e. an expoweb page
return expo.expopage(request, path)
@@ -209,14 +277,10 @@ def cavepage(request, karea, subpath):
except:
return render(request, 'pagenotfound.html', {'path': path})
-def caveSlug(request, slug):
- cave = Cave.objects.get(caveslug__slug = slug)
- if cave.non_public and settings.PUBLIC_SITE and not request.user.is_authenticated():
- return render(request,'nonpublic.html', {'instance': cave, 'cave_editable': slug})
- else:
- return render(request,'cave.html', {'cave': cave, 'cave_editable': slug})
-
def cave(request, cave_id='', offical_name=''):
+ '''Displays a cave description page
+ accesssed by a fairly random id which might be anything
+ '''
try:
cave=getCave(cave_id)
except MultipleObjectsReturned:
@@ -227,10 +291,7 @@ def cave(request, cave_id='', offical_name=''):
except:
return render(request, 'svxcavesingle404.html', {'settings': settings })
- if cave.non_public and settings.PUBLIC_SITE and not request.user.is_authenticated():
- return render(request, 'nonpublic.html', {'instance': cave, 'cavepage': True, 'cave_id': cave_id})
- else:
- return render(request,'cave.html', {'settings': settings, 'cave': cave, 'cavepage': True, 'cave_id': cave_id})
+ return rendercave(request, cave, cave.slug(), cave_id=cave_id)
def caveEntrance(request, slug):
cave = Cave.objects.get(caveslug__slug = slug)