diff --git a/core/views/uploads.py b/core/views/uploads.py index f76b38b..82e34c1 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, contentsjson, CopyWalletData +from troggle.parsers.scans import wallet_blank_json, contentsjson # 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 @@ -203,12 +203,12 @@ 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. + The Wallet object and the contents.json file are created when the user + first uploads files. 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. + You will find the Django documentation on forms very confusing, + as it covers many very differnet things we do not need. This is simpler. This subsumes much of the code which was in the old wallets.py script and so this function is very long indeed and needs refactoring. @@ -219,6 +219,48 @@ def scanupload(request, path=None): filesaved = False actual_saved = [] + def save_json(jsondict): + 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(jsondict, jfile, indent = 1) + print(f'--- FINISHED saving to JSON at {contents_path}') + + def make_wallet(walletname): + '''We need a wallet Object so that the django template stuff can find the files + ''' + try: + w, created = Wallet.objects.get_or_create(walletname=walletname) + print(f'wallet string {walletname}, wallet object {w} created new?: {created}') + if created: + w.fpath = Path(settings.SCANS_ROOT, walletname[0:4], walletname) + w.save() + except: + print(f'wallet string {walletname}, FAIL TO GET or create WALLET OBJECT') + raise + + def commit_json(): + destfolder = contents_path.parent + dr_add = subprocess.run([git, "add", contentsjson], cwd=destfolder, capture_output=True, text=True) + if dr_add.returncode != 0: + msgdata = 'Ask a nerd to fix this.\n--' + dr_add.stderr + '\n--' + dr_add.stdout + '\n--return code: ' + str(dr_add.returncode) + message = f'CANNOT git on server for this file {contentsjson}. Edits saved but not added to git.\n\n' + msgdata + print(message) + return render(request,'errors/generic.html', {'message': message}) + else: + dr_commit = subprocess.run([git, "commit", "-m", f'Update of {contentsjson} in wallet'], cwd=destfolder, capture_output=True, text=True) + # This produces return code = 1 if it commits OK + if dr_commit.returncode != 0: + msgdata = 'Ask a nerd to fix this.\n\n' + dr_commit.stderr + '\n\n' + dr_commit.stdout + '\n\nreturn code: ' + str(dr_commit.returncode) + message = f'Error code with git on server for this {contentsjson}. File is added to git, but NOT committed.\n' + msgdata + print(message) + return render(request,'errors/generic.html', {'message': message}) + + checkboxes = ["description written", "survex not required", "qms written", "website updated", "plan not required", "plan drawn", "elev not required", "elev drawn", "electronic survey" ] if path: @@ -301,45 +343,9 @@ 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 at {contents_path}') - - # This commits it, - # needs the troggle object wallet, not a string - - try: - w, created = Wallet.objects.get_or_create(walletname=wallet) - print(f'wallet string {wallet}, wallet object {w} created new?: {created}') - if created: - w.fpath = Path(settings.SCANS_ROOT, wallet[0:4], wallet) - w.save() - 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: - msgdata = 'Ask a nerd to fix this.\n--' + dr_add.stderr + '\n--' + dr_add.stdout + '\n--return code: ' + str(dr_add.returncode) - message = f'CANNOT git on server for this file {contentsjson}. Edits saved but not added to git.\n\n' + msgdata - print(message) - return render(request,'errors/generic.html', {'message': message}) - else: - dr_commit = subprocess.run([git, "commit", "-m", f'Update of {contentsjson} in wallet'], cwd=destfolder, capture_output=True, text=True) - # This produces return code = 1 if it commits OK - if dr_commit.returncode != 0: - msgdata = 'Ask a nerd to fix this.\n\n' + dr_commit.stderr + '\n\n' + dr_commit.stdout + '\n\nreturn code: ' + str(dr_commit.returncode) - message = f'Error code with git on server for this {contentsjson}. File is added to git, but NOT committed.\n' + msgdata - print(message) - return render(request,'errors/generic.html', {'message': message}) + save_json(wd) + make_wallet(wallet) + commit_json() else: print(f'--- INVALID JSON Update form submitted') @@ -357,7 +363,7 @@ def scanupload(request, path=None): form = FilesForm(request.POST,request.FILES) if form.is_valid(): - f = request.FILES["uploadfiles"] + #f = request.FILES["uploadfiles"] multiple = request.FILES.getlist('uploadfiles') fs = FileSystemStorage(os.path.join(dirpath)) # creates wallet folder if necessary @@ -373,20 +379,9 @@ def scanupload(request, path=None): actual_saved.append(saved_filename) # print(f'! - FORM scanupload multiple {actual_saved}') filesaved = True - - # 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 ) - + save_json(wallet_blank_json) + make_wallet(wallet) + commit_json() files = [] dirs = [] # print(f'! - FORM scanupload - start {wallet} {dirpath}') @@ -399,7 +394,7 @@ def scanupload(request, path=None): if f.is_file(): files.append(f.name) except FileNotFoundError: - files.append('(no wallet yet. It would be created if you upload a scan)') + files.append('(no wallet yet. It would be created if you upload a scan and then save the form with a date.)') else: create = True diff --git a/parsers/scans.py b/parsers/scans.py index 400bd74..1dbf17a 100644 --- a/parsers/scans.py +++ b/parsers/scans.py @@ -28,7 +28,7 @@ git = settings.GIT wallet_blank_json = { "cave": "", "date": "", - "description url": "/caves", + "description url": "1623/XXX", "description written": False, "electronic survey": False, "elev drawn": False, @@ -43,23 +43,7 @@ wallet_blank_json = { "survex file": [], "survex not required": False, "website updated": False} - -# wallet_blank_html = '''
List of trips: expedition/YEAR -# - troggle-processed .svx files and logbook entries on server
-#Date:
People: Unknown,
-#Cave Guidebook description -# - A description is indicated as being needed, so may need adding into this cave page. -#
Survex file: not identified yet -#
The description needs writing
-#The QMs needs writing
The website is marked as needing updating (using the guidebook description)
-#Tunnel / Therion drawing files need drawing
-#