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