enable photo file rename

This commit is contained in:
Philip Sargent 2022-08-11 21:19:52 +03:00
parent 25c425cff8
commit 3607b9f140
2 changed files with 37 additions and 6 deletions

View File

@ -59,7 +59,11 @@ todo = '''
class FilesForm(forms.Form): # not a model-form, just a form-form class FilesForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField() 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 class TextForm(forms.Form): # not a model-form, just a form-form
photographer = forms.CharField(strip=True) photographer = forms.CharField(strip=True)
@ -443,6 +447,13 @@ def photoupload(request, folder=None):
'''Upload photo image files into /expofiles/photos/<year>/<photographer>/ '''Upload photo image files into /expofiles/photos/<year>/<photographer>/
This does NOT use a Django model linked to a Django form. Just a simple Django form. 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. 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 year = settings.PHOTOS_YEAR
filesaved = False filesaved = False
@ -472,7 +483,7 @@ def photoupload(request, folder=None):
urldir = f'/photoupload/{year}' urldir = f'/photoupload/{year}'
form = FilesForm() form = FilesRenameForm()
formd = TextForm() formd = TextForm()
if request.method == 'POST': if request.method == 'POST':
@ -482,24 +493,37 @@ def photoupload(request, folder=None):
newphotographer = request.POST["photographer"] newphotographer = request.POST["photographer"]
(yearpath / newphotographer).mkdir(exist_ok=True) (yearpath / newphotographer).mkdir(exist_ok=True)
else: else:
form = FilesForm(request.POST,request.FILES) form = FilesRenameForm(request.POST,request.FILES)
if form.is_valid(): if form.is_valid():
f = request.FILES["uploadfiles"] f = request.FILES["uploadfiles"]
multiple = request.FILES.getlist('uploadfiles') multiple = request.FILES.getlist('uploadfiles')
# NO CHECK that the files being uploaded are image files # NO CHECK that the files being uploaded are image files
fs = FileSystemStorage(dirpath) fs = FileSystemStorage(dirpath)
renameto = request.POST["renameto"]
actual_saved = [] actual_saved = []
if multiple: if multiple:
for f in multiple: if len(multiple) == 1:
try: # crashes in Django os.chmod call if on WSL, but does save file! 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: except:
print(f'\n !! Permissions failure ?! on attempting to save file {f.name}') print(f'\n !! Permissions failure ?! on attempting to save file {f.name}')
if 'saved_filename' in locals(): if 'saved_filename' in locals():
if saved_filename.is_file(): if saved_filename.is_file():
actual_saved.append(saved_filename) actual_saved.append(saved_filename)
filesaved = True 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 = [] files = []
dirs = [] dirs = []
try: 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. 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. 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 We use get_or_create instead of simply creating a new object in case someone uploads the same file

View File

@ -19,6 +19,13 @@
<input class="fancybutton2" type = "file" multiple="multiple" <input class="fancybutton2" type = "file" multiple="multiple"
name = "uploadfiles" id="uploadfiles" /> name = "uploadfiles" id="uploadfiles" />
<br><br><br> <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_-\.]*"/>
<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" > <button class="fancybutton2" style="padding: 0.5em 25px; margin-left: 155px" type = "submit" value = "Upload" >
Upload Upload
</button> </button>