diff options
Diffstat (limited to 'core/models/caves.py')
-rw-r--r-- | core/models/caves.py | 320 |
1 files changed, 161 insertions, 159 deletions
diff --git a/core/models/caves.py b/core/models/caves.py index 921a5a4..6123210 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -23,33 +23,33 @@ from django.urls import reverse import settings from troggle.core.models.logbooks import QM from troggle.core.models.survex import SurvexStation -from troggle.core.models.troggle import (DataIssue, Expedition, Person, - TroggleModel) +from troggle.core.models.troggle import DataIssue, Expedition, Person, TroggleModel from troggle.core.utils import TROG, writetrogglefile # Use the TROG global object to cache the cave lookup list. No good for multi-user.. -Gcavelookup = TROG['caves']['gcavelookup'] -Gcave_count = TROG['caves']['gcavecount'] +Gcavelookup = TROG["caves"]["gcavelookup"] +Gcave_count = TROG["caves"]["gcavecount"] Gcavelookup = None Gcave_count = None -'''The model declarations for Areas, Caves and Entrances -''' +"""The model declarations for Areas, Caves and Entrances +""" -todo=''' +todo = """ - Find out why we have separate objects CaveSlug and EntranceSlug and why these are not just a single field on the Model. Do we ever need more than one slug per cave or entrance? Surely that would break everything?? - Restore constraint: unique_together = (("area", "kataster_number"), ("area", "unofficial_number")) -''' +""" + class Area(TroggleModel): short_name = models.CharField(max_length=100) name = models.CharField(max_length=200, blank=True, null=True) description = models.TextField(blank=True, null=True) - super = models.ForeignKey('Area', blank=True, null=True, on_delete=models.SET_NULL) + super = models.ForeignKey("Area", blank=True, null=True, on_delete=models.SET_NULL) def __str__(self): if self.super: @@ -63,56 +63,62 @@ class Area(TroggleModel): elif self.super: return self.super.kat_area() + class CaveAndEntrance(models.Model): - cave = models.ForeignKey('Cave',on_delete=models.CASCADE) - entrance = models.ForeignKey('Entrance',on_delete=models.CASCADE) - entrance_letter = models.CharField(max_length=20,blank=True, null=True) + cave = models.ForeignKey("Cave", on_delete=models.CASCADE) + entrance = models.ForeignKey("Entrance", on_delete=models.CASCADE) + entrance_letter = models.CharField(max_length=20, blank=True, null=True) + class Meta: - unique_together = [['cave', 'entrance'], ['cave', 'entrance_letter']] - ordering = ['entrance_letter'] - + unique_together = [["cave", "entrance"], ["cave", "entrance_letter"]] + ordering = ["entrance_letter"] + def __str__(self): return str(self.cave) + str(self.entrance_letter) - class Cave(TroggleModel): - # too much here perhaps, + # too much here perhaps, official_name = models.CharField(max_length=160) area = models.ManyToManyField(Area, blank=True) - kataster_code = models.CharField(max_length=20,blank=True, null=True) - kataster_number = models.CharField(max_length=10,blank=True, null=True) - unofficial_number = models.CharField(max_length=60,blank=True, null=True) - entrances = models.ManyToManyField('Entrance', through='CaveAndEntrance') - explorers = models.TextField(blank=True,null=True) - underground_description = models.TextField(blank=True,null=True) - equipment = models.TextField(blank=True,null=True) - references = models.TextField(blank=True,null=True) - survey = models.TextField(blank=True,null=True) - kataster_status = models.TextField(blank=True,null=True) - underground_centre_line = models.TextField(blank=True,null=True) - notes = models.TextField(blank=True,null=True) - length = models.CharField(max_length=100,blank=True, null=True) - depth = models.CharField(max_length=100,blank=True, null=True) - extent = models.CharField(max_length=100,blank=True, null=True) - survex_file = models.CharField(max_length=100,blank=True, null=True) - description_file = models.CharField(max_length=200,blank=True, null=True) - url = models.CharField(max_length=200,blank=True, null=True) + kataster_code = models.CharField(max_length=20, blank=True, null=True) + kataster_number = models.CharField(max_length=10, blank=True, null=True) + unofficial_number = models.CharField(max_length=60, blank=True, null=True) + entrances = models.ManyToManyField("Entrance", through="CaveAndEntrance") + explorers = models.TextField(blank=True, null=True) + underground_description = models.TextField(blank=True, null=True) + equipment = models.TextField(blank=True, null=True) + references = models.TextField(blank=True, null=True) + survey = models.TextField(blank=True, null=True) + kataster_status = models.TextField(blank=True, null=True) + underground_centre_line = models.TextField(blank=True, null=True) + notes = models.TextField(blank=True, null=True) + length = models.CharField(max_length=100, blank=True, null=True) + depth = models.CharField(max_length=100, blank=True, null=True) + extent = models.CharField(max_length=100, blank=True, null=True) + survex_file = models.CharField(max_length=100, blank=True, null=True) + description_file = models.CharField(max_length=200, blank=True, null=True) + url = models.CharField(max_length=200, blank=True, null=True) filename = models.CharField(max_length=200) - #class Meta: + # class Meta: # unique_together = (("area", "kataster_number"), ("area", "unofficial_number")) - # FIXME Kataster Areas and CUCC defined sub areas need seperating + # FIXME Kataster Areas and CUCC defined sub areas need seperating + + # href = models.CharField(max_length=100) - #href = models.CharField(max_length=100) - class Meta: - ordering = ('kataster_code', 'unofficial_number') + ordering = ("kataster_code", "unofficial_number") def hassurvey(self): if not self.underground_centre_line: return "No" - if (self.survey.find("<img") > -1 or self.survey.find("<a") > -1 or self.survey.find("<IMG") > -1 or self.survey.find("<A") > -1): + if ( + self.survey.find("<img") > -1 + or self.survey.find("<a") > -1 + or self.survey.find("<IMG") > -1 + or self.survey.find("<A") > -1 + ): return "Yes" return "Missing" @@ -122,9 +128,9 @@ class Cave(TroggleModel): if self.survex_filcavee: return "Yes" return "Missing" - + def slug(self): - primarySlugs = self.caveslug_set.filter(primary = True) + primarySlugs = self.caveslug_set.filter(primary=True) if primarySlugs: return primarySlugs[0].slug else: @@ -133,14 +139,14 @@ class Cave(TroggleModel): return slugs[0].slug def ours(self): - return bool(re.search(r'CUCC', self.explorers)) + return bool(re.search(r"CUCC", self.explorers)) def reference(self): if self.kataster_number: return f"{self.kat_area()}-{self.kataster_number}" else: return f"{self.kat_area()}-{self.unofficial_number}" - + def get_absolute_url(self): if self.kataster_number: href = self.kataster_number @@ -148,34 +154,35 @@ class Cave(TroggleModel): href = self.unofficial_number else: href = self.official_name.lower() - #return settings.URL_ROOT + '/cave/' + href + '/' - #return urljoin(settings.URL_ROOT, reverse('cave',kwargs={'cave_id':href,})) # WRONG. This produces /cave/161 and should be /1623/161 - return Path(settings.URL_ROOT) / self.url # not good Django style.. NEEDS actual URL - + # return settings.URL_ROOT + '/cave/' + href + '/' + # return urljoin(settings.URL_ROOT, reverse('cave',kwargs={'cave_id':href,})) # WRONG. This produces /cave/161 and should be /1623/161 + return Path(settings.URL_ROOT) / self.url # not good Django style.. NEEDS actual URL + def url_parent(self): return self.url.rsplit("/", 1)[0] - def __str__(self, sep = ": "): + def __str__(self, sep=": "): return str(self.slug()) def get_QMs(self): - '''Searches for all QMs that reference this cave. - ''' - #qms = self.qm_set.all().order_by('expoyear', 'block__date') - qms = QM.objects.filter(cave=self).order_by('expoyear', 'block__date') # a QuerySet, see https://docs.djangoproject.com/en/4.0/ref/models/querysets/#order-by - return qms # a QuerySet + """Searches for all QMs that reference this cave.""" + # qms = self.qm_set.all().order_by('expoyear', 'block__date') + qms = QM.objects.filter(cave=self).order_by( + "expoyear", "block__date" + ) # a QuerySet, see https://docs.djangoproject.com/en/4.0/ref/models/querysets/#order-by + return qms # a QuerySet def kat_area(self): for a in self.area.all(): if a.kat_area(): return a.kat_area() - + def entrances(self): return CaveAndEntrance.objects.filter(cave=self) def singleentrance(self): return len(CaveAndEntrance.objects.filter(cave=self)) == 1 - + def entrancelist(self): rs = [] res = "" @@ -183,11 +190,11 @@ class Cave(TroggleModel): if e.entrance_letter: rs.append(e.entrance_letter) rs.sort() - prevR = '' + prevR = "" n = 0 for r in rs: if prevR: - if chr(ord(prevR) + 1 ) == r: + if chr(ord(prevR) + 1) == r: prevR = r n += 1 else: @@ -205,27 +212,27 @@ class Cave(TroggleModel): else: res += "–" + prevR return res - + def writeDataFile(self): filepath = os.path.join(settings.CAVEDESCRIPTIONS, self.filename) - t = loader.get_template('dataformat/cave.xml') + t = loader.get_template("dataformat/cave.xml") now = datetime.now(timezone.utc) print(now) - c = dict({'cave': self, 'date': now}) + c = dict({"cave": self, "date": now}) u = t.render(c) writetrogglefile(filepath, u) return - + def file_output(self): filepath = Path(os.path.join(settings.CAVEDESCRIPTIONS, self.filename)) - t = loader.get_template('dataformat/cave.xml') + t = loader.get_template("dataformat/cave.xml") now = datetime.now(timezone.utc) - c = dict({'cave': self, 'date': now}) + c = dict({"cave": self, "date": now}) content = t.render(c) return (filepath, content, "utf8") - + def getArea(self): areas = self.area.all() lowestareas = list(areas) @@ -237,40 +244,39 @@ class Cave(TroggleModel): pass return lowestareas[0] + class EntranceSlug(models.Model): - entrance = models.ForeignKey('Entrance',on_delete=models.CASCADE) - slug = models.SlugField(max_length=50, unique = True) + entrance = models.ForeignKey("Entrance", on_delete=models.CASCADE) + slug = models.SlugField(max_length=50, unique=True) primary = models.BooleanField(default=False) + class Entrance(TroggleModel): - name = models.CharField(max_length=100, blank=True,null=True) - entrance_description = models.TextField(blank=True,null=True) - explorers = models.TextField(blank=True,null=True) - map_description = models.TextField(blank=True,null=True) - location_description = models.TextField(blank=True,null=True) - lastvisit = models.TextField(blank=True,null=True) - approach = models.TextField(blank=True,null=True) - underground_description = models.TextField(blank=True,null=True) - photo = models.TextField(blank=True,null=True) + name = models.CharField(max_length=100, blank=True, null=True) + entrance_description = models.TextField(blank=True, null=True) + explorers = models.TextField(blank=True, null=True) + map_description = models.TextField(blank=True, null=True) + location_description = models.TextField(blank=True, null=True) + lastvisit = models.TextField(blank=True, null=True) + approach = models.TextField(blank=True, null=True) + underground_description = models.TextField(blank=True, null=True) + photo = models.TextField(blank=True, null=True) MARKING_CHOICES = ( - ('P', 'Paint'), - ('P?', 'Paint (?)'), - ('T', 'Tag'), - ('T?', 'Tag (?)'), - ('R', 'Needs Retag'), - ('S', 'Spit'), - ('S?', 'Spit (?)'), - ('U', 'Unmarked'), - ('?', 'Unknown')) + ("P", "Paint"), + ("P?", "Paint (?)"), + ("T", "Tag"), + ("T?", "Tag (?)"), + ("R", "Needs Retag"), + ("S", "Spit"), + ("S?", "Spit (?)"), + ("U", "Unmarked"), + ("?", "Unknown"), + ) marking = models.CharField(max_length=2, choices=MARKING_CHOICES) - marking_comment = models.TextField(blank=True,null=True) - FINDABLE_CHOICES = ( - ('?', 'To be confirmed ...'), - ('S', 'Coordinates'), - ('L', 'Lost'), - ('R', 'Refindable')) + marking_comment = models.TextField(blank=True, null=True) + FINDABLE_CHOICES = (("?", "To be confirmed ..."), ("S", "Coordinates"), ("L", "Lost"), ("R", "Refindable")) findability = models.CharField(max_length=1, choices=FINDABLE_CHOICES, blank=True, null=True) - findability_description = models.TextField(blank=True,null=True) + findability_description = models.TextField(blank=True, null=True) alt = models.TextField(blank=True, null=True) northing = models.TextField(blank=True, null=True) easting = models.TextField(blank=True, null=True) @@ -279,14 +285,14 @@ class Entrance(TroggleModel): tag_station = models.TextField(blank=True, null=True) exact_station = models.TextField(blank=True, null=True) other_station = models.TextField(blank=True, null=True) - other_description = models.TextField(blank=True,null=True) - bearings = models.TextField(blank=True,null=True) - url = models.CharField(max_length=200,blank=True, null=True) + other_description = models.TextField(blank=True, null=True) + bearings = models.TextField(blank=True, null=True) + url = models.CharField(max_length=200, blank=True, null=True) filename = models.CharField(max_length=200) - cached_primary_slug = models.CharField(max_length=200,blank=True, null=True) - + cached_primary_slug = models.CharField(max_length=200, blank=True, null=True) + class Meta: - ordering = ['caveandentrance__entrance_letter'] + ordering = ["caveandentrance__entrance_letter"] def __str__(self): return str(self.slug()) @@ -298,11 +304,7 @@ class Entrance(TroggleModel): return SurvexStation.objects.lookup(self.other_station) def find_location(self): - r = {'': 'To be entered ', - '?': 'To be confirmed:', - 'S': '', - 'L': 'Lost:', - 'R': 'Refindable:'}[self.findability] + r = {"": "To be entered ", "?": "To be confirmed:", "S": "", "L": "Lost:", "R": "Refindable:"}[self.findability] if self.tag_station: try: s = SurvexStation.objects.lookup(self.tag_station) @@ -337,7 +339,12 @@ class Entrance(TroggleModel): def has_photo(self): if self.photo: - if (self.photo.find("<img") > -1 or self.photo.find("<a") > -1 or self.photo.find("<IMG") > -1 or self.photo.find("<A") > -1): + if ( + self.photo.find("<img") > -1 + or self.photo.find("<a") > -1 + or self.photo.find("<IMG") > -1 + or self.photo.find("<A") > -1 + ): return "Yes" else: return "Missing" @@ -363,17 +370,17 @@ class Entrance(TroggleModel): def get_absolute_url(self): # ancestor_titles='/'.join([subcave.title for subcave in self.get_ancestors()]) # if ancestor_titles: - # res = '/'.join((self.get_root().cave.get_absolute_url(), ancestor_titles, self.title)) + # res = '/'.join((self.get_root().cave.get_absolute_url(), ancestor_titles, self.title)) # else: - # res = '/'.jocavein((self.get_root().cave.get_absolute_url(), self.title)) + # res = '/'.jocavein((self.get_root().cave.get_absolute_url(), self.title)) # return res - res = '/'.join((self.get_root().cave.get_absolute_url(), self.title)) + res = "/".join((self.get_root().cave.get_absolute_url(), self.title)) return res def slug(self): if not self.cached_primary_slug: - primarySlugs = self.entranceslug_set.filter(primary = True) - if primarySlugs: + primarySlugs = self.entranceslug_set.filter(primary=True) + if primarySlugs: self.cached_primary_slug = primarySlugs[0].slug self.save() else: @@ -390,30 +397,29 @@ class Entrance(TroggleModel): if e.cave: rs.append(e.cave) return rs - def get_file_path(self): - return Path(settings.ENTRANCEDESCRIPTIONS, self.filename) - + return Path(settings.ENTRANCEDESCRIPTIONS, self.filename) + def file_output(self): filepath = Path(os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename)) - t = loader.get_template('dataformat/entrance.xml') + t = loader.get_template("dataformat/entrance.xml") now = datetime.now(timezone.utc) - c = dict({'entrance': self, 'date': now}) + c = dict({"entrance": self, "date": now}) content = t.render(c) return (filepath, content, "utf8") def writeDataFile(self): filepath = os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename) - t = loader.get_template('dataformat/entrance.xml') + t = loader.get_template("dataformat/entrance.xml") now = datetime.now(timezone.utc) - c = dict({'entrance': self, 'date': now}) + c = dict({"entrance": self, "date": now}) u = t.render(c) writetrogglefile(filepath, u) return - + def url_parent(self): if self.url: return self.url.rsplit("/", 1)[0] @@ -423,21 +429,22 @@ class Entrance(TroggleModel): return cavelist[0].url_parent() else: return "" - + def GetCaveLookup(): """A very relaxed way of finding probably the right cave given almost any string which might serve to identify it - + lookup function modelled on GetPersonExpeditionNameLookup repeated assignment each call, needs refactoring - + Used when parsing wallets contents.json file too in views/uploads.py - + Does NOT detect duplicates! Needs fixing. Needs to be a proper funciton that raises an exception if there is a duplicate. - OR we could set it to return None if there are duplicates, and require the caller to + OR we could set it to return None if there are duplicates, and require the caller to fall back on doing the actual database query it wants rather thna using this cache shortcut """ + def checkcaveid(cave, id): global Gcavelookup if id not in Gcavelookup: @@ -445,48 +452,48 @@ def GetCaveLookup(): Gcave_count[id] += 1 else: if cave == Gcavelookup[id]: - pass # same id, same cave - else: # same id but different cave + pass # same id, same cave + else: # same id but different cave message = f" - Warning: same alias id '{id:3}' for two caves '{Gcavelookup[id]}' and '{cave}'. Removing this shorthand alias entirely." Gcavelookup.pop(id) print(message) - DataIssue.objects.create(parser='aliases', message=message) - + DataIssue.objects.create(parser="aliases", message=message) + global Gcavelookup if Gcavelookup: return Gcavelookup Gcavelookup = {"NONEPLACEHOLDER": None} global Gcave_count - Gcave_count = defaultdict(int) # sets default value to int(0) - - DataIssue.objects.filter(parser='aliases').delete() - + Gcave_count = defaultdict(int) # sets default value to int(0) + + DataIssue.objects.filter(parser="aliases").delete() + for cave in Cave.objects.all(): key = cave.official_name.lower() if key != "" and key != "unamed" and key != "unnamed": Gcavelookup[key] = cave - Gcave_count[key] += 1 + Gcave_count[key] += 1 if cave.kataster_number: - checkcaveid(cave,cave.kataster_number) # we do expect 1623/55 and 1626/55 to cause a warning message - + checkcaveid(cave, cave.kataster_number) # we do expect 1623/55 and 1626/55 to cause a warning message + # the rest of these are 'nice to have' but may validly already be set if cave.unofficial_number: unoffn = cave.unofficial_number.lower() - checkcaveid(cave,unoffn) - + checkcaveid(cave, unoffn) + if cave.filename: # this is the slug - usually.. but usually done as as f'{cave.area}-{cave.kataster_number}' - fn = cave.filename.replace(".html","").lower() - checkcaveid(cave,fn) - + fn = cave.filename.replace(".html", "").lower() + checkcaveid(cave, fn) + if cave.slug(): # also possibly done already slug = cave.slug().lower() - checkcaveid(cave,slug) + checkcaveid(cave, slug) # These might alse create more duplicate entries # Yes, this should be set in, and imported from, settings.py - aliases =[ + aliases = [ ("1987-02", "267"), ("1990-01", "171"), ("1990-02", "172"), @@ -570,29 +577,25 @@ def GetCaveLookup(): ("2015-mf-06", "288"), ("2016-jb-01", "289"), ("2017-pw-01", "277"), - ("2018-dm-07", "359"), # NB this is 1626 - ("2017_cucc_24", "291"), # note _ not - - ("2017_cucc_23", "295"), # note _ not - - ("2017_cucc_28", "290"), # note _ not - + ("2018-dm-07", "359"), # NB this is 1626 + ("2017_cucc_24", "291"), # note _ not - + ("2017_cucc_23", "295"), # note _ not - + ("2017_cucc_28", "290"), # note _ not - ("bs17", "283"), - ("1976/b11", "198"), ("1976/b8", "197"), ("1976/b9", "190"), ("b11", "1976/b11"), ("b8", "1976/b8"), ("b9", "1976/b9"), - ("2011-01-bs30", "190"), ("bs30", "190"), ("2011-01", "190"), - ("quarriesd", "2002-08"), ("2002-x11", "2005-08"), ("2002-x12", "2005-07"), ("2002-x13", "2005-06"), ("2002-x14", "2005-05"), - ("kh", "161"), ("161-kh", "161"), ("204-steinBH", "204"), @@ -605,13 +608,12 @@ def GetCaveLookup(): ("balkon", "264"), ("fgh", "290"), ("gsh", "291"), - ("homecoming", "2018-dm-07"), ("heimkommen", "2018-dm-07"), ("Heimkehr", "2018-dm-07"), ("99ob02", "1999-ob-02"), ] - + for i in aliases: if i[1] in Gcavelookup: if i[0] in Gcavelookup: @@ -623,23 +625,23 @@ def GetCaveLookup(): Gcavelookup[i[0]] = Gcavelookup[i[1]] else: message = f" * Coding or cave existence mistake, cave for id '{i[1]}' does not exist. Expecting to set alias '{i[0]}' to it" - #print(message) - DataIssue.objects.create(parser='aliases', message=message) - + # print(message) + DataIssue.objects.create(parser="aliases", message=message) + addmore = {} for id in Gcavelookup: - addmore[id.replace("-","_")] = Gcavelookup[id] - addmore[id.replace("_","-")] = Gcavelookup[id] + addmore[id.replace("-", "_")] = Gcavelookup[id] + addmore[id.replace("_", "-")] = Gcavelookup[id] addmore[id.upper()] = Gcavelookup[id] Gcavelookup = {**addmore, **Gcavelookup} - addmore ={} - + addmore = {} + for c in Gcave_count: if Gcave_count[c] > 1: message = f" ** Duplicate cave id count={Gcave_count[c]} id:'{Gcavelookup[c]}' cave __str__:'{c}'" print(message) - DataIssue.objects.create(parser='aliases', message=message) + DataIssue.objects.create(parser="aliases", message=message) # logdataissues[Gcavelookup[c]]=message # pending troggle-wide issues logging system return Gcavelookup |