summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2023-10-23 22:54:46 +0300
committerPhilip Sargent <philip.sargent@gmail.com>2023-10-23 22:54:46 +0300
commit33477f2b40a5e88379d2d05cfc5356afdbb06269 (patch)
tree62d029458a69991bfe4de4a6a27d049696f28b39 /core
parentb522899216695187f70f8c35aef6c962124d3308 (diff)
downloadtroggle-33477f2b40a5e88379d2d05cfc5356afdbb06269.tar.gz
troggle-33477f2b40a5e88379d2d05cfc5356afdbb06269.tar.bz2
troggle-33477f2b40a5e88379d2d05cfc5356afdbb06269.zip
refactoring duplicate code
Diffstat (limited to 'core')
-rw-r--r--core/models/wallets.py79
-rw-r--r--core/views/scans.py10
2 files changed, 59 insertions, 30 deletions
diff --git a/core/models/wallets.py b/core/models/wallets.py
index 4fa5ef2..0335524 100644
--- a/core/models/wallets.py
+++ b/core/models/wallets.py
@@ -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:
diff --git a/core/views/scans.py b/core/views/scans.py
index 2f7c843..0143bea 100644
--- a/core/views/scans.py
+++ b/core/views/scans.py
@@ -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