diff options
author | Philip Sargent <philip.sargent@klebos.com> | 2022-03-15 17:04:43 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@klebos.com> | 2022-03-15 17:04:43 +0000 |
commit | 3390f51049c0dd45a995ce9d45804d2d05b96fda (patch) | |
tree | a95d6ee8387d39d94d3c0ec763c538ed6dd277ff /core/views/uploads.py | |
parent | fac748d2e24e5765c43350a247f2c92856c96b2d (diff) | |
download | troggle-3390f51049c0dd45a995ce9d45804d2d05b96fda.tar.gz troggle-3390f51049c0dd45a995ce9d45804d2d05b96fda.tar.bz2 troggle-3390f51049c0dd45a995ce9d45804d2d05b96fda.zip |
Form creates wallet folder and contents.json
Diffstat (limited to 'core/views/uploads.py')
-rw-r--r-- | core/views/uploads.py | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/core/views/uploads.py b/core/views/uploads.py index d227bda..3ccc587 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -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): |