From 3c7661836c5c34edf1548e4470c28765d3eec71c Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Tue, 31 Jan 2023 20:28:39 +0000 Subject: [PATCH] New wallet goes to next available slot now --- core/views/uploads.py | 2 +- core/views/wallets.py | 119 ++++++++++++++++++++------------------ templates/walletform.html | 2 +- 3 files changed, 65 insertions(+), 58 deletions(-) diff --git a/core/views/uploads.py b/core/views/uploads.py index 3dc14d6..21a6844 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -22,7 +22,7 @@ from .auth import login_required_if_public todo = """ - Register uploaded filenames in the Django db without needing to wait for a reset & bulk file import -- Need to validate uploaded file as being a valid image file, not a dubious script or hack +- Need to validate uploaded file as being a valid file type, not a dubious script or hack - Write equivalent GPX upload form system, similar to walletedit() but in expofiles/gpslogs/ Need to validate it as being a valid GPX file using an XML parser, not a dubious script or hack diff --git a/core/views/wallets.py b/core/views/wallets.py index 7507ed9..5484e0a 100644 --- a/core/views/wallets.py +++ b/core/views/wallets.py @@ -27,9 +27,7 @@ from troggle.core.views.uploads import FilesForm from troggle.parsers.scans import contentsjson -# from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt - -"""File upload 'views' +"""Main wallet editing form, which includes scan file upload into the wallet """ todo = """ @@ -38,17 +36,6 @@ todo = """ - Refactor walletedit() as it contains all the wallets 'complaints' code from the old script wallets.py - Need to validate uploaded file as being a valid image file, not a dubious script or hack - -- Write equivalent GPX upload form system, similar to walletedit() but in expofiles/gpslogs/ - Need to validate it as being a valid GPX file using an XML parser, not a dubious script or hack - -- Validate Tunnel & Therion files using an XML parser in dwgupload(). Though Julian says tunnel is only mostly correct XML - -- Validate image files using a magic recogniser in walletedit() https://pypi.org/project/reportlab/ or - https://stackoverflow.com/questions/889333/how-to-check-if-a-file-is-a-valid-image-file - -- Enable folder creation in dwguploads or as a separate form - """ WALLET_BLANK_JSON = { "cave": "", @@ -277,14 +264,61 @@ def walletedit(request, path=None): You will find the Django documentation on forms very confusing, as it covers many very differnet things we do not need. This is simpler. - This subsumes much of the code which was in the old wallets.py script and so this function is very long - indeed and needs refactoring. + This subsumes much of the code which was in the old wallets.py script and so this function is very long indeed and needs refactoring. REWRITE bits using the ticklist, dateify, caveify, populate etc utility functions in core.view.scans.py """ git = settings.GIT filesaved = False actual_saved = [] + + def get_next_empty(): + latest = Wallet.objects.filter(walletname__startswith="20").latest('walletname') + print(f"Latest {latest=}") + next = int(latest.walletname[5:]) + 1 + return f"{latest.walletname[:4]}:{next:02d}" + + def preprocess_path(path): + if path: + wpath = urllib.parse.unquote(path) + else: + return (None, get_next_empty() ) + + try: + year = wpath[:4] # if path too short, exception catches it + sepr = wpath[4] + y = int(year) # exception catches non-integer [:4] + wnumber = int(wpath[5:]) # exception catches nonumeric wallet number + if sepr != "#" and sepr != ":": + return (oldwallet(request, path), None) + except: + # if nonumeric wpath name for example + raise + return (oldwallet(request, path), None) + + if not re.match("(19|20)\d\d[:#]\d\d\d?", wpath): + return (None, get_next_empty() ) + + if int(year) < 1977: + year = "1977" + if int(year) > 2050: + year = "2050" + wallet = f"{year}:{wnumber:02d}" + return (None, wallet) + + def create_nav_links(wallet): + print(f" --- {wallet}") + y = wallet[:4] + n = wallet[5:] + nexty = f"{int(y)+1}" + prevy = f"{int(y)-1}" + next = f"{int(n)+1:02d}" + prev = f"{int(n)-1:02d}" + + if int(n) == 0: + prev = f"{int(n):02d}" + + return next, nexty, prev, prevy, y def read_json(): """Read JSON from the wallet metadata file in the repo @@ -405,45 +439,12 @@ def walletedit(request, path=None): "elev drawn", "electronic survey", ] - if path: - wallet = urllib.parse.unquote(path) - else: - wallet = "2022#00" # improve this later - - year = wallet[:4] - try: - if wallet[4] != "#" and wallet[4] != ":": - # print(f'! - FORM walletedit - {wallet[4]} unurlencoded {unquote(wallet)[4]}') - # print(f'! - FORM walletedit - start {wallet} REDIRECT TO OLDWALLET') - return oldwallet(request, path) - except: - # if nonumeric wallet name for example - return oldwallet(request, path) - - if str(wallet).lower().endswith("indexpages"): - # print(f'! - FORM walletedit - start {wallet} REDIRECT TO OLDWALLET') - return walletindex(request, path) - - if not re.match("(19|20)\d\d[:#]\d\d", wallet): - wallet = "2022:00" # improve this later - # print(f'! - FORM walletedit - start {wallet}') - - if path: - pass - # print(f'! - FORM walletedit - start wallet:{wallet}: path:{path}:') - if int(year) < 1977: - year = "1977" - if int(year) > 2050: - year = "2050" - nexty = f"{int(year)+1}" - prevy = f"{int(year)-1}" - - wnumber = wallet[5:] - next = f"{int(wnumber)+1:02d}" - prev = f"{int(wnumber)-1:02d}" - - if int(wnumber) == 0: - prev = f"{int(wnumber):02d}" + + redirect, wallet = preprocess_path(path) + if redirect: + return redirect + print(f"{wallet=}") + next, nexty, prev, prevy, year = create_nav_links(wallet) wurl = f"/walletedit/{wallet}".replace("#", ":") wallet = wallet.replace(":", "#") @@ -706,7 +707,13 @@ def walletedit(request, path=None): except: samedate = None - thisexpo = Expedition.objects.get(year=int(year)) + try: + thisexpo = Expedition.objects.get(year=int(year)) + except: # creating a wallet for an expo that does not exist perhaps + message = f"Trying to access an Expo for '{year}' which does not exist (yet)." + message += " See /handbook/computing/newyear.html" + print(message) + return render(request, "errors/generic.html", {"message": message}) if samedate: svxothers = SurvexBlock.objects.filter(date=samedate) trips = LogbookEntry.objects.filter(date=samedate) diff --git a/templates/walletform.html b/templates/walletform.html index 948279b..da0e2f4 100644 --- a/templates/walletform.html +++ b/templates/walletform.html @@ -18,7 +18,7 @@ + placeholder="2023#nn" value="2023#" />