Form creates wallet folder and contents.json

This commit is contained in:
Philip Sargent 2022-03-15 17:04:43 +00:00
parent fac748d2e2
commit 3390f51049
4 changed files with 90 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import re, os import re, os
import subprocess import subprocess
import json
from pathlib import Path from pathlib import Path
from django import forms from django import forms
@ -15,6 +16,7 @@ from django.core.files.storage import FileSystemStorage, default_storage
#from troggle import settings #from troggle import settings
from troggle.parsers.imports import import_caves, import_people, import_surveyscans from troggle.parsers.imports import import_caves, import_people, import_surveyscans
from troggle.parsers.imports import import_logbooks, import_QMs, import_drawingsfiles, import_survex from troggle.parsers.imports import import_logbooks, import_QMs, import_drawingsfiles, import_survex
from troggle.parsers.scans import wallet_blank_json, wallet_blank_html, contentsjson, indexhtml
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time* # from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
from troggle.core.models.troggle import Expedition, Person, PersonExpedition from troggle.core.models.troggle import Expedition, Person, PersonExpedition
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
@ -76,10 +78,12 @@ def scanupload(request, wallet=None):
if int(wnumber) == 0: if int(wnumber) == 0:
prev = f'{int(wnumber):02d}' prev = f'{int(wnumber):02d}'
context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty} context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty}
wallet = wallet.replace(':','#') wallet = wallet.replace(':','#')
dirpath = Path(settings.SURVEY_SCANS, year, wallet) dirpath = Path(settings.SURVEY_SCANS, year, wallet)
walletdata = dirpath / contentsjson
form = FilesForm() form = FilesForm()
@ -90,7 +94,7 @@ def scanupload(request, wallet=None):
if form.is_valid(): if form.is_valid():
f = request.FILES["uploadfiles"] f = request.FILES["uploadfiles"]
multiple = request.FILES.getlist('uploadfiles') multiple = request.FILES.getlist('uploadfiles')
fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, wallet)) fs = FileSystemStorage(os.path.join(dirpath)) # creates wallet folder if necessary
actual_saved = [] actual_saved = []
if multiple: if multiple:
@ -99,9 +103,24 @@ def scanupload(request, wallet=None):
# print(f'! - FORM scanupload multiple {actual_saved}') # print(f'! - FORM scanupload multiple {actual_saved}')
filesaved = True filesaved = True
# Wallet folder created, but index and contents.json need to be created.
contents_path = dirpath / contentsjson
if not contents_path.is_file(): # double-check
with open(contents_path, "w") as json_file:
json.dump(wallet_blank_json, json_file, sort_keys=True, indent = 1)
index_path = dirpath / indexhtml
if not index_path.is_file(): # double-check
thishtml = wallet_blank_html.replace("YEAR", str(year))
thishtml = thishtml.replace("WALLET", str(wallet))
with open(index_path, "w") as html_file:
html_file.write(thishtml )
files = [] files = []
dirs = [] dirs = []
# print(f'! - FORM scanupload - start {wallet} {dirpath}') # print(f'! - FORM scanupload - start {wallet} {dirpath}')
if dirpath.is_dir():
create = False
try: try:
for f in dirpath.iterdir(): for f in dirpath.iterdir():
if f.is_dir(): if f.is_dir():
@ -110,17 +129,18 @@ def scanupload(request, wallet=None):
if f.name != 'contents.json' and f.name != 'walletindex.html': if f.name != 'contents.json' and f.name != 'walletindex.html':
files.append(f.name) files.append(f.name)
except FileNotFoundError: except FileNotFoundError:
files.append('(no wallet yet - would be created)') files.append('(no wallet yet. It would be created if you upload a scan)')
if len(files) ==0 :
files.append('(no image files in wallet)')
else: else:
create = True
if len(files) >0 :
files = sorted(files) files = sorted(files)
if dirs: if dirs:
dirs = sorted(dirs) dirs = sorted(dirs)
return render(request, 'scanuploadform.html', return render(request, 'scanuploadform.html',
{'form': form, 'wallet': wallet, **context, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved}) {'form': form, 'wallet': wallet, **context, 'files': files, 'dirs': dirs, 'create': create, 'filesaved': filesaved, 'actual_saved': actual_saved})
@login_required_if_public @login_required_if_public
def photoupload(request, folder=None): def photoupload(request, folder=None):

View File

@ -16,6 +16,44 @@ from troggle.core.utils import save_carefully, GetListDir
'''Searches through all the survey scans directories (wallets) in expofiles, looking for images to be referenced. '''Searches through all the survey scans directories (wallets) in expofiles, looking for images to be referenced.
''' '''
contentsjson = "contents.json"
indexhtml = "walletindex.html"
wallet_blank_json = {
"cave": "",
"date": "",
"description url": "/caves",
"description written": False,
"electronic survey": False,
"elev drawn": False,
"elev not required": False,
"name": "",
"people": [
"Unknown"
],
"plan drawn": False,
"plan not required": False,
"qms written": False,
"survex file": [],
"survex not required": False,
"website updated": False}
wallet_blank_html = '''<html><body><H1>Wallet WALLET</H1>
<p>List of trips: <a href="http://expo.survex.com/expedition/YEAR">expedition/YEAR</a>
- troggle-processed .svx files and logbook entries on server</p>
<p>Date: </p><p>People: Unknown,</p>
<p>Cave <a href='http://expo.survex.com/caves/'>Guidebook description</a>
- A description is indicated as being needed, so may need adding into this cave page.
<p>Survex file: not identified yet
<H2>Issues</H2>
<p>The description needs writing</p>
<p>The QMs needs writing</p><p>The website is marked as needing updating (using the guidebook description)</p>
<p>Tunnel / Therion drawing files need drawing</p>
<H2>Files</H2>
<UL>
</UL>
</body></html>
'''
def get_or_create_placeholder(year): def get_or_create_placeholder(year):
""" All surveys must be related to a logbookentry. We don't have a way to """ All surveys must be related to a logbookentry. We don't have a way to

View File

@ -32,16 +32,29 @@
<p> <p>
<b>File(s) saved as <br> <b>File(s) saved as <br>
{% for f in actual_saved %} {% for f in actual_saved %}
<em>{{f}}</em> <br> <em>{{f}}</em> <br></b>
{% endfor %} {% endfor %}
<br><br>Upload more?</b>
</p> </p>
<br>
{% endif %} {% endif %}
<p> <p>
{% for f in files %} {% for f in files %}
<a href="/expofiles/surveyscans/{{year}}/{{wallet|urlencode}}/{{f}}">{{ f}}</a><br /> <a href="/expofiles/surveyscans/{{year}}/{{wallet|urlencode}}/{{f}}">{{ f}}</a><br />
{% empty %}
<p>&lt;No files here&gt;
{% endfor %} {% endfor %}
</p> </p>
</div> {% endblock %} {% if create %}
This online wallet does not yet exist.
<br>It will be created and initialised automatically when you upload a file.
{% endif %}
<p><em>
<a href="/expofiles/surveyscans/{{year}}/{{wallet|urlencode}}/walletindex.html">Wallet status page</a>
<br />
<a href="/expofiles/surveyscans/{{year}}/walletindex.html">Wallet index for this year</a>
</em>
</div>
<hr />
{% endblock %}