diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-07-31 15:49:54 +0300 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-07-31 15:49:54 +0300 |
commit | 89c1c653402f0c1199476755d106437bdee19ca4 (patch) | |
tree | 1ecc143fc4e18620c4d211e12ae5a79397c258af /core/views/uploads.py | |
parent | 5f07f234ef8d9c5cf8c4a13d1d0afb1142dbbff9 (diff) | |
download | troggle-89c1c653402f0c1199476755d106437bdee19ca4.tar.gz troggle-89c1c653402f0c1199476755d106437bdee19ca4.tar.bz2 troggle-89c1c653402f0c1199476755d106437bdee19ca4.zip |
hack wallet scan rename job
Diffstat (limited to 'core/views/uploads.py')
-rw-r--r-- | core/views/uploads.py | 70 |
1 files changed, 67 insertions, 3 deletions
diff --git a/core/views/uploads.py b/core/views/uploads.py index a22da16..31250cb 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -3,7 +3,7 @@ from pathlib import Path from django import forms from django.core.files.storage import FileSystemStorage -from django.shortcuts import render +from django.shortcuts import render, redirect import settings from troggle.core.models.survex import DrawingFile @@ -13,6 +13,8 @@ from troggle.core.models.survex import DrawingFile from .auth import login_required_if_public """File upload 'views' +Note that there are other file upload forms in views/wallet_edit.py +and that core/forms.py contains Django class-based forms for caves and entrances. """ todo = """ @@ -25,12 +27,14 @@ todo = """ Need to validate it as being a valid GPX file using an XML parser, not a dubious script or hack - Validate Tunnel & Therion files using an XML parser in dwgupload(). Though Julian says - tunnel is only mostly correct XML + tunnel is only mostly correct XML, and it does fail at least one XML parser. - parse the uploaded drawing file for links to wallets and scan files as done in parsers/drawings.py - Enable folder creation in dwguploads or as a separate form + +- Enable file rename on expofiles, particularly in /surveyscans/ (aka wallets) """ class FilesForm(forms.Form): # not a model-form, just a form-form @@ -42,7 +46,64 @@ 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 ExpofileRenameForm(forms.Form): # not a model-form, just a form-form + renameto = forms.CharField(strip=True, required=False) + +@login_required_if_public +def expofilerename(request, filepath): + """Rename any single file in /expofiles/ - eventually. + Currently this just does files within wallets i.e. in /surveyscans/ + and it returns control to the original wallet edit page + """ + if filepath: + actualpath = Path(settings.EXPOFILES) / Path(filepath) + else: + message = f'\n File to rename not specified "{filepath}"' + print(message) + return render(request, "errors/generic.html", {"message": message}) + + if not actualpath.is_file(): + message = f'\n File not found when attempting rename "{filepath}"' + print(message) + return render(request, "errors/generic.html", {"message": message}) + else: + filename = Path(filepath).name + folder = Path(actualpath).parent + + if not actualpath.is_relative_to(Path(settings.SCANS_ROOT)): + message = f'\n Can only do rename within wallets (expofiles/surveyscans/) currently, sorry. "{actualpath}" ' + print(message) + return render(request, "errors/generic.html", {"message": message}) + + + if request.method == "POST": + form = ExpofileRenameForm(request.POST) + if form.is_valid(): + renameto = request.POST["renameto"] + + if (folder / renameto).is_file(): + message = f'\n Cannot rename to an existing file. "{filename}" -> "{(folder / renameto)}"' + print(message) + return render(request, "errors/generic.html", {"message": message}) + else: + actualpath.rename((folder / renameto)) + message = f'\n RENAMED "{filename}" -> "{(folder / renameto)}"' + print(message) + return redirect('/survey_scans/2023%252314/') + else: + form = ExpofileRenameForm() + return render( + request, + "renameform.html", + { + "form": form, + "filepath": filepath, + "filename": filename, + }, + ) + @login_required_if_public def photoupload(request, folder=None): """Upload photo image files into /expofiles/photos/<year>/<photographer>/ @@ -55,6 +116,8 @@ def photoupload(request, folder=None): be renamed to something useful before starting the upload. Unfortunately this only works when uploading one file at at time , inevitable once you think about it. + + Pending generic file renaming capability more generally. """ year = settings.PHOTOS_YEAR filesaved = False @@ -186,7 +249,8 @@ def dwgupload(request, folder=None, gitdisable="no"): This does NOT use a Django model linked to a Django form. Just a simple Django form. You will find the Django documentation on forms very confusing, This is simpler. - We could validate the uploaded files as being a valid files using an XML parser, not a dubious script or hack + We could validate the uploaded files as being a valid files using an XML parser, not a dubious script or hack, + but this won't work on Tunnel files as Tunnel does not produce exactly valid xml We use get_or_create instead of simply creating a new object in case someone uploads the same file several times in one session, and expects them to be overwritten in the database. Although |