From 5798e8dcd507bc30c12bca7efa92185772bf0eee Mon Sep 17 00:00:00 2001
From: Philip Sargent
Date: Wed, 1 Feb 2023 21:31:07 +0000
Subject: [PATCH] make nav cope with missing wallets
---
core/views/wallets.py | 53 +++++++++++++++++++++++++++++++++------
templates/base.html | 2 +-
templates/wallet_new.html | 2 +-
templates/walletform.html | 37 ++++++++++++++++-----------
urls.py | 2 +-
5 files changed, 72 insertions(+), 24 deletions(-)
diff --git a/core/views/wallets.py b/core/views/wallets.py
index 0b9f426..12b2e01 100644
--- a/core/views/wallets.py
+++ b/core/views/wallets.py
@@ -309,8 +309,47 @@ def walletedit(request, path=None):
wallet = f"{year}:{wnumber:02d}"
return (None, wallet)
+ def identify_most_recent_wallet(wallet, y):
+ """ Need to find the last wallet of the previous year
+ Has to cope with years when there are no wallets
+ Has to cope with weirdly named imported wallets from 1999 & earlier
+ Has to cope if the 'current' wallet is one that happens to 'not exist' too.
+
+ Frankly this has just become too bizarre and we should devise a quite different
+ navigation system.
+ """
+ current_name = wallet.replace(":","#")
+
+ try:
+ allwallets = Wallet.objects.all().order_by('walletname')
+ previous_wallet = allwallets.first()
+ for w in allwallets:
+ if len(w.walletname) < 5:
+ continue
+ if w.walletname[4] != "#":
+ continue
+
+ if w.walletname == current_name:
+ break
+ if int(w.walletyear.year) >= int(y):
+ break
+ previous_wallet = w
+ name = previous_wallet.walletname
+
+ y = name[:4]
+ prevy = f"{int(y)-1}"
+ n = name[5:]
+ prev = f"{int(n):02d}"
+
+ except:
+ raise
+ prev = f"{int(n):02d}"
+
+ print(prev, prevy, y)
+ return prev, prevy, y
+
def create_nav_links(wallet):
- print(f"--- {wallet} now current")
+ """Find the previous wallet and next wallet and create navigation shortcuts"""
y = wallet[:4]
n = wallet[5:]
nexty = f"{int(y)+1}"
@@ -318,9 +357,9 @@ def walletedit(request, path=None):
next = f"{int(n)+1:02d}"
prev = f"{int(n)-1:02d}"
- if int(n) == 0:
- prev = f"{int(n):02d}"
-
+ if int(n) == 0:
+ prev, prevy, y = identify_most_recent_wallet(wallet, y)
+
return next, nexty, prev, prevy, y
def read_json():
@@ -339,7 +378,7 @@ def walletedit(request, path=None):
DataIssue.objects.create(parser="scans", message=message, url=wurl) # set URL to this wallet folder
raise
else: # no JSON file exists
- print("--- No JSON exists, so creating blank copy")
+ print("--- No JSON exists, so using default copy")
waldata = WALLET_BLANK_JSON.copy()
if not waldata["survex file"]:
try:
@@ -366,7 +405,7 @@ def walletedit(request, path=None):
"""We need a wallet Object so that the django template stuff can find the files"""
try:
w, created = Wallet.objects.get_or_create(walletname=walletname)
- print(f"--- Wallet string {walletname}, wallet object {w} created new?: {created}")
+ # print(f"--- Wallet string {walletname}, wallet object {w} created new?: {created}")
if created:
w.fpath = Path(settings.SCANS_ROOT, walletname[0:4], walletname)
w.save()
@@ -755,7 +794,7 @@ def walletedit(request, path=None):
caveifywallet(thiswallet)
thiswallet.ticks = thiswallet.get_ticks() # the complaints in colour form
# fixsurvextick(thiswallet, thiswallet.ticks)
- print(f"--- {wallet} {thiswallet} walletdate={thiswallet.walletdate} immediately before form render")
+ # print(f"--- {wallet} {thiswallet} walletdate={thiswallet.walletdate} immediately before form render")
except:
thiswallet = None
context = {
diff --git a/templates/base.html b/templates/base.html
index 377fa24..96c68fb 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -32,7 +32,7 @@
Survex |
All Survex |
Scans |
- Upload Scans |
+ Upload Scans |
Drawings |
Upload Drawings |
Upload Photos |
diff --git a/templates/wallet_new.html b/templates/wallet_new.html
index 4b41e56..619e025 100644
--- a/templates/wallet_new.html
+++ b/templates/wallet_new.html
@@ -6,7 +6,7 @@ Upload the survexfile using e.g.
(for a survex file for cave 1623-290). You will cut and paste the survex file data into the window on the form.
-While still logged-in, go to this page Create Wallet which will take you to the next unused wallet number page, and click the 'Create' button. This will not actually complete
+
While still logged-in, go to this page Create Wallet which will take you to the next unused wallet number page, and click the 'Create' button. This will not actually complete
the creation of the wallet until you have also set the date for the wallet in the wallet edit form (which will appear when you press 'Create').
While editing the wallet you should enter in the form the url of the survexfile which you have just created at e.g. /survexfile/caves-1623/290/mynewsurvex.svx (see above).
diff --git a/templates/walletform.html b/templates/walletform.html
index 871a895..fb0ecdd 100644
--- a/templates/walletform.html
+++ b/templates/walletform.html
@@ -5,7 +5,13 @@
{% block content %}
{% if user.username %}
-
Wallet {{wallet}} - edit settings or upload a file
+ {% if not create %}
+ Wallet {{wallet}} - edit settings or upload a file
+ {% else %}
+ Wallet {{wallet}} - Create a new Wallet
+ {% endif %}
+
+
{% else %}
Wallet {{wallet}}
{% endif %}
@@ -63,24 +69,27 @@
{% endif %}
-
-{% for f in files %}
-{{ f}}
- {% empty %}
-
<No files in this wallet. >
-
-{% endfor %}
-
-{% for d in dirs %}
- {{d}}
-{% endfor %}
-
+{% if not create %}
+
+ {% for f in files %}
+ {{ f}}
+ {% empty %}
+
<No files in this wallet. >
+
+ {% endfor %}
+
+ {% for d in dirs %}
+ {{d}}
+ {% endfor %}
+
+{% endif %}
+
{% if create %}
This online wallet does not yet exist.
{% if user.username %}
-
It will be created when you click the Create button, and then edit the date in the form below and save it.
+
It will be created when you click the Create button, and then edit the date in the form and save it.
{% else %}
To create a wallet, you need to Log In first and then return to this page.
{% endif %}
diff --git a/urls.py b/urls.py
index 72a7fc3..2374d92 100644
--- a/urls.py
+++ b/urls.py
@@ -87,7 +87,7 @@ trogglepatterns = [
re_path(r'^admin/', admin.site.urls), # includes admin login & logout urls
# Uploads - uploading a file
- path('walletedit/', walletedit, name='walletedit'), # path=2020#01
+ path('walletedit/', walletedit, name='walletedit'),
path('walletedit/', walletedit, name='walletedit'), # path=2020#01
path('photoupload/', photoupload, name='photoupload'), # restricted to current year
path('photoupload/', photoupload, name='photoupload'), # restricted to current year