diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2024-07-13 14:49:55 +0200 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2024-07-13 14:49:55 +0200 |
commit | cef872d038ee627500f089561345927c241e89f0 (patch) | |
tree | 6e63751ff53061f7bd9cd5d9c259f19158e43a0a | |
parent | fca95ce5399c03e2cb25d3832650c456863ebb59 (diff) | |
download | troggle-cef872d038ee627500f089561345927c241e89f0.tar.gz troggle-cef872d038ee627500f089561345927c241e89f0.tar.bz2 troggle-cef872d038ee627500f089561345927c241e89f0.zip |
GPX upload
-rw-r--r-- | core/views/uploads.py | 161 | ||||
-rw-r--r-- | templates/base.html | 5 | ||||
-rw-r--r-- | templates/gpxuploadform.html | 101 | ||||
-rw-r--r-- | urls.py | 4 |
4 files changed, 268 insertions, 3 deletions
diff --git a/core/views/uploads.py b/core/views/uploads.py index 403a30d..9d0ed35 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -142,6 +142,9 @@ class FilesRenameForm(forms.Form): # not a model-form, just a form-form class TextForm(forms.Form): # not a model-form, just a form-form photographer = forms.CharField(strip=True) + +class TextProspectorForm(forms.Form): # not a model-form, just a form-form + prospector = forms.CharField(strip=True) class ExpofileRenameForm(forms.Form): # not a model-form, just a form-form renameto = forms.CharField(strip=True, required=False) @@ -726,6 +729,7 @@ def photoupload(request, folder=None): if request.method == "POST": if "photographer" in request.POST: + # then we are creating a new folder formd = TextForm(request.POST) if formd.is_valid(): newphotographer = sanitize_name(request.POST["photographer"]) @@ -737,6 +741,7 @@ def photoupload(request, folder=None): return render(request, "errors/generic.html", {"message": message}) else: + # then we are renaming the file ? form = FilesRenameForm(request.POST, request.FILES) if form.is_valid(): f = request.FILES["uploadfiles"] @@ -815,6 +820,162 @@ def photoupload(request, folder=None): }, ) +@login_required_if_public +def gpxupload(request, folder=None): + """Copy of photo upload + folder is the "path" + """ + def gpxvalid(name): + if Path(name).suffix.lower() in [".xml", ".gpx"]: + return True # dangerous, we should check the actual file binary signature + return False + + print(f"gpxupload() {folder=}") + year = current_expo() + filesaved = False + actual_saved = [] + + context = {"year": year, "placeholder": "AnathemaDevice"} + + yearpath = Path(settings.EXPOFILES) / "gpslogs" / year + + if folder == str(year) or folder == str(year) + "/": + folder = None + + if folder is None: + folder = "" # improve this later + dirpath = yearpath + urlfile = f"/expofiles/gpslogs/{year}" + urldir = f"/gpxupload/{year}" + else: # it will contain the year as well as the prospector + dirpath = Path(settings.EXPOFILES) / "gpslogs" / folder + if dirpath.is_dir(): + urlfile = f"/expofiles/gpslogs/{folder}" + urldir = Path("/gpxupload") / folder + else: + folder = "" # improve this later + dirpath = yearpath + urlfile = f"/expofiles/gpslogs/{year}" + urldir = f"/gpxupload/{year}" + + print(f"gpxupload() {folder=} {dirpath=} {urlfile=} {urldir=}") + form = FilesRenameForm() + formd = TextProspectorForm() + print(f"gpxupload() {form=} {formd=} ") + + + if request.method == "POST": + print(f"gpxupload() method=POST") + for i in request.POST: + print(" ",i) + + if "prospector" in request.POST: + print(f"gpxupload() {request.POST=}\n {request.POST["prospector"]=}") + formd = TextProspectorForm(request.POST) + if formd.is_valid(): + newprospector = sanitize_name(request.POST["prospector"]) + print(f"gpxupload() {newprospector=}") + try: + (yearpath / newprospector).mkdir(exist_ok=True) + except: + message = f'\n !! Permissions failure ?! 0 attempting to mkdir "{(yearpath / newprospector)}"' + print(message) + return render(request, "errors/generic.html", {"message": message}) + + else: + print(f"gpxupload() no prospector field") + print(f"gpxupload() {request.FILES=}") + for i in request.FILES: + print(" ",i) + + form = FilesRenameForm(request.POST, request.FILES) + print(f"gpxupload() is the FilesRenameForm valid? {form=}") + for i in form: + print(" ",i) + + if not form.is_valid(): + print(f"gpxupload() Form is not valid {form=}") + else: + print(f"gpxupload() about to look at request.FILES") + f = request.FILES["uploadfiles"] + multiple = request.FILES.getlist("uploadfiles") + # NO CHECK that the files being uploaded are image files + fs = FileSystemStorage(dirpath) + + renameto = sanitize_name(request.POST["renameto"]) + + actual_saved = [] + if multiple: + if len(multiple) == 1: + if renameto != "": + try: # crashes in Django os.chmod call if on WSL, but does save file! + saved_filename = fs.save(renameto, content=f) + except: + print( + f'\n !! Permissions failure ?! 1 attempting to save "{f.name}" in "{dirpath}" {renameto=}' + ) + if "saved_filename" in locals(): + if saved_filename.is_file(): + actual_saved.append(saved_filename) + filesaved = True + else: # multiple is the uploaded content + try: # crashes in Django os.chmod call if on WSL, but does save file! + saved_filename = fs.save(f.name, content=f) + except: + print( + f'\n !! Permissions failure ?! 2 attempting to save "{f.name}" in "{dirpath}" {renameto=}' + ) + if "saved_filename" in locals(): + if saved_filename.is_file(): + actual_saved.append(saved_filename) + filesaved = True + else: # multiple is a list of content + for f in multiple: + try: # crashes in Django os.chmod call if on WSL, but does save file! + saved_filename = fs.save(f.name, content=f) + except: + print( + f'\n !! Permissions failure ?! 3 attempting to save "{f.name}" in "{dirpath}" {renameto=}' + ) + if "saved_filename" in locals(): + if saved_filename.is_file(): + actual_saved.append(saved_filename) + filesaved = True + print(f"gpxupload() drop through") + files = [] + dirs = [] + try: + for f in dirpath.iterdir(): + if f.is_dir(): + dirs.append(f.name) + if f.is_file(): + files.append(f.name) + except FileNotFoundError: + files.append("(no folder yet - would be created)") + except Exception as e: + print(f"gpxupload() EXCEPTION\n {e}") + if len(files) > 0: + files = sorted(files) + + if dirs: + dirs = sorted(dirs) + + print(f"gpxupload() about to render..") + return render( + request, + "gpxuploadform.html", + { + "form": form, + **context, + "urlfile": urlfile, + "urldir": urldir, + "folder": folder, + "files": files, + "dirs": dirs, + "filesaved": filesaved, + "actual_saved": actual_saved, + }, + ) @login_required_if_public def dwgupload(request, folder=None, gitdisable="no"): diff --git a/templates/base.html b/templates/base.html index 79312da..7e7cdf8 100644 --- a/templates/base.html +++ b/templates/base.html @@ -37,8 +37,8 @@ <div class="toolbarlinks"> {% endif %} <a href="/logbookedit/">Logbook Entry</a> | - <a href="/1626/359/359.html">359 (HC)</a> | - <a id="cavesLink" href="/caves">caves</a> | + <!--<a href="/1626/359/359.html">359 (HC)</a> |--> + <a id="cavesLink" href="/caves">Caves</a> | <a id="qmsLink" href="{% url "caveQMs" "1623-290" %}">QMs</a> | <a href="/survexfile/">Survex</a> | <a href="{% url "survexcaveslist" %}">All Survex</a> | @@ -47,6 +47,7 @@ <a href="{% url "dwgallfiles" %}">Drawings</a> | <a href="{% url "dwgupload" %}">Upload Drawings</a> | <a href="{% url "photoupload" %}">Upload Photos</a> | + <a href="{% url "gpxupload" %}">Upload GPX</a> | <br> diff --git a/templates/gpxuploadform.html b/templates/gpxuploadform.html new file mode 100644 index 0000000..513f2f0 --- /dev/null +++ b/templates/gpxuploadform.html @@ -0,0 +1,101 @@ +{% extends "base.html" %} + +{% block title %}Simple Fileupload (GPX files){% endblock %} + +{% block content %} + + +{% if folder %} +<h2>Upload GPX files into /gpslogs/{{folder}}/</h2> +{% else %} +<h2>Upload GPX files into /gpslogs/{{year}}/</h2> +{% endif %} + + +<div style = "max-width:35%; margin-left:20%; text-align: center; " > + <form method ='post' enctype ="multipart/form-data"> + {% csrf_token %} + <br> + <input class="fancybutton2" type = "file" multiple="multiple" + name = "uploadfiles" id="uploadfiles" /> + <br><br><br> + <input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px" + label = "Rename to" name = "renameto" id="renameto" + pattern="[A-Za-z][A-Za-z0-9_-\.]*"/> + <br /> + <label + style="padding: 0.5em 25px; margin-left: 110px" + for="renameto">If uploading a single file, you can rename it<br></label> + <br><br><br> + <button class="fancybutton2" style="padding: 0.5em 25px; margin-left: 155px" type = "submit" value = "Upload" > + Upload + </button> + </form> +</div> +<div style = "max-width:29%; margin-left:20%; text-align: left" > + {% if filesaved %} + <p> + <b>File(s) saved as <br> + {% for f in actual_saved %} + <em>{{f}}</em> <br> + {% endfor %} + </p> + {% endif %} + + <strong style="font-size: 110%;">Files:</strong><br> + {% for f in files %} + <a href="{{urlfile|urlencode}}/{{f|urlencode}}">{{f}}</a><br /> + {% empty %} + <p><No files here> + {% endfor %} + + + <p><strong style="font-size: 110%;">Prospector Directories:</strong><br> + {% if folder %} + <a href="{{urldir}}/..">[up]</a><br /> + {% endif %} + {% for f in dirs %} + <a href="{{urldir}}/{{f}}">/{{f}}/</a><br /> + {% empty %} + <p><No subdirectories> + {% endfor %} +</div> +{% if folder %} +<p>You can upload your GPX tracks here. +<p>You cannot create sub-folders here. +<p>Note that only GPX files are accepted: all other types of files are refused. +{% else %} +<p>You can upload your GPX tracks to any of these folders, and you can create a new folder in your name for your gpslogs. +<p>Note that only GPX files are accepted: all other types of files are refused. +<hr> +<h2>Create new prospector folder in /gpslogs/{{year}}/</h2> + + + +<div style = "max-width:35%; margin-left:20%; text-align: center; " > + <form method ='post'> + {% csrf_token %} + <br> + <input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px" + label = "prospector" name = "prospector" id="prospector" + pattern="[A-Za-z]+" + placeholder="{{placeholder}}" required /> + <label + style="padding: 0.5em 25px; margin-left: 110px" + for="prospector">prospector's name<br></label> + <label + style="padding: 0.5em 25px; margin-left: 110px" + for="prospector">(no spaces, only letters)</label> + <br><br><br> + <button class="fancybutton2" style="padding: 0.5em 25px; margin-left: 155px" type = "submit" value = "Create" > + Create folder + </button> + </form> +</div> +<p> + +{% endif %} +<hr /> + + +{% endblock %}
\ No newline at end of file @@ -24,7 +24,7 @@ from troggle.core.views.other import (controlpanel, exportlogbook, frontpage, from troggle.core.views.prospect import prospecting from troggle.core.views.scans import (allscans, cavewallets, scansingle, walletslistperson, walletslistyear) -from troggle.core.views.uploads import dwgupload, photoupload, expofilerename, logbookedit +from troggle.core.views.uploads import dwgupload, photoupload, gpxupload, expofilerename, logbookedit from troggle.core.views.wallets_edit import walletedit """This sets the actualurlpatterns[] and urlpatterns[] lists which django uses to resolve urls - in both directions as these are declarative. @@ -115,6 +115,8 @@ trogglepatterns = [ path('walletedit/<path:path>', walletedit, name='walletedit'), # path=2020#01 path('photoupload/', photoupload, name='photoupload'), # restricted to current year path('photoupload/<path:folder>', photoupload, name='photoupload'), # restricted to current year + path('gpxupload/', gpxupload, name='gpxupload'), # restricted to current year + path('gpxupload/<path:folder>', gpxupload, name='gpxupload'), # restricted to current year path('dwgupload/<path:folder>', dwgupload, name='dwgupload'), path('dwgupload/', dwgupload, name='dwgupload'), path('dwguploadnogit/', dwgupload, {'gitdisable': 'yes'}, name='dwguploadnogit'), # used in testing |