diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-02-27 22:23:24 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-02-27 22:23:24 +0000 |
commit | 5c3927c25d51e907a176203431be3b23cdcfd8ae (patch) | |
tree | edc1757cedc791dd549392a5f0afb766e9f2a264 /core/views/survex.py | |
parent | 154722f765d99c014ddd43f8cbce47799bdc3cce (diff) | |
download | troggle-5c3927c25d51e907a176203431be3b23cdcfd8ae.tar.gz troggle-5c3927c25d51e907a176203431be3b23cdcfd8ae.tar.bz2 troggle-5c3927c25d51e907a176203431be3b23cdcfd8ae.zip |
all working, queries improved, date-ordered.
Diffstat (limited to 'core/views/survex.py')
-rw-r--r-- | core/views/survex.py | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/core/views/survex.py b/core/views/survex.py index 9507795..2a3c67d 100644 --- a/core/views/survex.py +++ b/core/views/survex.py @@ -8,6 +8,7 @@ from collections import namedtuple from django import forms from django.db import models +from django.db.models import Q from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.http import HttpResponse @@ -257,7 +258,7 @@ def svx(request, survex_file): rform = SvxForm(request.POST) # if rform.is_valid(): # All validation rules pass (how do we check it against the filename and users?) rcode = rform.cleaned_data["code"] - outputtype = rform.cleaned_data["outputtype"] + outputtype = rform.cleaned_data["outputtype"] # used by CodeMirror ajax I think difflist = form.DiffCode(rcode) # print(">>>> ", rform.data) @@ -296,24 +297,16 @@ def svx(request, survex_file): svxincludes = re.findall(r"(?i)\*include\s+(\S+)", form.data["code"] or "") svxfile = form.survexfile # only valid once form.GetDiscCode() called - print(f"{svxfile=}") 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"- {b}") - # collect all the stuff that happens on the same dates as the survex blocks + # collect all the survex blocks which actually have a valid date + svxblocks = svxfile.survexblock_set.filter(date__isnull=False).order_by('date') - dates = set() - for b in svxblocks: - dates.add(b.date) - print(f"- {b.date}") - events = events_on_dates(dates) + + events = events_on_dates(svxblocks) vmap = { "settings": settings, @@ -329,30 +322,40 @@ def svx(request, survex_file): } # vmap.update(csrf(request)) # this now refreshes to the wrong value, now that we user render(request, - if outputtype == "ajax": + if outputtype == "ajax": # used by CodeMirror ajax I think return render(request, "svxfiledifflistonly.html", vmap) return render(request, "svxfile.html", vmap) -SameDateEvents = namedtuple('SameDateEvents', ['trips', 'svxfiles', 'wallets']) +SameDateEvents = namedtuple('SameDateEvents', ['trips', 'svxfiles', 'wallets', 'blocks']) -def events_on_dates(dates): +def events_on_dates(svxblocks): """Returns a dictionary of indexed by date. For each date there is a named tuple of 3 lists: logbookentries, survexfiles (NB files, not blocks), and wallets. """ + # deduplicate but maintain date order + dates = [] + for b in svxblocks: + if b.date not in dates: + dates.append(b.date) + # print(f"- {b.date}") + events = {} - for d in dates: - trips = LogbookEntry.objects.filter(date=d) + for date in dates: + trips = LogbookEntry.objects.filter(date=date) + + svxfiles = SurvexFile.objects.filter(survexblock__date=date).distinct() - svxfiles = False + # https://stackoverflow.com/questions/739776/how-do-i-do-an-or-filter-in-a-django-query + wallets = Wallet.objects.filter(Q(survexblock__date=date) | Q(walletdate=date)).distinct() - # Wallets needs to get those identified only from JSON too, - # see logbookeentry() in views/logbooks.py - allwallets = Wallet.objects.all() - refwallets = allwallets.filter(survexblock__date=d) + blocks = [] + for b in svxblocks: + if b.date == date: + blocks.append(b.name) - events[d] = SameDateEvents(trips=trips, svxfiles=svxfiles, wallets=refwallets) - print(events) + events[date] = SameDateEvents(trips=trips, svxfiles=svxfiles, wallets=wallets, blocks=blocks) + # print(events) return events # The cavern running function. This is NOT where it is run inside the form! see SvxForm.Process() for that |