2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-04-02 10:21:01 +01:00

wallets & caves now many:many

This commit is contained in:
2023-10-21 16:22:20 +03:00
parent 24029be7d3
commit e7a0c57330
11 changed files with 112 additions and 67 deletions

View File

@@ -88,7 +88,7 @@ class Cave(TroggleModel):
url = models.CharField(max_length=300, blank=True, null=True, unique = True)
class Meta:
# we do not enforce uniqueness at the db level as that causes confusing errors for users.
# we do not enforce uniqueness at the db level as that causes confusing errors for newbie maintainers
# unique_together = (("area", "kataster_number"), ("area", "unofficial_number"))
ordering = ("kataster_code", "unofficial_number")

View File

@@ -10,6 +10,8 @@ from django.conf import settings
from django.db import models
from django.urls import reverse
from troggle.core.views.caves import get_cave_leniently
# from troggle.core.models.survex import SurvexBlock
# from troggle.core.models.troggle import DataIssue # circular import. Hmm
@@ -74,12 +76,15 @@ archaic_wallets = [
class Wallet(models.Model):
"""We do not keep the JSON values in the database, we query them afresh each time,
but we may change this if we need to do a Django query on e.g. personame
ManyToMany field uses modern Django: a hidden Class, unlike CaveAndEntrances which is explict and visible.
"""
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)
caves = models.ManyToManyField("Cave", related_name="wallets")
class Meta:
ordering = ("walletname",)
@@ -139,7 +144,31 @@ class Wallet(models.Model):
from troggle.core.models.troggle import DataIssue
DataIssue.objects.update_or_create(parser="wallets", message=message, url=wurl)
return waldata
def allcaves(self):
"""Reads all the JSON data just to get the JSON date."""
if not (jsondata := self.get_json()): # WALRUS
print(f"Failed to read JSON file {self}")
return None
cavelist = jsondata["cave"]
if type(cavelist) is list:
for i in cavelist:
if i != "":
i = i.replace("/", "-")
caveobject = get_cave_leniently(i)
self.caves.add(caveobject) # new many-to-many field
else:
# either single cave or the square barckets have been removed and it s a singoe string
ids = cavelist.split(",")
for i in ids:
j = i.replace("'","").replace("/", "-").strip('[] "')
if i != "":
try:
caveobject = get_cave_leniently(j) # may fail if garbage value ,e.g. space, in wallet data
self.caves.add(caveobject)
except:
print(f"FAIL adding cave to wallet.caves '{j}'")
pass
def year(self):
"""This gets the year syntactically without opening and reading the JSON"""
if len(self.walletname) < 5:
@@ -180,6 +209,7 @@ class Wallet(models.Model):
self.save()
return self.walletdate
# for gods sake redo this, it parse JSON twice every time..
def people(self):
if not self.get_json():
return None

View File

@@ -40,6 +40,20 @@ todo = """
https://zerotobyte.com/how-to-use-django-select-related-and-prefetch-related/
"""
def get_cave_leniently(caveid):
try:
c = getCave(caveid)
if c:
return c
except:
# print(f"get_cave_leniently FAIL {caveid}")
try:
c = getCave("1623-"+caveid)
if c:
return c
except:
return None
def getCaves(cave_id):
"""Only gets called if a call to getCave() raises a MultipleObjects exception
@@ -59,7 +73,8 @@ def getCaves(cave_id):
def getCave(cave_id):
"""Returns a cave object when given a cave name or number. It is used by views including cavehref, ent, and qm.
"""Returns a cave object when given a cave name or number.
It is used by views including cavehref, ent, wallets and qm.
TO DO: search GCavelookup first, which should raise a MultpleObjectsReturned exception if there
are duplicates"""

View File

@@ -52,20 +52,15 @@ def populatewallet(w):
def caveifywallet(w):
"""Gets the cave from the list of survex files,
only selects one of them though. Only used for display.
FIX THIS to display many caves
"""
"""Gets the caves from the list of survex files,
"""
# print(f' - Caveify {w=}')
blocknames = []
blocks = SurvexBlock.objects.filter(scanswallet=w)
for b in blocks:
# NB b.cave is not populated by parser. Use b.survexfile.cave instead, or we could parse b.survexpath
if b.survexfile.cave:
w.caveobj = (
b.survexfile.cave
) # just gets the last one, randomly. SHould make this a list or many:many ideally
w.cave = w.caveobj
w.caves.add(b.survexfile.cave)
if b.name:
blocknames.append(b.name)
@@ -120,11 +115,10 @@ def is_cave(wallet, id):
if not id:
return False
Gcavelookup = GetCaveLookup()
id = id.strip("' []'")
if id in Gcavelookup:
return True
else:
# Historic wallets used just 2 or 3 digits and were all 1623 area. So, just for these wallets,
# Historic wallets were all 1623 area. So, just for wallets,
# assume it is 1623-xxx
if f"1623-{id}" in Gcavelookup:
print(f" - Should modify wallet {wallet} to use 1623- prefix for cave <{id}>")
@@ -145,26 +139,26 @@ def fillblankothers(w):
Gcavelookup = GetCaveLookup()
caveifywallet(w)
wcaveid = w.cave()
if not wcaveid or wcaveid == "":
caveifywallet(w)
else:
if type(wcaveid) == list:
for i in wcaveid:
i = i.strip("' []'")
i = i.strip("' []\"")
if is_cave(w,i):
w.caveobj = Gcavelookup[i] # just sets it to the last one found. nasty. bug waiting to happen
w.caves.add(Gcavelookup[i])
elif wcaveid.find(',') != -1:
# it's a list of cave ids as a string
ids = wcaveid.split(',')
for i in ids:
i = i.strip("' []'")
i = i.strip("' []\"")
if is_cave(w,i):
w.caveobj = Gcavelookup[i] # just sets it to the last one found. nasty. bug waiting to happen
w.caves.add(Gcavelookup[i])
else:
if is_cave(w,wcaveid):
w.caveobj = Gcavelookup[wcaveid.strip("' []'")]
w.caves.add(Gcavelookup[i])
def fixsurvextick(w, ticks):
ticks["S"] = w.fixsurvextick(ticks["S"])

View File

@@ -23,7 +23,7 @@ from troggle.core.models.troggle import DataIssue, Expedition
from troggle.core.models.wallets import Wallet, YEAR_RANGE, make_valid_date
from troggle.core.views.auth import login_required_if_public
from troggle.core.views.caves import getCave
from troggle.core.views.caves import getCave, get_cave_leniently
from troggle.core.views.scans import caveifywallet, oldwallet
from troggle.core.views.uploads import FilesForm
@@ -108,7 +108,8 @@ xlate = {
"survex": "survex file",
}
def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl):
"""Taken from old script wallets.py and edited to make more comprehensible
Loads the survex files names and processes all complaints
@@ -231,20 +232,29 @@ def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl):
)
# Find the cave, if it exists
# Just for wallets, we are lenient about whether the 1623- prefix has been written down.
if waldata["cave"]:
caveobject = None
try:
caveid = waldata["cave"]
if type(caveid) is list:
for i in caveid:
i = i.replace("/", "-")
caveobject = getCave(i) # only the last one gets recorded.. ouch.
caveobject = get_cave_leniently(i)
w.caves.add(caveobject) # new many-to-many field
#print(w.caves)
else:
caveid = caveid # ?urk? why?
try:
caveobject = getCave(caveid) # may fail if garbage value ,e.g. space, in wallet data
except:
caveobject = None
print(f'getCave for id "{waldata["cave"]}" {caveobject}')
# either single cave or the square barckets have been removed
ids = caveid.split(",")
for i in ids:
j = i.replace("'","").strip('[] "')
#print(j)
try:
caveobject = get_cave_leniently(j) # may fail if garbage value ,e.g. space, in wallet data
w.caves.add(caveobject)
except:
pass
print(f'get_cave_leniently from "{waldata["cave"]}" => {caveobject}')
# if not caveobject.url == waldata["description url"]:
# complaints.append(f'The URL of cave description \"{waldata["description url"]}\" does not match the one on record for this cave which is: "{caveobject.url}". If the wallet is not for a cave, put a useful URL here.')
except Cave.MultipleObjectsReturned: