2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-21 14:51:51 +00:00

Remove CaveSLug as a Class. Now each Cave has only one slug for sure.

This commit is contained in:
Philip Sargent 2024-07-12 17:18:05 +02:00
parent a4783d2332
commit 24c5ba9711
8 changed files with 17 additions and 63 deletions

View File

@ -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):

View File

@ -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)

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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
}

View File

@ -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)

View File

@ -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():