2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-15 21:17:07 +00:00

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 subprocess
import json
from pathlib import Path
from django import forms
@@ -15,6 +16,7 @@ from django.core.files.storage import FileSystemStorage, default_storage
#from troggle import settings
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.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 troggle.core.models.troggle import Expedition, Person, PersonExpedition
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
@@ -75,11 +77,13 @@ def scanupload(request, wallet=None):
if int(wnumber) == 0:
prev = f'{int(wnumber):02d}'
context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty}
wallet = wallet.replace(':','#')
dirpath = Path(settings.SURVEY_SCANS, year, wallet)
walletdata = dirpath / contentsjson
form = FilesForm()
@@ -90,7 +94,7 @@ def scanupload(request, wallet=None):
if form.is_valid():
f = request.FILES["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 = []
if multiple:
@@ -98,29 +102,45 @@ def scanupload(request, wallet=None):
actual_saved.append( fs.save(f.name, content=f) )
# print(f'! - FORM scanupload multiple {actual_saved}')
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 = []
dirs = []
# print(f'! - FORM scanupload - start {wallet} {dirpath}')
try:
for f in dirpath.iterdir():
if f.is_dir():
dirs.append(f.name)
if f.is_file():
if f.name != 'contents.json' and f.name != 'walletindex.html':
files.append(f.name)
except FileNotFoundError:
files.append('(no wallet yet - would be created)')
if len(files) ==0 :
files.append('(no image files in wallet)')
if dirpath.is_dir():
create = False
try:
for f in dirpath.iterdir():
if f.is_dir():
dirs.append(f.name)
if f.is_file():
if f.name != 'contents.json' and f.name != 'walletindex.html':
files.append(f.name)
except FileNotFoundError:
files.append('(no wallet yet. It would be created if you upload a scan)')
else:
create = True
if len(files) >0 :
files = sorted(files)
if dirs:
dirs = sorted(dirs)
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
def photoupload(request, folder=None):