diff --git a/parsers/survex.py b/parsers/survex.py index 031d9e0..b9c26ca 100644 --- a/parsers/survex.py +++ b/parsers/survex.py @@ -843,26 +843,16 @@ class LoadingSurvex: def get_expo_from_year(self, year, line, survexblock): # cacheing to save DB query on every block - if year in self.expos: - expo = self.expos[year] + # Minimize ORM calls: cache all expeditions by year on first use + if not hasattr(self, '_expedition_cache'): + self._expedition_cache = {e.year: e for e in Expedition.objects.all()} + if year in self._expedition_cache: + expo = self._expedition_cache[year] else: - expeditions = Expedition.objects.filter(year=year) - if len(expeditions) > 1: - message = ( - f"! More than one expedition in year {year} '{line}' ({survexblock}) {survexblock.survexfile.path}" - ) - print(self.insp + message) - stash_data_issue( - parser="survex", message=message, url=None, sb=(survexblock.survexfile.path) - ) - if expeditions: - expo = expeditions[0] - self.expos[year] = expo - else: - expo = Expedition.objects.get(year="1976") - message = f"! DATE INCORRECT. There is no expedition for the year {year}. {survexblock.survexfile.path} ({survexblock}) - set to 1976." - print(self.insp + message) - stash_data_issue(parser='survex', message=message, url=None, sb=(survexblock.survexfile.path)) + message = f"! DATE INCORRECT. There is no expedition for the year {year}. {survexblock.survexfile.path} ({survexblock}) - set to 1976." + print(self.insp + message) + stash_data_issue(parser='survex', message=message, url=None, sb=(survexblock.survexfile.path)) + expo = self._expedition_cache.get("1976") return expo def LoadSurvexDate(self, survexblock, line):