summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@klebos.com>2022-03-05 22:16:03 +0000
committerPhilip Sargent <philip.sargent@klebos.com>2022-03-05 22:16:03 +0000
commit7a58aac08eb0b19b05f84955d855d8af6bb369de (patch)
tree91a3cfd135dac2d058df0aa78480201ae01f31ce /core
parenta3a65524b86abde3bc924d611620f71f87b7bf36 (diff)
downloadtroggle-7a58aac08eb0b19b05f84955d855d8af6bb369de.tar.gz
troggle-7a58aac08eb0b19b05f84955d855d8af6bb369de.tar.bz2
troggle-7a58aac08eb0b19b05f84955d855d8af6bb369de.zip
Drawings uploads git works
Diffstat (limited to 'core')
-rw-r--r--core/views/drawings.py27
-rw-r--r--core/views/uploads.py33
2 files changed, 43 insertions, 17 deletions
diff --git a/core/views/drawings.py b/core/views/drawings.py
index 4145123..d6a1599 100644
--- a/core/views/drawings.py
+++ b/core/views/drawings.py
@@ -32,25 +32,30 @@ def dwgallfiles(request):
def dwgfilesingle(request, path):
'''sends a single binary file to the user, We should have a renderer that syntax-colours this Tunnel xml
- but it might be a Therion file
+ but it might be a Therion file. And it could be an old PNG, PDF or SVG for that matter,
+ so we should attempt to render it
'''
try:
dwgfile = DrawingFile.objects.get(dwgpath=urlunquote(path))
except:
message = f'Drawing file error or not found \'{path}\' .'
return render(request, 'errors/generic.html', {'message': message})
-
+
tfile = Path(settings.DRAWINGS_DATA, dwgfile.dwgpath)
- try: # for display not download
- return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml")
- except UnicodeDecodeError:
- try:
- return HttpResponse(content=open(tfile,encoding='iso-8859-1'), content_type="text/xhtml")
- except:
- return HttpResponse(content=open(tfile,mode='rb'), content_type="text/xhtml")
+
+ if Path(dwgfile.dwgpath).suffix in ['.xml', 'th2', '.th']:
+ try: # for display not download
+ return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml")
+ except UnicodeDecodeError:
+ try:
+ return HttpResponse(content=open(tfile,encoding='iso-8859-1'), content_type="text/xhtml")
+ except:
+ return HttpResponse(content=open(tfile,mode='rb'), content_type="text/xhtml")
+ else:
+ return HttpResponse(content=open(tfile, errors='ignore'), content_type="text/xhtml")
else:
- return HttpResponse(content=open(tfile, errors='ignore'), content_type="text/xhtml")
+ return HttpResponse(content="Unable to understand the encoding for this file: not UTF-8 nor iso-8859-1, or some other read error happened.")
else:
- return HttpResponse(content="Unable to understand the encoding for this file: not UTF-8 nor iso-8859-1, or some other read error happened.")
+ return HttpResponse(content=open(tfile,"rb"), content_type=getmimetype(tfile))
diff --git a/core/views/uploads.py b/core/views/uploads.py
index 6819e3d..9301aba 100644
--- a/core/views/uploads.py
+++ b/core/views/uploads.py
@@ -114,10 +114,19 @@ def dwgupload(request, folder=None, gitdisable='no'):
def dwgvalid(name):
if name in [ '.gitignore', ]:
return False
- if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.jpg', '.pdf', '.jpeg', '.txt']:
+ if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.txt']:
return True # dangerous, we should check the actual file binary signature
return False
-
+
+ def dwgvaliddisp(name):
+ '''OK to display, even if we are not going to allow a new one to be uploaded
+ '''
+ if name in [ '.gitignore', ]:
+ return False
+ if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.txt', '.jpg', '.jpeg', '.png', '.pdf']:
+ return True # dangerous, we should check the actual file binary signature
+ return False
+
filesaved = False
actual_saved = []
refused = []
@@ -158,7 +167,11 @@ def dwgupload(request, folder=None, gitdisable='no'):
saved_filename = fs.save(f.name, content=f)
actual_saved.append(saved_filename)
if gitdisable != 'yes':
- subprocess.call([git, "add", saved_filename], cwd=dirpath)
+ dr_add = subprocess.run([git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True)
+ 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
+ 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()
else:
@@ -166,9 +179,17 @@ def dwgupload(request, folder=None, gitdisable='no'):
print(f'REFUSED {f.name}')
if actual_saved: # maybe all were refused by the suffix test in dwgvalid()
filesaved = True
+ if len(actual_saved) > 1:
+ dots = "..."
+ else:
+ dots = ""
if gitdisable != 'yes':
- subprocess.call([git, "commit", "-m", f'Drawings upload - {list(multiple)}'], cwd=dirpath)
-
+ dr_commit = subprocess.run([git, "commit", "-m", f'Drawings upload - {actual_saved[0]}{dots}'], cwd=dirpath, 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 {actual_saved[0]}{dots}. Edits saved, added to git, but NOT committed.\n\n' + msgdata
+ return render(request,'errors/generic.html', {'message': message})
files = []
dirs = []
@@ -180,7 +201,7 @@ def dwgupload(request, folder=None, gitdisable='no'):
dirs.append(f.name)
continue
if f.is_file():
- if dwgvalid(f.name):
+ if dwgvaliddisp(f.name):
files.append(f.name)
continue
except FileNotFoundError: