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

refactoring duplicate code

This commit is contained in:
2023-10-23 22:54:46 +03:00
parent b522899216
commit 33477f2b40
4 changed files with 65 additions and 39 deletions

View File

@@ -105,6 +105,29 @@ class Wallet(models.Model):
class Meta:
ordering = ("walletname",)
@staticmethod
def input_to_list(stuff):
"""With wallets we often have to deal with either a list object (from valid JSON parsing)
or a string which may or may not also be a list, but munged by user eror on a form etc.
This function returns a list, either the JSON list, or a list with a single object in it,
or a list of strings. It silently absorbs empty strings an consumes odd quotes and square
brackets.
Always return a list, even if it is an empty list"""
if type(stuff) == list:
newstuff = []
for o in stuff:
if o: # not an empty string, None
newstuff.append(o)
return newstuff
if type(stuff) == str:
newstuff = stuff.split(",")
for s in newstuff:
s = s.strip('[] ').replace("'","").replace('"','').replace("/", "-").replace(" ", "_").strip('[] ')
return newstuff
if stuff:
return [stuff] # single object, not a string, but now in a list.
def get_absolute_url(self):
# we do not use URL_ROOT any more.
return reverse("singlewallet", kwargs={"path": re.sub("#", "%23", self.walletname)})
@@ -114,6 +137,8 @@ class Wallet(models.Model):
Do it every time it is queried, to be sure the result is fresh.. well, no.
Do it every time we have a new python instance.
Reads JSON date and sets w.walletdate
import DataIssue locally to prevent import cycle problem"""
if hasattr(self, "JSONdata"):
@@ -170,8 +195,7 @@ class Wallet(models.Model):
if not (waldata := self.get_json()): # WALRUS
return None
if waldata["survex file"]:
if not type(waldata["survex file"]) == list: # a string also is a sequence type, so do it this way
waldata["survex file"] = [waldata["survex file"]]
waldata["survex file"] = Wallet.input_to_list(waldata["survex file"])
for sx in waldata["survex file"]:
# this logic appears in several places, inc get_ticks(). and wallets_edit.py Refactor.
if sx != "":
@@ -182,28 +206,39 @@ class Wallet(models.Model):
DataIssue.objects.update_or_create(parser="wallets", message=message, url=wurl)
def allcaves(self):
"""Reads all the JSON data just to get the JSON date."""
"""Called when parsing importing all data. Called on all new wallets, but before
the survex files are parsed"""
if not (jsondata := self.get_json()): # WALRUS
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
#cavelist = jsondata["cave"]
cavelist = Wallet.input_to_list(jsondata["cave"])
for i in cavelist:
try:
caveobject = get_cave_leniently(i)
if caveobject:
self.caves.add(caveobject)
except:
print(f"FAIL adding cave to wallet.caves '{i}'")
pass
# 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:

View File

@@ -174,23 +174,17 @@ def parse_name_list(w):
crew = GetPersonExpeditionNameLookup(expo)
for n in namelist:
# if n.lower().startswith("lydia"):
# print(f"{w} {n=} ")
# for x in crew:
# if x.lower()==n.lower():
# print(f"{w} {n=} {x=}")
if n.lower() in crew:
peeps.add(crew[n.lower()].person)
else:
if n.startswith("*"): #ignore people flagged as guests or not-expo anyway, such as ARGE
if check := n.startswith("*"): #ignore people flagged as guests or not-expo anyway, such as ARGE
continue
nobod = n.lower()
if nobod == "unknown" or nobod == "nobody" or nobod == " " or nobod == "":
continue
else:
wurl = f"/walletedit/{w.walletname.replace('#',':')}"
message = f"{w} name '{n.lower()}' NOT found in GetPersonExpeditionNameLookup({w.year()}) ?!"
message = f"{w} name '{n.lower()}' NOT found in GetPersonExpeditionNameLookup({w.year()}) n.startswith* = {check} ?!"
print(message)
DataIssue.objects.update_or_create(parser="wallets", message=message, url=wurl)
return peeps