Drawings uploads git works

This commit is contained in:
Philip Sargent 2022-03-05 22:16:03 +00:00
parent a3a65524b8
commit 7a58aac08e
4 changed files with 54 additions and 21 deletions

View File

@ -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))

View File

@ -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:

View File

@ -179,7 +179,7 @@ def settnlfileinfo(dwgfile):
def setdrwfileinfo(dwgfile):
'''Read in the drawing file contents and sets values on the dwgfile object,
but these are PDFs or .txt files, so there is no useful format to search for
but these are SVGs, PDFs or .txt files, so there is no useful format to search for
This function is a placeholder in case we thnk of a way to do something
to recognise generic survex filenames.
'''
@ -190,7 +190,6 @@ def setdrwfileinfo(dwgfile):
print(message)
DataIssue.objects.create(parser='drawings', message=message, url=f'/dwgdataraw/{dwgfile.dwgpath}')
return
def load_drawings_files():
'''Breadth first search of drawings directory looking for sub-directories and *.xml filesize
@ -198,7 +197,8 @@ def load_drawings_files():
Why do we have all this detection of file types/! Why not use get_mime_types ?
What is it all for ??
ALL THIS NEEDS TO DETECT UPPER CASE suffices
We import JPG, PNG and SVG files; which have already been put on the server,
but the upload form intentionally refuses to upload PNG and JPG (though it does allow SVG)
'''
all_xml = []
drawdatadir = settings.DRAWINGS_DATA
@ -243,6 +243,11 @@ def load_drawings_files():
dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1])
dwgfile.save()
all_xml.append(('pdf',dwgfile))
elif Path(f).suffix.lower() == ".png":
# Always creates new
dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1])
dwgfile.save()
all_xml.append(('png',dwgfile))
elif Path(f).suffix.lower() == ".svg":
# Always creates new
dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1])
@ -262,7 +267,7 @@ def load_drawings_files():
print(f' - {len(all_xml)} Drawings files found')
for d in all_xml:
if d[0] in ['pdf', 'txt', '']:
if d[0] in ['pdf', 'txt', 'svg', 'jpg', 'png', '']:
setdrwfileinfo(d[1])
if d[0] == 'xml':
settnlfileinfo(d[1])

View File

@ -64,6 +64,8 @@
<p>Clicking on a filename only shows the file if the drawing file had already been imported into the system as part of a bulk-import
as we are matching it against a file recorded in the database. If you only just uploaded it, you will get an error message.
<p>You cannot create folders here, but you can put files into any of the pre-existing folders.
<p>Note that JPG, PNG and PDF files are refused: this area is reserved for Tunnel and Therion files. You may see some old ones here,
pending their removal and replacement, but you cannot upload new ones.
{% endif %}
</div>