From 24c5ba97111428f0ee43d5633b278dc1a0c055c0 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 12 Jul 2024 17:18:05 +0200 Subject: [PATCH] Remove CaveSLug as a Class. Now each Cave has only one slug for sure. --- core/TESTS/test_imports.py | 2 +- core/admin.py | 3 +-- core/models/caves.py | 4 +--- core/models/logbooks.py | 23 ++--------------------- core/views/caves.py | 10 +++++++--- core/views/uploads.py | 4 ++-- parsers/caves.py | 32 ++------------------------------ parsers/logbooks.py | 2 +- 8 files changed, 17 insertions(+), 63 deletions(-) diff --git a/core/TESTS/test_imports.py b/core/TESTS/test_imports.py index c8bb1b4..f5127c3 100644 --- a/core/TESTS/test_imports.py +++ b/core/TESTS/test_imports.py @@ -72,7 +72,7 @@ class SimpleTest(SimpleTestCase): from django.utils.timezone import get_current_timezone, make_aware from parsers.people import GetPersonExpeditionNameLookup - from troggle.core.models.logbooks import CaveSlug, QM, LogbookEntry, PersonLogEntry + from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry from troggle.core.models.troggle import DataIssue, Expedition def test_import_core_views_caves(self): diff --git a/core/admin.py b/core/admin.py index 880d163..fa04b12 100644 --- a/core/admin.py +++ b/core/admin.py @@ -3,7 +3,7 @@ from django.core import serializers from django.http import HttpResponse from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance -from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry, CaveSlug +from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry from troggle.core.models.survex import ( DrawingFile, SingleScan, @@ -138,7 +138,6 @@ admin.site.register(Cave, CaveAdmin) #admin.site.register(Area) admin.site.register(CaveAndEntrance) admin.site.register(Entrance, EntranceAdmin) -admin.site.register(CaveSlug) admin.site.register(SurvexBlock, SurvexBlockAdmin) admin.site.register(DrawingFile, DrawingFileAdmin) admin.site.register(Expedition) diff --git a/core/models/caves.py b/core/models/caves.py index 4cf0512..b63df2e 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -58,9 +58,7 @@ class CaveAndEntrance(models.Model): def __str__(self): return str(self.cave) + str(self.entranceletter) - -# class CaveSlug(models.Model): -# moved to models/logbooks.py to avoid cyclic import problem. No I don't know why either. + def get_cave_leniently(caveid): try: diff --git a/core/models/logbooks.py b/core/models/logbooks.py index 1ffe55d..d8b4d93 100644 --- a/core/models/logbooks.py +++ b/core/models/logbooks.py @@ -10,34 +10,19 @@ from django.template import loader import settings from troggle.core.models.troggle import Expedition, TroggleModel + """The model declarations LogBookEntry, PersonLogEntry, QM """ todo = """ -- Can we rewrite things to eliminate the CaveSlug and objects? No - Surely foreign keys work fine ?! No Foreign keys do not allow for there being multiple ways to refer to a cave, eg 1623-1999-03 aka 1623-204 Having slugs allows for much more loose coupling to caves, which removes alot of the need to reset the database, which interupts work flow. It also means we do not have to be creating temporary cave objects in the database, where we do not have the underlying file in cave_data. - - To Do move Cave Slug back to troggle.core.models """ -class CaveSlug(models.Model): - """Moved here to avoid nasty cyclic import error - CASCADE means that if the Cave is deleted, this is too - """ - - cave = models.ForeignKey("Cave", on_delete=models.CASCADE) - slug = models.SlugField(max_length=50, unique=True) - primary = models.BooleanField(default=False) - - def __str__(self): - return f"{self.slug}: {self.cave}" - class LogbookEntry(TroggleModel): """Single parsed entry from Logbook @@ -48,7 +33,7 @@ class LogbookEntry(TroggleModel): ) expedition = models.ForeignKey(Expedition, blank=True, null=True, on_delete=models.CASCADE) title = models.CharField(max_length=200) - cave_slug = models.SlugField(max_length=50, blank=True, null=True) + cave = models.ForeignKey("Cave", blank=True, null=True, on_delete=models.SET_NULL) place = models.CharField( max_length=100, blank=True, null=True, help_text="Only use this if you haven't chosen a cave" ) @@ -62,10 +47,6 @@ class LogbookEntry(TroggleModel): # several PersonLogEntrys point in to this object ordering = ("-date",) - def cave(self): # Why didn't he just make this a foreign key to Cave ? - c = CaveSlug.objects.get(slug=self.cave_slug, primary=True).cave - return c - def isLogbookEntry(self): # Function used in templates return True diff --git a/core/views/caves.py b/core/views/caves.py index d7699d0..795bb18 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -16,7 +16,7 @@ from django.urls import NoReverseMatch, reverse import troggle.settings as settings from troggle.core.forms import CaveAndEntranceFormSet, CaveForm, EntranceForm, EntranceLetterForm from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance, GetCaveLookup, get_cave_leniently -from troggle.core.models.logbooks import CaveSlug, QM +from troggle.core.models.logbooks import QM from troggle.core.models.wallets import Wallet from troggle.core.utils import write_and_commit from troggle.core.utils import current_expo @@ -51,9 +51,9 @@ todo = """ # """ # return redirect("/caves") + def get_cave_from_slug(caveslug): - """This is the old way of doing it, usingt eh CaveSlug intermediate object which does - the many:many relationship between slugs and caves - whcih we don't do (and never did in practice) + """Needs refactoring """ caves = [] @@ -74,10 +74,14 @@ def get_cave_from_slug(caveslug): if len(caves) > 1: print(f"get_cave_from_slug(): {caveslug} More than 1 \n{caves}") + return None if len(caves) <1: print(f"get_cave_from_slug(): {caveslug} Nowt found..") + return None cave = caves[0] print(f"get_cave_from_slug(): {caveslug} FOUND {cave}") + return cave + try: cave_zero = Cave.objects.get(caveslug__slug=caveslug) diff --git a/core/views/uploads.py b/core/views/uploads.py index 3d4e987..403a30d 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -69,7 +69,7 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t year = slug[0:4] expedition = Expedition.objects.get(year=year) cave = GetCaveLookup().get(place.lower()) - # print(f"{place} {cave=}") + # print(f"store_edited_entry_into_database(): {place=} {cave=}") if LogbookEntry.objects.filter(slug=slug).exists(): # oops. @@ -82,7 +82,7 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t "text": text, "expedition": expedition, "time_underground": tu, - "cave_slug": str(cave), + "cave": cave, "title": f"{place} - {title}", # "other_people": others } diff --git a/parsers/caves.py b/parsers/caves.py index c1f8094..0a5f2b2 100644 --- a/parsers/caves.py +++ b/parsers/caves.py @@ -9,7 +9,6 @@ from django.conf import settings from django.db import transaction from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance, GetCaveLookup -from troggle.core.models.logbooks import CaveSlug from troggle.core.models.survex import SurvexStation from troggle.core.models.troggle import DataIssue from troggle.settings import CAVEDESCRIPTIONS, ENTRANCEDESCRIPTIONS, EXPOWEB, SURVEX_DATA @@ -235,12 +234,6 @@ def do_ARGE_cave(slug, caveid, areacode, svxid): cave.save() - try: # Now create a cave slug ID - CaveSlug.objects.update_or_create(cave=cave, slug=slug, primary=False) - except: - message = f" ! {slug:11s} ARGE CaveSLUG create failure {caveid=} {url=} {areacode=} {svxid=}" - DataIssue.objects.create(parser="caves", message=message) - print(message) else: message = f" ! {slug:11s} ARGE cave create failure {caveid=} {url=} {areacode=} {svxid=}" DataIssue.objects.create(parser="caves", message=message) @@ -368,12 +361,6 @@ def do_pending_cave(slug, caveid, url, areacode, msg=None): DataIssue.objects.create(parser="caves", message=message, url=url) print(message) - try: # Now create a cave slug ID - CaveSlug.objects.update_or_create(cave=cave, slug=slug, primary=False) - except: - message = f" ! {slug:11s} PENDING CaveSLUG {slug} create failure - {msg}" - DataIssue.objects.create(parser="caves", message=message) - print(message) else: message = f" ! {slug:11s} PENDING cave create failure - {msg}" DataIssue.objects.create(parser="caves", message=message) @@ -609,7 +596,7 @@ def make_cave(slug): except: print(f" ! FAILED to get only one CAVE in db when updating using: {filename} or not found.") - kaves = Cave.objects.all().filter(filename=filename) # replace with slug when CaveSlug tidied up + kaves = Cave.objects.all().filter(filename=filename) # assumes filename unique, eeugh. for k in kaves: message = " ! - DUPLICATES in db. kataster:" + str(k.kataster_number) + ", slug:" + str(k.slug()) DataIssue.objects.create(parser="caves", message=message, url=context) @@ -723,22 +710,7 @@ def read_cave(filename, mvf=None, cave=None): Pending destruction of this whole concept and Class CaveSlug What is Class CaveSlug for? """ - #return - primary = True # this sets the first thing we find to be primary=True and all the others =False - for slug in slugs: - if slug in caves_xslug: - cs = caves_xslug[slug] - else: - try: - cs = CaveSlug.objects.update_or_create(cave=cave, slug=slug, primary=primary) - caves_xslug[slug] = cs - except Exception as ex: - #raise - # This fails to do an update! It just crashes.. to be fixed - message = f" ! CaveSlug update/create failure : {slug}, skipping cave_data file {slug} with exception\nException: {ex.__class__}" - DataIssue.objects.create(parser="caves", message=message, url=context) - print(message) - primary = False + return def check_directory(areacode, caveid, url, cave): dir = Path(settings.EXPOWEB, areacode, caveid) diff --git a/parsers/logbooks.py b/parsers/logbooks.py index b122250..d5580a1 100644 --- a/parsers/logbooks.py +++ b/parsers/logbooks.py @@ -269,7 +269,7 @@ def store_entry_into_database(date, place, tripcave, title, text, trippersons, a "text": text, "expedition": expedition, "time_underground": logtime_underground, - "cave_slug": str(tripcave), + "cave": tripcave, } coUniqueAttribs = {"slug": tid, "date": date, "title": title} if LogbookEntry.objects.filter(slug=tid).exists():