2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00:00

Rename lookupAttribs and nonLookupAttribs + add slug to Person

This commit is contained in:
Philip Sargent 2023-10-01 12:42:47 +03:00
parent fd94909ee7
commit 16d3ee9f92
7 changed files with 42 additions and 39 deletions

View File

@ -39,13 +39,13 @@ class ImportTest(TestCase):
def setUpTestData(cls): def setUpTestData(cls):
def make_person(firstname, lastname, nickname=False, vfho=False, guest=False): def make_person(firstname, lastname, nickname=False, vfho=False, guest=False):
fullname = f"{firstname} {lastname}" fullname = f"{firstname} {lastname}"
lookupAttribs = {"first_name": firstname, "last_name": (lastname or "")} coUniqueAttribs = {"first_name": firstname, "last_name": (lastname or "")}
nonLookupAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname} otherAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname}
person = Person.objects.create(**nonLookupAttribs, **lookupAttribs) person = Person.objects.create(**otherAttribs, **coUniqueAttribs)
lookupAttribs = {"person": person, "expedition": cls.test_expo} coUniqueAttribs = {"person": person, "expedition": cls.test_expo}
nonLookupAttribs = {"is_guest": guest} otherAttribs = {"is_guest": guest}
pe = PersonExpedition.objects.create(**nonLookupAttribs, **lookupAttribs) pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs)
return person return person
@ -58,9 +58,9 @@ class ImportTest(TestCase):
if frontmatter_file.is_file(): if frontmatter_file.is_file():
frontmatter_file.unlink() # delete if it exists frontmatter_file.unlink() # delete if it exists
lookupAttribs = {"year": TEST_YEAR} coUniqueAttribs = {"year": TEST_YEAR}
nonLookupAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"} otherAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"}
cls.test_expo = Expedition.objects.create(**nonLookupAttribs, **lookupAttribs) cls.test_expo = Expedition.objects.create(**otherAttribs, **coUniqueAttribs)
fred = make_person("Fred", "Smartarse", nickname="freddy") fred = make_person("Fred", "Smartarse", nickname="freddy")
phil = make_person("Phil", "Tosser", nickname="tosspot") phil = make_person("Phil", "Tosser", nickname="tosspot")

View File

@ -82,6 +82,8 @@ class Person(TroggleModel):
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)
fullname = models.CharField(max_length=200) fullname = models.CharField(max_length=200)
nickname = models.CharField(max_length=200) nickname = models.CharField(max_length=200)
slug = models.SlugField(max_length=50, unique=True)
is_vfho = models.BooleanField( is_vfho = models.BooleanField(
help_text="VFHO is the Vereines für Höhlenkunde in Obersteier, a nearby Austrian caving club.", help_text="VFHO is the Vereines für Höhlenkunde in Obersteier, a nearby Austrian caving club.",
default=False, default=False,

View File

@ -276,8 +276,8 @@ def writetrogglefile(filepath, filecontent):
# not catching and re-raising any exceptions yet, inc. the stderr etc.,. We should do that. # not catching and re-raising any exceptions yet, inc. the stderr etc.,. We should do that.
def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): def save_carefully(objectType, coUniqueAttribs={}, otherAttribs={}):
"""Looks up instance using lookupAttribs and carries out the following: """Looks up instance using coUniqueAttribs and carries out the following:
-if instance does not exist in DB: add instance to DB, return (new instance, True) -if instance does not exist in DB: add instance to DB, return (new instance, True)
-if instance exists in DB and was modified using Troggle: do nothing, return (existing instance, False) -if instance exists in DB and was modified using Troggle: do nothing, return (existing instance, False)
-if instance exists in DB and was not modified using Troggle: overwrite instance, return (instance, False) -if instance exists in DB and was not modified using Troggle: overwrite instance, return (instance, False)
@ -293,15 +293,15 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}):
""" """
try: try:
instance, created = objectType.objects.get_or_create(defaults=nonLookupAttribs, **lookupAttribs) instance, created = objectType.objects.get_or_create(defaults=otherAttribs, **coUniqueAttribs)
except: except:
print(" !! - FAIL in SAVE CAREFULLY ===================", objectType) print(" !! - FAIL in SAVE CAREFULLY ===================", objectType)
print(" !! - -- objects.get_or_create()") print(" !! - -- objects.get_or_create()")
print(f" !! - lookupAttribs:{lookupAttribs}\n !! - nonLookupAttribs:{nonLookupAttribs}") print(f" !! - coUniqueAttribs:{coUniqueAttribs}\n !! - otherAttribs:{otherAttribs}")
raise raise
if not created and not instance.new_since_parsing: if not created and not instance.new_since_parsing:
for k, v in list( for k, v in list(
nonLookupAttribs.items() otherAttribs.items()
): # overwrite the existing attributes from the logbook text (except date and title) ): # overwrite the existing attributes from the logbook text (except date and title)
setattr(instance, k, v) setattr(instance, k, v)
try: try:
@ -309,7 +309,7 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}):
except: except:
print(" !! - SAVE CAREFULLY ===================", objectType) print(" !! - SAVE CAREFULLY ===================", objectType)
print(" !! - -- instance.save()") print(" !! - -- instance.save()")
print(f" !! - lookupAttribs:{lookupAttribs}\n !! - nonLookupAttribs:{nonLookupAttribs}") print(f" !! - coUniqueAttribs:{coUniqueAttribs}\n !! - otherAttribs:{otherAttribs}")
raise raise
try: try:
str(instance) str(instance)

View File

@ -77,7 +77,7 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t
DataIssue.objects.create(parser="logbooks", message=message) DataIssue.objects.create(parser="logbooks", message=message)
slug = slug + "_" + unique_slug(text,2) slug = slug + "_" + unique_slug(text,2)
nonLookupAttribs = { otherAttribs = {
"place": place, "place": place,
"text": text, "text": text,
"expedition": expedition, "expedition": expedition,
@ -86,9 +86,9 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t
"title": f"{place} - {title}", "title": f"{place} - {title}",
# "other_people": others # "other_people": others
} }
lookupAttribs = {"slug": slug, "date": date } coUniqueAttribs = {"slug": slug, "date": date }
lbo = LogbookEntry.objects.create(**nonLookupAttribs, **lookupAttribs) lbo = LogbookEntry.objects.create(**otherAttribs, **coUniqueAttribs)
pt_list = [] pt_list = []
# These entities have to be PersonExpedition objects # These entities have to be PersonExpedition objects
@ -116,9 +116,9 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t
print(message) print(message)
DataIssue.objects.create(parser="logbooks", message=message) DataIssue.objects.create(parser="logbooks", message=message)
else: else:
lookupAttribs = {"personexpedition": personyear, "nickname_used": name, "logbook_entry": lbo} # lbo is primary key coUniqueAttribs = {"personexpedition": personyear, "nickname_used": name, "logbook_entry": lbo} # lbo is primary key
nonLookupAttribs = {"time_underground": tu, "is_logbook_entry_author": (name==author)} otherAttribs = {"time_underground": tu, "is_logbook_entry_author": (name==author)}
pt_list.append(PersonLogEntry(**nonLookupAttribs, **lookupAttribs)) pt_list.append(PersonLogEntry(**otherAttribs, **coUniqueAttribs))
except: except:
# This should not happen. We do not raise exceptions in that function # This should not happen. We do not raise exceptions in that function

View File

@ -173,7 +173,7 @@ def parse_KH_QMs(kh, inputFile, ticked):
resolution_station_name = station_name.replace("<a href=\"","<a href=\"/1623/161/") resolution_station_name = station_name.replace("<a href=\"","<a href=\"/1623/161/")
else: else:
nearest_station_name = station_name.replace("<a href=\"","<a href=\"/1623/161/") nearest_station_name = station_name.replace("<a href=\"","<a href=\"/1623/161/")
lookupAttribs = { coUniqueAttribs = {
#'found_by':placeholder, #'found_by':placeholder,
"blockname": "", "blockname": "",
"expoyear": year, "expoyear": year,
@ -181,7 +181,7 @@ def parse_KH_QMs(kh, inputFile, ticked):
"cave": kh, "cave": kh,
"grade": res["grade"], "grade": res["grade"],
} }
nonLookupAttribs = { otherAttribs = {
"ticked": ticked, "ticked": ticked,
"page_ref": "", "page_ref": "",
"completion_description": completion, "completion_description": completion,
@ -190,7 +190,7 @@ def parse_KH_QMs(kh, inputFile, ticked):
"location_description": res["location_description"].replace("<a href=\"","<a href=\"/1623/161/"), "location_description": res["location_description"].replace("<a href=\"","<a href=\"/1623/161/"),
} }
# Create new. We know it doesn't exist as we deleted evrything when we started. # Create new. We know it doesn't exist as we deleted evrything when we started.
instance = QM.objects.create(**nonLookupAttribs, **lookupAttribs) instance = QM.objects.create(**otherAttribs, **coUniqueAttribs)
nqms += 1 nqms += 1
else: else:
if dataline.startswith("<dt><a href"): if dataline.startswith("<dt><a href"):

View File

@ -262,7 +262,7 @@ def store_entry_into_database(date, place, tripcave, title, text, trippersons, a
# if guests: # if guests:
# print(f" {date} - {guests}") # print(f" {date} - {guests}")
nonLookupAttribs = { otherAttribs = {
"place": place, "place": place,
"other_people": other_people, # *Ol's Mum, foreigners.. "other_people": other_people, # *Ol's Mum, foreigners..
"text": text, "text": text,
@ -270,20 +270,20 @@ def store_entry_into_database(date, place, tripcave, title, text, trippersons, a
"time_underground": logtime_underground, "time_underground": logtime_underground,
"cave_slug": str(tripcave), "cave_slug": str(tripcave),
} }
lookupAttribs = {"slug": tid, "date": date, "title": title} coUniqueAttribs = {"slug": tid, "date": date, "title": title}
if LogbookEntry.objects.filter(slug=tid).exists(): if LogbookEntry.objects.filter(slug=tid).exists():
# oops. Our code should already have ensured this is unique. # oops. Our code should already have ensured this is unique.
message = " ! - DUPLICATE SLUG for logbook entry " + tripdate + " - " + slug message = " ! - DUPLICATE SLUG for logbook entry " + tripdate + " - " + slug
DataIssue.objects.create(parser="logbooks", message=message) DataIssue.objects.create(parser="logbooks", message=message)
slug = slug + "_" + unique_slug(text,2) slug = slug + "_" + unique_slug(text,2)
lbo = LogbookEntry.objects.create(**nonLookupAttribs, **lookupAttribs) lbo = LogbookEntry.objects.create(**otherAttribs, **coUniqueAttribs)
pt_list = [] pt_list = []
for tripperson, nickname_used, time_underground in trippersons: for tripperson, nickname_used, time_underground in trippersons:
lookupAttribs = {"personexpedition": tripperson, "nickname_used": nickname_used, "logbook_entry": lbo} # lbo is primary key coUniqueAttribs = {"personexpedition": tripperson, "nickname_used": nickname_used, "logbook_entry": lbo} # lbo is primary key
nonLookupAttribs = {"time_underground": time_underground, "is_logbook_entry_author": (tripperson == author)} otherAttribs = {"time_underground": time_underground, "is_logbook_entry_author": (tripperson == author)}
pt_list.append(PersonLogEntry(**nonLookupAttribs, **lookupAttribs)) pt_list.append(PersonLogEntry(**otherAttribs, **coUniqueAttribs))
PersonLogEntry.objects.bulk_create(pt_list) PersonLogEntry.objects.bulk_create(pt_list)
def parser_date(tripdate, year): def parser_date(tripdate, year):

View File

@ -78,15 +78,16 @@ def load_people_expos():
if nexpos <= 0: if nexpos <= 0:
print(" - Creating expeditions") print(" - Creating expeditions")
for year in years: for year in years:
lookupAttribs = {"year": year} coUniqueAttribs = {"year": year}
nonLookupAttribs = {"name": f"CUCC expo {year}"} otherAttribs = {"name": f"CUCC expo {year}"}
e = Expedition.objects.create(**nonLookupAttribs, **lookupAttribs) e = Expedition.objects.create(**otherAttribs, **coUniqueAttribs)
print(" - Loading personexpeditions") print(" - Loading personexpeditions")
for personline in personreader: for personline in personreader:
name = personline[header["Name"]] name = personline[header["Name"]]
name = re.sub(r"<.*?>", "", name) name = re.sub(r"<.*?>", "", name)
slug = slugify(name)
firstname = "" firstname = ""
nick = "" nick = ""
@ -111,9 +112,9 @@ def load_people_expos():
else: else:
vfho = True vfho = True
lookupAttribs = {"first_name": firstname, "last_name": (lastname or "")} coUniqueAttribs = {"first_name": firstname, "last_name": (lastname or "")}
nonLookupAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nick} otherAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nick}
person = Person.objects.create(**nonLookupAttribs, **lookupAttribs) person = Person.objects.create(**otherAttribs, **coUniqueAttribs)
parse_blurb(personline=personline, header=header, person=person) parse_blurb(personline=personline, header=header, person=person)
@ -121,9 +122,9 @@ def load_people_expos():
for year, attended in list(zip(headers, personline))[5:]: for year, attended in list(zip(headers, personline))[5:]:
expedition = Expedition.objects.get(year=year) expedition = Expedition.objects.get(year=year)
if attended == "1" or attended == "-1": if attended == "1" or attended == "-1":
lookupAttribs = {"person": person, "expedition": expedition} coUniqueAttribs = {"person": person, "expedition": expedition}
nonLookupAttribs = {"is_guest": (personline[header["Guest"]] == "1")} otherAttribs = {"is_guest": (personline[header["Guest"]] == "1")}
pe = PersonExpedition.objects.create(**nonLookupAttribs, **lookupAttribs) pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs)
print("", flush=True) print("", flush=True)