diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-01-30 15:28:11 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-01-30 15:28:11 +0000 |
commit | 3742e0f3670bfe19c6bccce81a4ef9bb11953067 (patch) | |
tree | 57549d6dcf7851882a56364502effef75192d33b /core/views/logbooks.py | |
parent | 7d98980121531c8db21256e85590007f621c331c (diff) | |
download | troggle-3742e0f3670bfe19c6bccce81a4ef9bb11953067.tar.gz troggle-3742e0f3670bfe19c6bccce81a4ef9bb11953067.tar.bz2 troggle-3742e0f3670bfe19c6bccce81a4ef9bb11953067.zip |
fixing Sunday display on calendar
Diffstat (limited to 'core/views/logbooks.py')
-rw-r--r-- | core/views/logbooks.py | 74 |
1 files changed, 46 insertions, 28 deletions
diff --git a/core/views/logbooks.py b/core/views/logbooks.py index 2cfc183..5ffae6a 100644 --- a/core/views/logbooks.py +++ b/core/views/logbooks.py @@ -58,22 +58,32 @@ def notablepersons(request): def expedition(request, expeditionname): '''Returns a rendered page for one expedition, specified by the year e.g. '2019'. If page caching is enabled, it caches the dictionaries used to render the template page. + + This is not as difficult to understand as it looks. Yes there are many levels of indirection, with multiple trees being traversed at the same time. And the Django special syntax + makes this hard for normal Python programmers. + + Remember that 'personexpedition__expedition' is interpreted by Django to mean the + 'expedition' object which is connected by a foreign key to the 'personexpedition' + object, which is a field of the PersonTrip object: + PersonTrip.objects.filter(personexpedition__expedition=expo) + + Queries are not evaluated to hit the database until a result is actually used. Django + does lazy evaluation. + ''' + try: + expo = Expedition.objects.get(year=int(expeditionname)) + except: + message = f'Expedition not found - database apparently empty, you probably need to do a full re-import of all data.' + return render(request, 'errors/generic.html', {'message': message}) + if request.user.is_authenticated: - if "reload" in request.GET: - this_expedition = Expedition.objects.get(year=int(expeditionname)) - # Need to delete the existing entries or we get duplication - # Need to delete both in the Django ORM and in our own object-store. - entries = this_expedition.logbookentry_set.all() - for entry in entries: - #print(f'! - delete entry: "{entry}"') - entry.delete() - entries = this_expedition.logbookentry_set.all() - import_logbook(year=this_expedition.year) logged_in = True + if "reload" in request.GET: + expo.logbookentry_set.all().delete() + import_logbook(year=expo.year) else: logged_in = False - ts = TROG['pagecache']['expedition'] # not much use unless single user! if settings.CACHEDPAGES: @@ -83,33 +93,41 @@ def expedition(request, expeditionname): #print('! - expo {expeditionanme} using cached page') return render(request,'expedition.html', { **ts[expeditionname], 'logged_in' : logged_in }) - try: - this_expedition = Expedition.objects.get(year=int(expeditionname)) - except: - message = f'Expedition not found - database apparently empty, you probably need to do a full re-import of all data.' - return render(request, 'errors/generic.html', {'message': message}) - - expeditions = Expedition.objects.all() - personexpeditiondays = [ ] - dateditems = list(this_expedition.logbookentry_set.all()) + list(this_expedition.survexblock_set.all()) + expeditions = Expedition.objects.all() # top menu only, evaluated only when template renders + + entries = expo.logbookentry_set.all() + blocks = expo.survexblock_set.all() + dateditems = list(entries) + list(blocks) # evaluates the Django query and hits db dates = sorted(set([item.date for item in dateditems])) - for personexpedition in this_expedition.personexpedition_set.all(): + + allpersontrips = PersonTrip.objects.filter(personexpedition__expedition=expo) + + + personexpeditiondays = [ ] + for personexpedition in expo.personexpedition_set.all(): + expotrips = allpersontrips.filter(personexpedition=personexpedition) # lazy + expoblocks = blocks.filter(survexpersonrole__personexpedition=personexpedition) + prow = [ ] + for date in dates: - pcell = { "persontrips": PersonTrip.objects.filter(personexpedition=personexpedition, - logbook_entry__date=date) } - pcell["survexblocks"] = set(SurvexBlock.objects.filter(survexpersonrole__personexpedition=personexpedition, - date = date)) + personentries = expotrips.filter(logbook_entry__date=date) # lazy + personblocks = set(expoblocks.filter(date = date)) # not lazy + pcell = {} + pcell["personentries"] = personentries + pcell["survexblocks"] = personblocks + if issunday := (date.weekday() == 6): # WALRUS + pcell["sunday"] = issunday prow.append(pcell) personexpeditiondays.append({"personexpedition":personexpedition, "personrow":prow}) - ts[expeditionname] = {'expedition': this_expedition, 'expeditions':expeditions, + ts[expeditionname] = {'expedition': expo, + 'expeditions':expeditions, 'personexpeditiondays':personexpeditiondays, 'settings':settings, 'dateditems': dateditems, 'dates':dates} TROG['pagecache']['expedition'][expeditionname] = ts[expeditionname] - nexpos = len( TROG['pagecache']['expedition']) - #print(f'! - expo {expeditionname} pre-render N expos:{nexpos}') + return render(request,'expedition.html', { **ts[expeditionname], 'logged_in' : logged_in } ) class Expeditions_tsvListView(ListView): |