From edfba8d3554efa2b1bf96939ed5cbc59f97e93e5 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 13 Aug 2022 23:56:56 +0300 Subject: [PATCH] git commit edits to wallet data --- core/views/uploads.py | 29 ++++++++++++++++++++++------- parsers/scans.py | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/core/views/uploads.py b/core/views/uploads.py index 6ebdecf..369e297 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -198,7 +198,7 @@ def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl): # @login_required_if_public 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. + Also display AND EDIT the contents.json data in the wallet. 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. @@ -208,6 +208,7 @@ def scanupload(request, path=None): REWRITE bits using the ticklist, dateify, caveify, populate etc utility functions in core.view.scans.py ''' + git = settings.GIT filesaved = False actual_saved = [] @@ -257,7 +258,6 @@ def scanupload(request, path=None): wurl = f"/scanupload/{wallet}".replace('#', ':') wallet = wallet.replace(':','#') dirpath = Path(settings.SCANS_ROOT, year, wallet) - #contents_path = dirpath / contentsjson contents_path = Path(settings.DRAWINGS_DATA, "walletjson") / year / wallet / contentsjson form = FilesForm() @@ -299,7 +299,7 @@ def scanupload(request, path=None): json.dump(wd, jfile, indent = 1) # print(f'--- FINISHED saving to JSON\n') - # This copies the new data to the drawings repo and commit it + # This copies the new data to the drawings repo and commits it # needs the troggle object wallet, not a string try: @@ -308,17 +308,32 @@ def scanupload(request, path=None): if created: w.fpath = Path(settings.SCANS_ROOT, wallet[0:4], wallet) w.save() - CopyWalletData(w) except: - print(f'wallet string {wallet}, FAIL TO GET WALLET OBJECT, maybe we need to create it ?') + print(f'wallet string {wallet}, FAIL TO GET or create WALLET OBJECT') raise - + + 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}) + else: print(f'--- INVALID JSON Update form submitted') print(formj.errors) return render(request,'errors/generic.html', {'message': formj.errors}) - else: + else: # not editing wallet data, uploading a file.. form = FilesForm(request.POST,request.FILES) if form.is_valid(): diff --git a/parsers/scans.py b/parsers/scans.py index cf4a100..28d0325 100644 --- a/parsers/scans.py +++ b/parsers/scans.py @@ -102,7 +102,7 @@ def LoadListScansFile(wallet): c = 0 def CopyWalletData(wallet): '''Copies all the contents.json to a parallel set of folders in the drawings repo - refreshes everything during a ful import, but it should all be up to date as every time + refreshes everything during a full import, but it should all be up to date as every time wallet data gets saved it should also be copied across and committed. ''' # not needed now the drawinsg repo is the master