From 0295fce110ea6b10845bbfb99f761cfae271343b Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@gmail.com>
Date: Thu, 14 Sep 2023 13:40:19 +0300
Subject: [PATCH] logic rearranged

---
 core/views/uploads.py | 95 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 71 insertions(+), 24 deletions(-)

diff --git a/core/views/uploads.py b/core/views/uploads.py
index 999206a..655738f 100644
--- a/core/views/uploads.py
+++ b/core/views/uploads.py
@@ -406,6 +406,26 @@ def expofilerename(request, filepath):
     Currently this just does files within wallets i.e. in /surveyscans/
     and it returns control to the original wallet edit page
     """
+    def rotate_image():
+        print("ROTATE")
+        return simple_get()
+        
+    def simple_get():
+        form = ExpofileRenameForm()
+        return render(
+            request,
+            "renameform.html",
+            {
+                "form": form,
+                "filepath": filepath,
+                "filename": filename,
+                "filesize": filesize,
+                "files": files,
+                "walletpath": walletpath,
+                "notpics": notpics,
+            },
+        )
+        
     if filepath:
         actualpath = Path(settings.EXPOFILES) / Path(filepath)
     else:
@@ -419,6 +439,7 @@ def expofilerename(request, filepath):
         return render(request, "errors/generic.html", {"message": message})
     else:
         filename = Path(filepath).name
+        walletpath = Path(filepath).parent
         folder = actualpath.parent
         filesize = f"{actualpath.stat().st_size:,}"
 
@@ -426,16 +447,48 @@ def expofilerename(request, filepath):
         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":
+        
+    files = []
+    dirs = []
+    notpics =[]
+    dirpath = actualpath.parent
+    print(f'! - FORM rename expofile - start \n{filepath=} \n{dirpath=} \n{walletpath=}')
+    if dirpath.is_dir():
+        try:
+            for f in dirpath.iterdir():
+                if f.is_dir():
+                    for d in f.iterdir():
+                        dirs.append(f"{f.name}/{d.name}")
+                if f.is_file():
+                    if Path(f.name).suffix.lower() in [".jpg", ".jpeg", ".png"]:
+                        files.append(f.name)
+                    else:
+                         notpics.append(f.name)     
+        except FileNotFoundError:
+            files.append(
+                "(Error. There should be at least one filename visible here. Try refresh.)"
+            )
+    if request.method == "GET":
+        return simple_get()
+        
+    elif request.method == "POST":
         form = ExpofileRenameForm(request.POST)
         if not form.is_valid():
             message = f'Invalid form response for file renaming "{request.POST}"'
             print(message)
             return render(request, "errors/generic.html", {"message": message})
-        else:
-            renameto = sanitize_name(request.POST["renameto"])
+
+        if "rotate" in request.POST:
+            return rotate_image()
             
+        if "rename" in request.POST:
+            if "renametoname" not in request.POST:
+                print("renametoname not in request.POST")
+                # blank filename passed it, so just treat as another GET
+                return simple_get()  
+
+
+            renameto = sanitize_name(request.POST["renametoname"])  
             if (folder / renameto).is_file() or (folder / renameto).is_dir():
                 rename_bad = renameto
                 message = f'\n Cannot rename to an existing file or folder. "{filename}" -> "{(folder / renameto)}"'
@@ -448,29 +501,23 @@ def expofilerename(request, filepath):
                         "filepath": filepath,
                         "filename": filename,
                         "filesize": filesize,
+                        "files": files,
+                        "walletpath": walletpath,
+                        "notpics": notpics,
                         "rename_bad": rename_bad,
                     },
                 )
-            else:
-                actualpath.rename((folder / renameto))
-                message = f'\n RENAMED "{filename}" -> "{(folder / renameto)}"'
-                print(message)
-                walletid  = actualpath.relative_to(Path(settings.SCANS_ROOT)).parent.stem.replace("#",":")
-                print(walletid)
-                return redirect(f'/survey_scans/{walletid}/')
-                
-    else:
-        form = ExpofileRenameForm()
-        return render(
-            request,
-            "renameform.html",
-            {
-                "form": form,
-                "filepath": filepath,
-                "filename": filename,
-                "filesize": filesize,
-            },
-        )
+
+            actualpath.rename((folder / renameto))
+            message = f'\n RENAMED "{filename}" -> "{(folder / renameto)}"'
+            print(message)
+            walletid  = actualpath.relative_to(Path(settings.SCANS_ROOT)).parent.stem.replace("#",":")
+            print(walletid)
+            return redirect(f'/survey_scans/{walletid}/')
+            
+    else: # not GET or POST
+        print("UNRECOGNIZED action")
+        return simple_get()
  
 @login_required_if_public
 def photoupload(request, folder=None):