From 284e044a03caccab0d5dfa2cd1ac56949b80c20b Mon Sep 17 00:00:00 2001
From: Philip Sargent
Date: Sun, 14 Aug 2022 22:52:14 +0300
Subject: [PATCH] Fix wallets scan upload faults
---
core/views/uploads.py | 54 +++++++++++++++++++++++++++------------
parsers/drawings.py | 4 +--
parsers/scans.py | 34 ++++++++++++------------
parsers/survex.py | 6 +++++
templates/walletform.html | 20 ++++++++++++---
5 files changed, 80 insertions(+), 38 deletions(-)
diff --git a/core/views/uploads.py b/core/views/uploads.py
index 369e297..f76b38b 100644
--- a/core/views/uploads.py
+++ b/core/views/uploads.py
@@ -23,7 +23,7 @@ from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
#from troggle import settings
from troggle.parsers.imports import import_caves, import_people, import_surveyscans
from troggle.parsers.imports import import_logbooks, import_QMs, import_drawingsfiles, import_survex
-from troggle.parsers.scans import wallet_blank_json, wallet_blank_html, contentsjson, indexhtml, CopyWalletData
+from troggle.parsers.scans import wallet_blank_json, contentsjson, CopyWalletData
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
from troggle.core.models.troggle import DataIssue
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
@@ -63,6 +63,9 @@ class FilesForm(forms.Form): # not a model-form, just a form-form
class FilesRenameForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField()
renameto = forms.CharField(strip=True, required=False)
+
+class WalletGotoForm(forms.Form): # not a model-form, just a form-form
+ walletgoto = forms.CharField(strip=True, required=False)
class TextForm(forms.Form): # not a model-form, just a form-form
photographer = forms.CharField(strip=True)
@@ -200,6 +203,10 @@ def scanupload(request, path=None):
'''Upload scanned image files into a wallet on /expofiles
Also display AND EDIT the contents.json data in the wallet.
+ The Wallet object and the contents.json file are only created when the user
+ edits the JSON data, not when they upload files. This may be a bad idea
+ as the wallet will not appear in reports untilt his is done.
+
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.
@@ -269,7 +276,6 @@ def scanupload(request, path=None):
# Also lots of hassle with lists of strings interpreted as a single string
# Unset checkboxes do not return any value, checked ones return "True". So need initialising to False
if formj.is_valid():
- #print(f'--- JSON Update form is VALID, saving to {contents_path}')
posted = request.POST.copy()
posted.pop("csrfmiddlewaretoken") # discard this
wd = wallet_blank_json
@@ -295,11 +301,17 @@ def scanupload(request, path=None):
wd["survex file"][i] = elem.strip()
#print(f'--- {wd["survex file"]} - {type(wd["survex file"])}')
+ newfolder = contents_path.parent
+ print(f'--- wallet directory in :drawings: repo {newfolder=}')
+ if not os.path.exists(contents_path.parent):
+ print(f'--- No wallet directory in :drawings: repo, so creating it')
+ os.makedirs(contents_path.parent)
+
with open(contents_path, "w") as jfile:
json.dump(wd, jfile, indent = 1)
- # print(f'--- FINISHED saving to JSON\n')
+ print(f'--- FINISHED saving to JSON at {contents_path}')
- # This copies the new data to the drawings repo and commits it
+ # This commits it,
# needs the troggle object wallet, not a string
try:
@@ -311,7 +323,8 @@ def scanupload(request, path=None):
except:
print(f'wallet string {wallet}, FAIL TO GET or create WALLET OBJECT')
raise
-
+
+ # now we do the git add and commit
destfolder = contents_path.parent
dr_add = subprocess.run([git, "add", contentsjson], cwd=destfolder, capture_output=True, text=True)
if dr_add.returncode != 0:
@@ -333,6 +346,13 @@ def scanupload(request, path=None):
print(formj.errors)
return render(request,'errors/generic.html', {'message': formj.errors})
+ elif "walletgoto" in request.POST: # not editing wallet data, uploading a file.. going direct to a named wallet
+ formg = WalletGotoForm(request.POST,request.FILES)
+ if formg.is_valid():
+ walletgoto = request.POST["walletgoto"]
+
+ return HttpResponseRedirect(f'/scanupload/{walletgoto.replace("#",":")}')
+
else: # not editing wallet data, uploading a file..
form = FilesForm(request.POST,request.FILES)
@@ -354,15 +374,18 @@ def scanupload(request, path=None):
# print(f'! - FORM scanupload multiple {actual_saved}')
filesaved = True
- if not contents_path.is_file(): # double-check
- with open(contents_path, "w") as json_file:
- json.dump(wallet_blank_json, json_file, sort_keys=True, indent = 1)
- index_path = dirpath / indexhtml
- if not index_path.is_file(): # double-check
- thishtml = wallet_blank_html.replace("YEAR", str(year))
- thishtml = thishtml.replace("WALLET", str(wallet))
- with open(index_path, "w") as html_file:
- html_file.write(thishtml )
+ # Don't do this when uploading a file.
+ # The contents.json file will be created when the wallet data is edited for the first time.
+ # if not contents_path.is_file(): # double-check: it doesn't exist yet so create it when uploading a file.
+ # with open(contents_path, "w") as json_file:
+ # json.dump(wallet_blank_json, json_file, sort_keys=True, indent = 1)
+
+ # index_path = dirpath / indexhtml
+ # if not index_path.is_file(): # double-check it doesn't exist yet
+ # thishtml = wallet_blank_html.replace("YEAR", str(year))
+ # thishtml = thishtml.replace("WALLET", str,(wallet))
+ # with open(index_path, "w") as html_file:
+ # html_file.write(thishtml )
files = []
dirs = []
@@ -374,8 +397,7 @@ def scanupload(request, path=None):
if f.is_dir():
dirs.append(f.name)
if f.is_file():
- if f.name != 'contents.json' and f.name != 'walletindex.html':
- files.append(f.name)
+ files.append(f.name)
except FileNotFoundError:
files.append('(no wallet yet. It would be created if you upload a scan)')
else:
diff --git a/parsers/drawings.py b/parsers/drawings.py
index 1c5a1e8..4f52889 100644
--- a/parsers/drawings.py
+++ b/parsers/drawings.py
@@ -18,7 +18,7 @@ from troggle.core.utils import save_carefully, GetListDir
for tunnel and therion files
'''
-todo='''
+todo='''Rename functions more consistently between tunnel and therion variants
'''
def find_dwg_file(dwgfile, path):
@@ -104,7 +104,7 @@ def findwalletimage(therionfile, foundpath):
else:
message = f'! Scanned file {scanfilename} mentioned in "{therionfile.dwgpath}" is not actually found in {wallet.walletname}'
wurl = f'/survey_scans/{wallet.walletname}/'.replace("#",":")
- print(message)
+ # print(message)
DataIssue.objects.create(parser='Therion', message=message, url = wurl)
diff --git a/parsers/scans.py b/parsers/scans.py
index 6439549..400bd74 100644
--- a/parsers/scans.py
+++ b/parsers/scans.py
@@ -21,7 +21,7 @@ from troggle.core.views.scans import datewallet
'''
contentsjson = "contents.json"
-indexhtml = "walletindex.html"
+#indexhtml = "walletindex.html"
git = settings.GIT
# to do: create a 'low priority' field, so that any such wallet does not appear in summary reports
@@ -44,22 +44,22 @@ wallet_blank_json = {
"survex not required": False,
"website updated": False}
-wallet_blank_html = '''Wallet WALLET
-List of trips: expedition/YEAR
-- troggle-processed .svx files and logbook entries on server
-Date:
People: Unknown,
-Cave Guidebook description
-- A description is indicated as being needed, so may need adding into this cave page.
-
Survex file: not identified yet
-
Issues
-The description needs writing
-The QMs needs writing
The website is marked as needing updating (using the guidebook description)
-Tunnel / Therion drawing files need drawing
-Files
-
-
-'''
+# wallet_blank_html = '''Wallet WALLET
+# List of trips: expedition/YEAR
+# - troggle-processed .svx files and logbook entries on server
+# Date:
People: Unknown,
+# Cave Guidebook description
+# - A description is indicated as being needed, so may need adding into this cave page.
+#
Survex file: not identified yet
+#
Issues
+# The description needs writing
+# The QMs needs writing
The website is marked as needing updating (using the guidebook description)
+# Tunnel / Therion drawing files need drawing
+# Files
+#
+#
+# '''
def CheckEmptyDate(wallet):
'''If date is not set, get it from a linked survex file.
diff --git a/parsers/survex.py b/parsers/survex.py
index d5ed15f..07f5799 100644
--- a/parsers/survex.py
+++ b/parsers/survex.py
@@ -1528,6 +1528,12 @@ def LoadSurvexBlocks():
survexblockroot = SurvexBlock(name=ROOTBLOCK, survexpath="", cave=None, survexfile=survexfileroot,
legsall=0, legslength=0.0)
# crashes here sometimes on MariaDB complaining that cave_id should not be null. But it should be.
+ #django.db.utils.IntegrityError: (1048, "Column 'cave_id' cannot be null")
+ # fix by restarting db on server
+ # sudo service mariadb stop
+ # sudo service mariadb start
+
+
survexblockroot.save()
print(' - Loading Survex Blocks...')
diff --git a/templates/walletform.html b/templates/walletform.html
index bf859f0..c9d9f4b 100644
--- a/templates/walletform.html
+++ b/templates/walletform.html
@@ -10,6 +10,18 @@
Wallet {{wallet}}
{% endif %}
+
{{prevy}}
...
@@ -18,8 +30,9 @@
{{year}}:{{next}}
...
{{nexty}}
-
+
+