diff options
author | Philip Sargent <philip.sargent@klebos.com> | 2022-08-14 22:52:14 +0300 |
---|---|---|
committer | Philip Sargent <philip.sargent@klebos.com> | 2022-08-14 22:52:14 +0300 |
commit | 284e044a03caccab0d5dfa2cd1ac56949b80c20b (patch) | |
tree | fd8a96bf6b63bb10f829086d199cb16fc3412b1f /core/views/uploads.py | |
parent | b093d00ff46477c2fc7042bfd19962098cf6002c (diff) | |
download | troggle-284e044a03caccab0d5dfa2cd1ac56949b80c20b.tar.gz troggle-284e044a03caccab0d5dfa2cd1ac56949b80c20b.tar.bz2 troggle-284e044a03caccab0d5dfa2cd1ac56949b80c20b.zip |
Fix wallets scan upload faults
Diffstat (limited to 'core/views/uploads.py')
-rw-r--r-- | core/views/uploads.py | 54 |
1 files changed, 38 insertions, 16 deletions
diff --git a/core/views/uploads.py b/core/views/uploads.py index 369e297..f76b38b 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -23,7 +23,7 @@ from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned #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, CopyWalletData +from troggle.parsers.scans import wallet_blank_json, contentsjson, CopyWalletData # from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time* from troggle.core.models.troggle import DataIssue from troggle.core.models.troggle import Expedition, Person, PersonExpedition @@ -63,6 +63,9 @@ class FilesForm(forms.Form): # not a model-form, just a form-form class FilesRenameForm(forms.Form): # not a model-form, just a form-form uploadfiles = forms.FileField() renameto = forms.CharField(strip=True, required=False) + +class WalletGotoForm(forms.Form): # not a model-form, just a form-form + walletgoto = forms.CharField(strip=True, required=False) class TextForm(forms.Form): # not a model-form, just a form-form photographer = forms.CharField(strip=True) @@ -200,6 +203,10 @@ def scanupload(request, path=None): '''Upload scanned image files into a wallet on /expofiles Also display AND EDIT the contents.json data in the wallet. + The Wallet object and the contents.json file are only created when the user + edits the JSON data, not when they upload files. This may be a bad idea + as the wallet will not appear in reports untilt his is done. + This does NOT use a Django model linked to a Django form. Just a simple Django form. You will find the Django documentation on forms very confusing, This is simpler. @@ -269,7 +276,6 @@ def scanupload(request, path=None): # Also lots of hassle with lists of strings interpreted as a single string # Unset checkboxes do not return any value, checked ones return "True". So need initialising to False if formj.is_valid(): - #print(f'--- JSON Update form is VALID, saving to {contents_path}') posted = request.POST.copy() posted.pop("csrfmiddlewaretoken") # discard this wd = wallet_blank_json @@ -295,11 +301,17 @@ def scanupload(request, path=None): wd["survex file"][i] = elem.strip() #print(f'--- {wd["survex file"]} - {type(wd["survex file"])}') + newfolder = contents_path.parent + print(f'--- wallet directory in :drawings: repo {newfolder=}') + if not os.path.exists(contents_path.parent): + print(f'--- No wallet directory in :drawings: repo, so creating it') + os.makedirs(contents_path.parent) + with open(contents_path, "w") as jfile: json.dump(wd, jfile, indent = 1) - # print(f'--- FINISHED saving to JSON\n') + print(f'--- FINISHED saving to JSON at {contents_path}') - # This copies the new data to the drawings repo and commits it + # This commits it, # needs the troggle object wallet, not a string try: @@ -311,7 +323,8 @@ def scanupload(request, path=None): except: print(f'wallet string {wallet}, FAIL TO GET or create WALLET OBJECT') raise - + + # now we do the git add and commit destfolder = contents_path.parent dr_add = subprocess.run([git, "add", contentsjson], cwd=destfolder, capture_output=True, text=True) if dr_add.returncode != 0: @@ -333,6 +346,13 @@ def scanupload(request, path=None): print(formj.errors) return render(request,'errors/generic.html', {'message': formj.errors}) + elif "walletgoto" in request.POST: # not editing wallet data, uploading a file.. going direct to a named wallet + formg = WalletGotoForm(request.POST,request.FILES) + if formg.is_valid(): + walletgoto = request.POST["walletgoto"] + + return HttpResponseRedirect(f'/scanupload/{walletgoto.replace("#",":")}') + else: # not editing wallet data, uploading a file.. form = FilesForm(request.POST,request.FILES) @@ -354,15 +374,18 @@ def scanupload(request, path=None): # print(f'! - FORM scanupload multiple {actual_saved}') filesaved = True - 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 ) + # Don't do this when uploading a file. + # The contents.json file will be created when the wallet data is edited for the first time. + # if not contents_path.is_file(): # double-check: it doesn't exist yet so create it when uploading a file. + # 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 it doesn't exist yet + # 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 = [] @@ -374,8 +397,7 @@ def scanupload(request, path=None): 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) + files.append(f.name) except FileNotFoundError: files.append('(no wallet yet. It would be created if you upload a scan)') else: |