From 1eadc931cbf02879ad305416ebe9f6493ae71e8b Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 13 Aug 2022 21:14:57 +0300 Subject: [PATCH] Bugfix for git add for uploaded drawings --- core/views/uploads.py | 57 ++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/core/views/uploads.py b/core/views/uploads.py index 09f612c..6ebdecf 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -603,6 +603,7 @@ def dwgupload(request, folder=None, gitdisable='no'): if request.method == 'POST': form = FilesForm(request.POST,request.FILES) if form.is_valid(): + # print(f'! - FORM dwgupload - POST valid: "{request.FILES["uploadfiles"]}" ') f = request.FILES["uploadfiles"] multiple = request.FILES.getlist('uploadfiles') fs = FileSystemStorage(os.path.join(settings.DRAWINGS_DATA, folder)) @@ -616,29 +617,49 @@ def dwgupload(request, folder=None, gitdisable='no'): git = settings.GIT else: git = 'echo' + # print(f'git DISABLED {f.name}') if multiple: for f in multiple: + # print(f'! - FORM dwgupload - file {f} in {multiple=}') if dwgvalid(f.name): try: # crashes in Django os.chmod call if on WSL without metadata drvfs, but does save file! saved_filename = fs.save(f.name, content=f) except: - print(f'\n !! Permissions failure ?! on attempting to save file {f.name}') - if 'saved_filename' in locals(): - if saved_filename.is_file(): - actual_saved.append(saved_filename) - if gitdisable != 'yes': - dr_add = subprocess.run([git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True) + print(f'! - FORM dwgupload - \n!! Permissions failure ?! on attempting to save file {f.name}. Attempting to continue..') + if 'saved_filename' in locals(): + if Path(dirpath, saved_filename).is_file(): + actual_saved.append(saved_filename) + if gitdisable != 'yes': + dr_add = subprocess.run([git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True) + msgdata = dr_add.stderr + '\n' + dr_add.stdout + '\nreturn code: ' + str(dr_add.returncode) + # message = f'! - FORM dwgupload - Success: git ADD on server for this file {saved_filename}.' + msgdata + # print(message) if dr_add.returncode != 0: msgdata = 'Ask a nerd to fix this.\n\n' + dr_add.stderr + '\n\n' + dr_add.stdout + '\n\nreturn code: ' + str(dr_add.returncode) - message = f'CANNOT git on server for this file {saved_filename}. Edits saved but not added to git.\n\n' + msgdata + message = f'! - FORM dwgupload - CANNOT git ADD on server for this file {saved_filename}. Edits saved but not added to git.\n' + msgdata + print(message) return render(request,'errors/generic.html', {'message': message}) - dwgfile, created = DrawingFile.objects.get_or_create(dwgpath=saved_filename, dwgname=Path(f.name).stem, filesize=f.size) - dwgfile.save() + dwgfile, created = DrawingFile.objects.get_or_create(dwgpath=saved_filename, dwgname=Path(f.name).stem, filesize=f.size) + dwgfile.save() + else: + message = f'! - FORM dwgupload - NOT A FILE {Path(dirpath, saved_filename)=}. ' + print(message) + else: + message = f'! - FORM dwgupload - Save failure for {saved_filename}. Changes NOT saved.' + print(message) + return render(request,'errors/generic.html', {'message': message}) + + if saved_filename != f.name: + # message = f'! - FORM dwgupload - Save RENAME {f.name} renamed as {saved_filename}. This is OK.' + # print(message) + pass + else: refused.append(f.name) - print(f'REFUSED {f.name}') - if actual_saved: # maybe all were refused by the suffix test in dwgvalid() + # print(f'REFUSED {f.name}') + + if actual_saved: filesaved = True if len(actual_saved) > 1: dots = "..." @@ -646,15 +667,23 @@ def dwgupload(request, folder=None, gitdisable='no'): dots = "" if gitdisable != 'yes': dr_commit = subprocess.run([git, "commit", "-m", f'Drawings upload - {actual_saved[0]}{dots}'], cwd=dirpath, capture_output=True, text=True) + # message = f'! - FORM dwgupload - For uploading {actual_saved[0]}{dots}. Edits saved, added to git, and COMMITTED.\n' + msgdata + # print(message) # 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 {actual_saved[0]}{dots}. Edits saved, added to git, but NOT committed.\n\n' + msgdata + msgdata = 'Ask a nerd to fix this.\n\n' + dr_commit.stderr + '\n' + dr_commit.stdout + '\nreturn code: ' + str(dr_commit.returncode) + message = f'! - FORM dwgupload -Error code with git on server for this {actual_saved[0]}{dots}. Edits saved, added to git, but NOT committed.\n' + msgdata + print(message) return render(request,'errors/generic.html', {'message': message}) + else: + print(f' git disabled "{git=}"') + else:# maybe all were refused by the suffix test in dwgvalid() + message = f'! - FORM dwgupload - Nothing actually saved. All were refused. {actual_saved=}' + print(message) files = [] dirs = [] - # print(f'! - FORM dwgupload - start {folder} \n"{dirpath}" \n"{dirpath.parent}" \n"{dirpath.exists()}"') + # print(f'! - FORM dwgupload - start {folder=} \n"{dirpath=}" \n"{dirpath.parent=}" \n"{dirpath.exists()=}"') try: for f in dirpath.iterdir(): if f.is_dir():