From 90bb0759a082755c55083f83bf029a44d225e013 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Tue, 4 May 2021 02:46:56 +0100 Subject: [PATCH] Drawing files upload form --- core/views/drawings.py | 2 +- core/views/other.py | 76 ++++++++++++++++++++++++++++++++---- parsers/drawings.py | 26 ++++++++++++ templates/dirdisplay.html | 4 +- templates/dwguploadform.html | 63 ++++++++++++++++++++++++++++++ templates/pagenotfound.html | 2 +- urls.py | 10 +++-- 7 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 templates/dwguploadform.html diff --git a/core/views/drawings.py b/core/views/drawings.py index 7657d3d..19507b9 100644 --- a/core/views/drawings.py +++ b/core/views/drawings.py @@ -51,7 +51,7 @@ def dwgfilesingle(request, path): def dwgfileupload(request, path): - '''uploads a drawing file, but where is the Form? This just processes POST info. Apparently unfinished? + '''Use bits of this to REGISTEr a recently uploaded dwg file which used dwgupload ''' try: dwgfile = DrawingFile.objects.get(dwgpath=urlunquote(path)) # need to check if inavlid query string and produce friendly error diff --git a/core/views/other.py b/core/views/other.py index 56ad5a4..582d3fa 100644 --- a/core/views/other.py +++ b/core/views/other.py @@ -193,20 +193,13 @@ def exportlogbook(request,year=None,extension=None): return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':[completed]}) - -# def ajax_test(request): - # post_text = request.POST['post_data'] - # return HttpResponse("{'response_text': '"+post_text+" recieved.'}", - # content_type="application/json") - - class FilesForm(forms.Form): # not a model-form, just a form-form scanfiles = forms.FileField() @login_required_if_public def scanupload(request, wallet=None): - '''Upload one scanned image file into a wallet on /expofiles + '''Upload scanned image files into a wallet on /expofiles This does NOT use a Django model linked to a Django form. Just a simple Django form. ''' filesaved = False @@ -275,3 +268,70 @@ def scanupload(request, wallet=None): return render(request, 'scanuploadform.html', {'form': form, 'wallet': wallet, **context, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved}) + +@login_required_if_public +def dwgupload(request, folder=None): + '''Upload DRAWING files (tunnel or therion) into the upload folder in :drawings: + This does NOT use a Django model linked to a Django form. Just a simple Django form. + ''' + def dwgvalid(name): + if name in [ '.gitignore', '.hgignore', ]: + return False + if Path(name).suffix in ['.xml', '.th', '.th2', '', '.svg', '.jpg']: + return True + return False + + filesaved = False + actual_saved = [] + doesnotexist = '' + #print(f'! - FORM dwgupload - start "{folder}"') + if folder is None: + folder = "" # improve this later + dirpath = Path(settings.DRAWINGS_DATA) + urlfile = '/dwgdataraw' + urldir = '/dwgupload' + else: + dirpath = Path(settings.DRAWINGS_DATA, folder) + urlfile = Path('/dwgdataraw/') / folder + urldir = Path('/dwgupload/') / folder + + form = FilesForm() + + if request.method == 'POST': + form = FilesForm(request.POST,request.FILES) + if form.is_valid(): + f = request.FILES["scanfiles"] + multiple = request.FILES.getlist('scanfiles') + fs = FileSystemStorage(os.path.join(settings.DRAWINGS_DATA, folder)) + + actual_saved = [] + if multiple: + for f in multiple: + if dwgvalid(f.name): + actual_saved.append( fs.save(f.name, content=f) ) + # print(f'! - FORM dwgupload multiple {actual_saved}') + filesaved = True + + files = [] + dirs = [] + #print(f'! - FORM dwgupload - start {folder} \n"{dirpath}" \n"{dirpath.parent}" \n"{dirpath.exists()}"') + try: + for f in dirpath.iterdir(): + if f.is_dir(): + if f.name not in ['.git' ]: + dirs.append(f.name) + continue + if f.is_file(): + if dwgvalid(f.name): + files.append(f.name) + continue + except FileNotFoundError: + doesnotexist = True + if files: + files = sorted(files) + + if dirs: + dirs = sorted(dirs) + + return render(request, 'dwguploadform.html', + {'form': form, 'doesnotexist': doesnotexist, 'urlfile': urlfile, 'urldir': urldir,'folder': folder, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved}) diff --git a/parsers/drawings.py b/parsers/drawings.py index 5b7bc1e..a313a83 100644 --- a/parsers/drawings.py +++ b/parsers/drawings.py @@ -5,6 +5,7 @@ import stat import csv import re import datetime +from pathlib import Path from PIL import Image from functools import reduce @@ -261,6 +262,11 @@ def setdwgfileinfo(dwgfile): def load_drawings_files(): '''Breadth first search of drawings directory looking for sub-directories and *.xml filesize + + 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 DETCT UPPER CASE suffices ''' all_xml = [] drawdatadir = settings.DRAWINGS_DATA @@ -294,6 +300,26 @@ def load_drawings_files(): dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1]) dwgfile.save() all_xml.append(('th2',dwgfile)) + elif f[-4:] == ".pdf": + # Always creates new + dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1]) + dwgfile.save() + all_xml.append(('pdf',dwgfile)) + elif f[-4:] == ".svg": + # Always creates new + dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1]) + dwgfile.save() + all_xml.append(('svg',dwgfile)) + elif f[-4:] == ".jpg": + # Always creates new + dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f[:-4])[1]) + dwgfile.save() + all_xml.append(('jpg',dwgfile)) + elif Path(f).suffix == '': + # therion file + dwgfile = DrawingFile(dwgpath=lf, dwgname=os.path.split(f)[1]) + dwgfile.save() + all_xml.append(('',dwgfile)) print(f' - {len(all_xml)} Drawings files found') diff --git a/templates/dirdisplay.html b/templates/dirdisplay.html index 39460a7..b39796d 100644 --- a/templates/dirdisplay.html +++ b/templates/dirdisplay.html @@ -23,6 +23,6 @@ {% endblock content %} {% block margins %} - - + + {% endblock margins %} \ No newline at end of file diff --git a/templates/dwguploadform.html b/templates/dwguploadform.html new file mode 100644 index 0000000..c99647c --- /dev/null +++ b/templates/dwguploadform.html @@ -0,0 +1,63 @@ +{% extends "base.html" %} + +{% block title %}Simple Fileupload{% endblock %} + +{% block content %} + +

Upload drawing file into folder '{{folder}}'

+

+

+
+ {% csrf_token %} +
+ + +


+ +
+
+
+

Only drawings and drawing config files can be uploaded. +

+
+ {% if filesaved %} +

+ Drawing(s) saved as
+ {% for f in actual_saved %} + {{f}}
+ {% endfor %} +

Upload more?
+

+
+ {% endif %} + +{% if doesnotexist %} +

No folder of this name.
+It would be created if you upload a file. +{% else %} + Files:
+ {% for f in files %} + {{f}}
+ {% empty %} +

<No files here> + {% endfor %} + + +

Directories:
+ {% if folder %} + [up]
+ {% endif %} + {% for f in dirs %} + /{{f}}/
+ {% empty %} +

<No subdirectories> + {% endfor %} +

Clicking on a filename only works if the drawing file has been imported into the system as part of a bulk-import + as we are matching it against a file recorded in the database. +{% endif %} +

+ +{% endblock %} diff --git a/templates/pagenotfound.html b/templates/pagenotfound.html index fe87445..11000f7 100644 --- a/templates/pagenotfound.html +++ b/templates/pagenotfound.html @@ -43,7 +43,7 @@ div#editLinks a{
  • You can log on or log off using the gray menu bar above. -

    Did you get lost ? +

    Did you get lost ?

    {% include "menu.html" %} diff --git a/urls.py b/urls.py index 06e7b61..1d5c9c9 100644 --- a/urls.py +++ b/urls.py @@ -10,7 +10,9 @@ from django.urls import reverse, resolve from troggle.core.views import caves, statistics, survex from troggle.core.views.scans import scansingle, singlewallet, allwallets -from troggle.core.views.drawings import dwgdata, dwgfilesingle, dwgfileupload +from troggle.core.views.drawings import dwgdata, dwgfilesingle +from troggle.core.views.drawings import dwgfileupload +from troggle.core.views.other import dwgupload from troggle.core.views.other import troggle404, frontpage, todos, controlpanel, frontpage, scanupload from troggle.core.views.other import exportlogbook from troggle.core.views.caves import ent, cavepage @@ -79,7 +81,9 @@ trogglepatterns = [ re_path(r'^admin/doc/', include('django.contrib.admindocs.urls')), # needs docutils Python module (http://docutils.sf.net/). re_path(r'^admin/', admin.site.urls), # includes admin login & logout urls - path('scanupload/', scanupload, name='scanupload'), + path('scanupload/', scanupload, name='scanupload'), + path('dwgupload/', dwgupload, name='dwgupload'), + path('dwgupload/', dwgupload, name='dwgupload'), # setting LOGIN_URL = '/accounts/login/' is default # url ENDS WITH this string @@ -158,7 +162,7 @@ trogglepatterns = [ re_path(r'^dwgdataraw/(?P.+?\.th)$', dwgfilesingle, name="dwgfilesingle"), re_path(r'^dwgdataraw/(?P.+?\.th2)$', dwgfilesingle, name="dwgfilesingle"), # re_path(r'^dwgdatainfo/(?P.+?\.xml)$', dwgfileinfo, name="dwgfileinfo"), # parses tunnel for info & ref to wallet - re_path(r'^dwgdataraw/(?P.+?\.xml)/upload$', dwgfileupload, name="dwgfileupload"), # Not working +# re_path(r'^dwgdataraw/(?P.+?\.xml)/upload$', dwgfileupload, name="dwgfileupload"), # Not working # QMs pages - must precede other /caves pages?