From 6de4fa66a2ea7986230c3bb0f64887488acaa33f Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sun, 26 Feb 2023 22:13:37 +0000 Subject: [PATCH] Initial add of On This Day links to svx file page --- core/models/caves.py | 2 ++ core/models/survex.py | 12 ++++++----- core/views/survex.py | 40 +++++++++++++++++++++++++++++++---- parsers/survex.py | 2 +- templates/logbookentry.html | 42 +------------------------------------ templates/onthisdate.html | 42 +++++++++++++++++++++++++++++++++++++ templates/svxfile.html | 15 +++++++++++-- templates/wallet_table.html | 2 +- templates/walletform.html | 2 +- 9 files changed, 104 insertions(+), 55 deletions(-) create mode 100644 templates/onthisdate.html diff --git a/core/models/caves.py b/core/models/caves.py index dffd670..1de5ef9 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -32,6 +32,8 @@ todo = """ foreign keys work fine ?! - Why do we have CaveAndEntrance objects ? Surely entranceletter belong son the Entrance object? + +- move the aliases list from the code and put into an editable file - Restore constraint: unique_together = (("area", "kataster_number"), ("area", "unofficial_number")) """ diff --git a/core/models/survex.py b/core/models/survex.py index 338a235..c6d6af1 100644 --- a/core/models/survex.py +++ b/core/models/survex.py @@ -1,6 +1,7 @@ import os import re from urllib.parse import urljoin +from pathlib import Path from django.conf import settings from django.db import models @@ -39,8 +40,8 @@ class SurvexFile(models.Model): # return "[SurvexFile:"+str(self.path) + "-" + str(self.survexdirectory) + "-" + str(self.cave)+"]" def exists(self): - fname = os.path.join(settings.SURVEX_DATA, self.path + ".svx") - return os.path.isfile(fname) + fname = Path(settings.SURVEX_DATA, self.path + ".svx") + return fname.is_file() def OpenFile(self): fname = os.path.join(settings.SURVEX_DATA, self.path + ".svx") @@ -64,6 +65,8 @@ class SurvexFile(models.Model): class SurvexStationLookUpManager(models.Manager): + """Don't know what this does, + https://docs.djangoproject.com/en/dev/topics/db/managers/""" def lookup(self, name): blocknames, sep, stationname = name.rpartition(".") return self.get(block=SurvexBlock.objects.lookup(blocknames), name__iexact=stationname) @@ -99,9 +102,8 @@ class SurvexStation(models.Model): # Single SurvexBlock # class SurvexBlockLookUpManager(models.Manager): - """Don't know what this does, suspect it is part of the Django admin - system""" - + """Don't know what this does, + https://docs.djangoproject.com/en/dev/topics/db/managers/ """ def lookup(self, name): if name == "": blocknames = [] diff --git a/core/views/survex.py b/core/views/survex.py index 3575bc9..70f4616 100644 --- a/core/views/survex.py +++ b/core/views/survex.py @@ -6,6 +6,8 @@ import socket from pathlib import Path from django import forms +from django.db import models + from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.http import HttpResponse from django.shortcuts import render @@ -13,14 +15,20 @@ from django.views.decorators.csrf import ensure_csrf_cookie import troggle.settings as settings from troggle.core.models.caves import Cave +from troggle.core.models.survex import SurvexFile, SurvexBlock from troggle.core.utils import only_commit """Everything that views survexfiles but also displays data on a cave or caves when there is ambiguity """ -todo = """survexcavesingle is not properly producing any result for Homecoming, 1626-359, 2018-dm-07 +todo = """- survexcavesingle is not properly producing any result for Homecoming, 1626-359, 2018-dm-07 even though there are dozens of surveys. + +- Never actual uses the object for the survexfile, works entirely from the filepath! Make it check and validate + +- Save the edited survexfile as a survexfile object and re-parse it, and update +all its dependencies """ survexdatasetpath = Path(settings.SURVEX_DATA) @@ -107,6 +115,7 @@ class SvxForm(forms.Form): datetime = forms.DateTimeField(widget=forms.TextInput(attrs={"readonly": True})) outputtype = forms.CharField(widget=forms.TextInput(attrs={"readonly": True})) code = forms.CharField(widget=forms.Textarea(attrs={"cols": 140, "rows": 36})) + survexfile = models.ForeignKey(SurvexFile, blank=True, null=True, on_delete=models.SET_NULL) # 1:1 ? template = False @@ -116,6 +125,16 @@ class SvxForm(forms.Form): print(">>> >>> WARNING - svx file not found, showing TEMPLATE SVX", fname, flush=True) self.template = True return survextemplatefile + refs = SurvexFile.objects.filter(path=self.data["filename"]) + if len(refs)==1: + self.survexfile = SurvexFile.objects.get(path=self.data["filename"]) + else: + self.survexfile = refs[0] + # OK this is due to a bug in the import file parsing, whoops. + print("BUG - to be fixed in the survex parser - not critical..") + print(f"Number of SurvexFile objects found: {len(refs)}") + for s in refs: + print (s.path, s.survexdirectory, s.cave) try: fin = open(fname, "r", encoding="utf8", newline="") svxtext = fin.read() @@ -215,12 +234,12 @@ def svx(request, survex_file): """Displays a single survex file in an textarea window (using a javascript online editor to enable editing) with buttons which allow SAVE, check for DIFFerences from saved, and RUN (which runs the cavern executable and displays the output below the main textarea). - Requires CSRF to be set up correct;ly, and requires permission to write to the filesystem. + Requires CSRF to be set up correctly, and requires permission to write to the filesystem. """ warning = False # get the basic data from the file given in the URL - dirname = os.path.split(survex_file)[0] + dirname = os.path.split(survex_file)[0] # replace with proper pathlib function.. dirname += "/" nowtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") outputtype = "normal" @@ -273,11 +292,24 @@ def svx(request, survex_file): # print [ form.data['code'] ] svxincludes = re.findall(r"(?i)\*include\s+(\S+)", form.data["code"] or "") + svxfile = form.survexfile # only valid once form.GetDiscCode() called + try: + svxblocksall = svxfile.survexblock_set.all() + except AttributeError: # some survexfiles just *include files and have no blocks themselves + svxblocksall = [] + svxblocks = [] + for b in svxblocksall: + if b.date: + svxblocks.append(b) + print(f"{svxfile=} {svxblocks}") + + vmap = { "settings": settings, "warning": warning, "has_3d": (Path(survexdatasetpath) / Path(survex_file + ".3d")).is_file(), "title": survex_file, + "svxblocks": svxblocks, "svxincludes": svxincludes, "difflist": difflist, "logmessage": logmessage, @@ -521,7 +553,7 @@ def survexcavesingle(request, survex_cave): def check_cave_registered(area, survex_cave): """Checks whether a cave has been properly registered when it is found in the Loser repo - This should really be called by Databasereset not here in a view + This should really be called by databaseReset not here in a view Currently Caves are only registered if they are listed in :expoweb: settings.CAVEDESCRIPTIONS so we need to add in any more here. diff --git a/parsers/survex.py b/parsers/survex.py index b88267a..3cf3168 100644 --- a/parsers/survex.py +++ b/parsers/survex.py @@ -126,7 +126,7 @@ def get_team_on_trip(survexblock): if survexblock in trip_people_cache: return trip_people_cache[survexblock] - qpeople = SurvexPersonRole.objects.filter(survexblock=survexblock) + qpeople = SurvexPersonRole.objects.filter(survexblock=survexblock) # not very good Django style trip_people_cache[survexblock] = qpeople # this is a query list return qpeople diff --git a/templates/logbookentry.html b/templates/logbookentry.html index 6fff583..2fdfb41 100644 --- a/templates/logbookentry.html +++ b/templates/logbookentry.html @@ -71,46 +71,6 @@ - - {% if svxothers %}Survex files on this date:
- - {% for item in svxothers %} - {% if item.isSurvexBlock %}    {{item.survexfile.path|safe}}
{% endif %} - {% empty %} - None found for this date.
- {% endfor %} -
- {% else %} - No survex files found for this date.
- {% endif %} - {% if wallets %}Wallets on this date:
- - {% for item in wallets %} -     {{item.walletname|safe}} - {% if item.name %} - {{item.name|safe}}
- {% else %} - {{item.get_fnames|safe}} -
- {% endif %} - {% empty %} - None found for this date.
- {% endfor %} -
- {% else %} - No wallets files found for this date.
- {% endif %} - {% if trips %}All logbook trips on this date:
- - {% for item in trips %} - {% if item.isLogbookEntry %}    {{item.title|safe}}
{% endif %} - {% empty %} - None found for this date, but there should be..
- {% endfor %} -
- {% else %} - Hmm...
- {% endif %} - + {% include 'onthisdate.html' %}
{% endblock %} diff --git a/templates/onthisdate.html b/templates/onthisdate.html new file mode 100644 index 0000000..dd75144 --- /dev/null +++ b/templates/onthisdate.html @@ -0,0 +1,42 @@ + + + {% if svxothers %}Survex files on this date:
+ + {% for item in svxothers %} + {% if item.isSurvexBlock %}    {{item.survexfile.path|safe}}
{% endif %} + {% empty %} + None found for this date.
+ {% endfor %} +
+ {% else %} + No survex files found for this date.
+ {% endif %} + {% if wallets %}Wallets on this date:
+ + {% for item in wallets %} +     {{item.walletname|safe}} + {% if item.name %} + {{item.name|safe}}
+ {% else %} + {{item.get_fnames|safe}} +
+ {% endif %} + {% empty %} + None found for this date.
+ {% endfor %} +
+ {% else %} + No wallets files found for this date.
+ {% endif %} + {% if trips %}All logbook trips on this date:
+ + {% for item in trips %} + {% if item.isLogbookEntry %}    {{item.title|safe}}
{% endif %} + {% empty %} + None found for this date, but there should be..
+ {% endfor %} +
+ {% else %} + Hmm...
+ {% endif %} + \ No newline at end of file diff --git a/templates/svxfile.html b/templates/svxfile.html index 3417859..45e0127 100644 --- a/templates/svxfile.html +++ b/templates/svxfile.html @@ -80,6 +80,17 @@ LOGMESSAGES --> {% endif %} - - + +{% for sb in svxblocks %} +
{{sb.date|date:"Y-m-d"}} {{sb.title}} '{{sb.name}}' +List of Logbook entries, wallets and other survex files on {{sb.date|date:"Y-m-d"}}. +
+[Work in progress, Feb. 2023...]
+ {% include 'onthisdate.html' %} +
+{% empty %} +Cannot find any survex blocks in this survex file.
+Report this to a nerd if you think this is incorrect. +{% endfor %} +
{% endblock %} diff --git a/templates/wallet_table.html b/templates/wallet_table.html index d5aead8..4e1c81b 100644 --- a/templates/wallet_table.html +++ b/templates/wallet_table.html @@ -1,4 +1,4 @@ - + diff --git a/templates/walletform.html b/templates/walletform.html index 1583393..c35fa03 100644 --- a/templates/walletform.html +++ b/templates/walletform.html @@ -246,7 +246,7 @@ {% for item in svxothers %} {% if item.isSurvexBlock %}    {{item.survexfile.path|safe}}
{% endif %} {% empty %} - None found for this date, bit there should be..
+ None found for this date, but there should be..
{% endfor %} {% else %}
WalletWallet DateCaveWallet NameSurvex survey length