hack wallet scan rename job

This commit is contained in:
2023-07-31 15:49:54 +03:00
parent 5f07f234ef
commit 89c1c65340
7 changed files with 133 additions and 22 deletions

View File

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