diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2022-12-20 00:07:55 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2022-12-20 00:07:55 +0000 |
commit | f24f283a073d070f1335239d0df47d499ba3876f (patch) | |
tree | 29f291c740335ec6fdbe8c7e8e2daebfc984c028 /core | |
parent | bb14c94ab10cbd279586c97822372bba8375b67b (diff) | |
download | troggle-f24f283a073d070f1335239d0df47d499ba3876f.tar.gz troggle-f24f283a073d070f1335239d0df47d499ba3876f.tar.bz2 troggle-f24f283a073d070f1335239d0df47d499ba3876f.zip |
attempted speedup, explicit dates wallet objects
Diffstat (limited to 'core')
-rw-r--r-- | core/models/survex.py | 16 | ||||
-rw-r--r-- | core/views/scans.py | 59 |
2 files changed, 51 insertions, 24 deletions
diff --git a/core/models/survex.py b/core/models/survex.py index 5d60e62..3c6ba47 100644 --- a/core/models/survex.py +++ b/core/models/survex.py @@ -172,6 +172,7 @@ class Wallet(models.Model): fpath = models.CharField(max_length=200) walletname = models.CharField(max_length=200) walletdate = models.DateField(blank=True, null=True) + walletyear = models.DateField(blank=True, null=True) class Meta: ordering = ('walletname',) @@ -213,6 +214,8 @@ class Wallet(models.Model): print(f' - {datestr=} ') try: thisdate = datetime.date.fromisoformat(datestr) + self.walletdate = thisdate + self.save() try: waldata["date"] = thisdate.isoformat() except: @@ -220,20 +223,24 @@ class Wallet(models.Model): from troggle.core.models.troggle import DataIssue DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl) except: - message = f"! {str(self.walletname)} Date format not ISO {datestr}. Failed to load fro, {jsonfile} JSON file" + message = f"! {str(self.walletname)} Date format not ISO {datestr}. Failed to load from {jsonfile} JSON file" from troggle.core.models.troggle import DataIssue DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl) return waldata def year(self): + '''This gets the year syntactically without opening and reading the JSON + ''' if len(self.walletname) < 5: return None if self.walletname[4] != "#": return None year = int(self.walletname[0:4]) - if year < 1976 or year > 2050: + if year < 1975 or year > 2050: return None else: + self.walletyear = datetime.date(year, 1, 1) + self.save() return str(year) @@ -243,7 +250,7 @@ class Wallet(models.Model): return self.walletdate if not self.get_json(): return None - jsondata = self.get_json() + jsondata = self.get_json() # use walrus operator? datestr = jsondata["date"] if not datestr: @@ -252,12 +259,13 @@ class Wallet(models.Model): datestr = datestr.replace('.','-') try: samedate = datetime.date.fromisoformat(datestr) + self.walletdate = samedate.isoformat() except: try: samedate = datetime.date.fromisoformat(datestr[:10]) + self.walletdate = samedate.isoformat() except: samedate = None - self.walletdate = samedate.isoformat() self.save() return self.walletdate diff --git a/core/views/scans.py b/core/views/scans.py index 9fd58d6..a7ce27a 100644 --- a/core/views/scans.py +++ b/core/views/scans.py @@ -8,6 +8,7 @@ from urllib.request import urlopen from django.conf import settings from django.shortcuts import render from django.http import HttpResponse +from django.db import transaction from troggle.core.models.survex import Wallet, SingleScan, SurvexBlock from troggle.core.models.troggle import Person, Expedition @@ -48,6 +49,9 @@ def populatewallet(w): w.persons = list(set(survexpeople)) def datewallet(w, earliest): + '''Gets the date of the youngest survexblock associated with the wallet + REFACTOR this to do the whole date-getting task + ''' first = earliest blocks = SurvexBlock.objects.filter(scanswallet = w) for b in blocks: @@ -59,6 +63,7 @@ def datewallet(w, earliest): w.date = None else: w.date = first.isoformat() + return w.date def caveifywallet(w): '''Gets the cave from the list of survex files, @@ -94,10 +99,13 @@ def fillblankpeople(w): # print(f' - {wp=} {nobody=}') populatewallet(w) -def fillblankothers(w): - earliest = datetime.datetime.now().date() - if not w.date(): - datewallet(w, earliest) +def fillblankothers(w): + if not w.walletdate: + earliest = datetime.datetime.now().date() + if not w.date(): # sets .walletdate as a side-effect, gets it from JSON + d =datewallet(w, earliest) # if nothing in JASON, it looks at the survex blocks + w.walletdate = d + w.save() Gcavelookup = GetCaveLookup() @@ -140,6 +148,8 @@ def walletslistperson(request, first_name, last_name): w.ticks = w.get_ticks() # the complaints in colour form fixsurvextick(w, w.ticks) return manywallets + + print(f"-walletslistperson") try: if last_name: @@ -153,37 +163,43 @@ def walletslistperson(request, first_name, last_name): manywallets = tickspersonwallet(p) expeditions = Expedition.objects.all() + print(f"--") return render(request, 'personwallets.html', { 'manywallets':manywallets, 'settings': settings, 'person': p, 'expeditions': expeditions}) +def setwalletsdates(): + wallets = Wallet.objects.filter(walletdate=None) + print(f"undated wallets: {len(wallets)}") + for w in wallets: + w.walletdate = w.date() + w.save() + def walletslistyear(request, year): - '''Page which displays a list of all the wallets in a specific year + '''Page which displays a list of all the wallets in a specific year. + We have a field .walletyear, which we set on import. ''' def ticksyearwallet(year): manywallets = [] - wallets = Wallet.objects.all() + wallets = Wallet.objects.filter(walletyear__year=year) for w in wallets: - - if year == w.year(): - manywallets.append(w) - fillblankpeople(w) - fillblankothers(w) - w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc - fixsurvextick(w, w.ticks) - else: - continue + manywallets.append(w) + fillblankpeople(w) + fillblankothers(w) + w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc + fixsurvextick(w, w.ticks) return manywallets - + print(f"-walletslistyear") if year < 1976 or year > 2050: return render(request, 'errors/generic.html', {'message': 'Year out of range. Must be between 1976 and 2050'}) - else: - year = str(year) + #return render(request, 'errors/generic.html', {'message': 'This page logic not implemented yet'}) - + + year = str(year) manywallets = ticksyearwallet(year) expeditions = Expedition.objects.all() - expedition = Expedition.objects.filter(year=year) + expedition = expeditions.filter(year=year) + print(f"--") return render(request, 'yearwallets.html', { 'manywallets':manywallets, 'settings': settings, 'year': year, 'expeditions': expeditions, 'expedition': expedition}) @@ -191,6 +207,8 @@ def walletslistyear(request, year): def cavewallets(request, caveid): '''Returns all the wallets for just one cave ''' + print(f"-cavewalletsl") + Gcavelookup = GetCaveLookup() if caveid in Gcavelookup: cave = Gcavelookup[caveid] @@ -224,6 +242,7 @@ def cavewallets(request, caveid): w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc fixsurvextick(w, w.ticks) expeditions = Expedition.objects.all() + print(f"--") return render(request, 'cavewallets.html', { 'manywallets':manywallets, 'settings': settings, 'cave': cave, 'expeditions': expeditions}) |