From 3607b9f1409c4e4d176f457f63739670c88b4951 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 11 Aug 2022 21:19:52 +0300 Subject: [PATCH] enable photo file rename --- core/views/uploads.py | 36 ++++++++++++++++++++++++++++------ templates/photouploadform.html | 7 +++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/core/views/uploads.py b/core/views/uploads.py index 9bb37cd..619ea90 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -59,7 +59,11 @@ todo = ''' class FilesForm(forms.Form): # not a model-form, just a form-form uploadfiles = forms.FileField() - + +class FilesRenameForm(forms.Form): # not a model-form, just a form-form + uploadfiles = forms.FileField() + renameto = forms.CharField(strip=True) + class TextForm(forms.Form): # not a model-form, just a form-form photographer = forms.CharField(strip=True) @@ -443,6 +447,13 @@ def photoupload(request, folder=None): '''Upload photo image files into /expofiles/photos/// 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. + + + When uploading from a phone, it is useful to be able to rename the file to something + meaningful as this is difficult to do on a phone. Previously we had assumed files would + 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. ''' year = settings.PHOTOS_YEAR filesaved = False @@ -472,7 +483,7 @@ def photoupload(request, folder=None): urldir = f'/photoupload/{year}' - form = FilesForm() + form = FilesRenameForm() formd = TextForm() if request.method == 'POST': @@ -482,24 +493,37 @@ def photoupload(request, folder=None): newphotographer = request.POST["photographer"] (yearpath / newphotographer).mkdir(exist_ok=True) else: - form = FilesForm(request.POST,request.FILES) + form = FilesRenameForm(request.POST,request.FILES) if form.is_valid(): f = request.FILES["uploadfiles"] multiple = request.FILES.getlist('uploadfiles') # NO CHECK that the files being uploaded are image files fs = FileSystemStorage(dirpath) + + renameto = request.POST["renameto"] actual_saved = [] if multiple: - for f in multiple: + if len(multiple) == 1: try: # crashes in Django os.chmod call if on WSL, but does save file! - saved_filename = fs.save(f.name, content=f) + saved_filename = fs.save(renameto, content=f) except: print(f'\n !! Permissions failure ?! on attempting to save file {f.name}') if 'saved_filename' in locals(): if saved_filename.is_file(): actual_saved.append(saved_filename) filesaved = True + + else: + 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 ?! on attempting to save file {f.name}') + if 'saved_filename' in locals(): + if saved_filename.is_file(): + actual_saved.append(saved_filename) + filesaved = True files = [] dirs = [] try: @@ -527,7 +551,7 @@ 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. - Need to validate it as being a valid GPX file 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 We use get_or_create instead of simply creating a new object in case someone uploads the same file diff --git a/templates/photouploadform.html b/templates/photouploadform.html index 0d6dddc..4c2b5a0 100644 --- a/templates/photouploadform.html +++ b/templates/photouploadform.html @@ -19,6 +19,13 @@


+ + +