diff options
-rw-r--r-- | core/TESTS/test_parsers.py | 18 | ||||
-rw-r--r-- | core/models/troggle.py | 2 | ||||
-rw-r--r-- | core/utils.py | 12 | ||||
-rw-r--r-- | core/views/uploads.py | 12 | ||||
-rw-r--r-- | parsers/QMs.py | 6 | ||||
-rw-r--r-- | parsers/logbooks.py | 12 | ||||
-rw-r--r-- | parsers/people.py | 19 |
7 files changed, 42 insertions, 39 deletions
diff --git a/core/TESTS/test_parsers.py b/core/TESTS/test_parsers.py index fd87af3..21f79c7 100644 --- a/core/TESTS/test_parsers.py +++ b/core/TESTS/test_parsers.py @@ -39,13 +39,13 @@ class ImportTest(TestCase): def setUpTestData(cls): def make_person(firstname, lastname, nickname=False, vfho=False, guest=False): fullname = f"{firstname} {lastname}" - lookupAttribs = {"first_name": firstname, "last_name": (lastname or "")} - nonLookupAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname} - person = Person.objects.create(**nonLookupAttribs, **lookupAttribs) + coUniqueAttribs = {"first_name": firstname, "last_name": (lastname or "")} + otherAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname} + person = Person.objects.create(**otherAttribs, **coUniqueAttribs) - lookupAttribs = {"person": person, "expedition": cls.test_expo} - nonLookupAttribs = {"is_guest": guest} - pe = PersonExpedition.objects.create(**nonLookupAttribs, **lookupAttribs) + coUniqueAttribs = {"person": person, "expedition": cls.test_expo} + otherAttribs = {"is_guest": guest} + pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs) return person @@ -58,9 +58,9 @@ class ImportTest(TestCase): if frontmatter_file.is_file(): frontmatter_file.unlink() # delete if it exists - lookupAttribs = {"year": TEST_YEAR} - nonLookupAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"} - cls.test_expo = Expedition.objects.create(**nonLookupAttribs, **lookupAttribs) + coUniqueAttribs = {"year": TEST_YEAR} + otherAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"} + cls.test_expo = Expedition.objects.create(**otherAttribs, **coUniqueAttribs) fred = make_person("Fred", "Smartarse", nickname="freddy") phil = make_person("Phil", "Tosser", nickname="tosspot") diff --git a/core/models/troggle.py b/core/models/troggle.py index 38850ee..5db2d10 100644 --- a/core/models/troggle.py +++ b/core/models/troggle.py @@ -82,6 +82,8 @@ class Person(TroggleModel): last_name = models.CharField(max_length=100) fullname = models.CharField(max_length=200) nickname = models.CharField(max_length=200) + slug = models.SlugField(max_length=50, unique=True) + is_vfho = models.BooleanField( help_text="VFHO is the Vereines für Höhlenkunde in Obersteier, a nearby Austrian caving club.", default=False, diff --git a/core/utils.py b/core/utils.py index 17bd953..b7f3417 100644 --- a/core/utils.py +++ b/core/utils.py @@ -276,8 +276,8 @@ def writetrogglefile(filepath, filecontent): # not catching and re-raising any exceptions yet, inc. the stderr etc.,. We should do that. -def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): - """Looks up instance using lookupAttribs and carries out the following: +def save_carefully(objectType, coUniqueAttribs={}, otherAttribs={}): + """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 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) @@ -293,15 +293,15 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): """ try: - instance, created = objectType.objects.get_or_create(defaults=nonLookupAttribs, **lookupAttribs) + instance, created = objectType.objects.get_or_create(defaults=otherAttribs, **coUniqueAttribs) except: print(" !! - FAIL in SAVE CAREFULLY ===================", objectType) print(" !! - -- objects.get_or_create()") - print(f" !! - lookupAttribs:{lookupAttribs}\n !! - nonLookupAttribs:{nonLookupAttribs}") + print(f" !! - coUniqueAttribs:{coUniqueAttribs}\n !! - otherAttribs:{otherAttribs}") raise if not created and not instance.new_since_parsing: for k, v in list( - nonLookupAttribs.items() + otherAttribs.items() ): # overwrite the existing attributes from the logbook text (except date and title) setattr(instance, k, v) try: @@ -309,7 +309,7 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): except: print(" !! - SAVE CAREFULLY ===================", objectType) print(" !! - -- instance.save()") - print(f" !! - lookupAttribs:{lookupAttribs}\n !! - nonLookupAttribs:{nonLookupAttribs}") + print(f" !! - coUniqueAttribs:{coUniqueAttribs}\n !! - otherAttribs:{otherAttribs}") raise try: str(instance) diff --git a/core/views/uploads.py b/core/views/uploads.py index 8bec24e..0c2f655 100644 --- a/core/views/uploads.py +++ b/core/views/uploads.py @@ -77,7 +77,7 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t DataIssue.objects.create(parser="logbooks", message=message) slug = slug + "_" + unique_slug(text,2) - nonLookupAttribs = { + otherAttribs = { "place": place, "text": text, "expedition": expedition, @@ -86,9 +86,9 @@ def store_edited_entry_into_database(date, place, title, text, others, author, t "title": f"{place} - {title}", # "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 = [] # 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) DataIssue.objects.create(parser="logbooks", message=message) else: - lookupAttribs = {"personexpedition": personyear, "nickname_used": name, "logbook_entry": lbo} # lbo is primary key - nonLookupAttribs = {"time_underground": tu, "is_logbook_entry_author": (name==author)} - pt_list.append(PersonLogEntry(**nonLookupAttribs, **lookupAttribs)) + coUniqueAttribs = {"personexpedition": personyear, "nickname_used": name, "logbook_entry": lbo} # lbo is primary key + otherAttribs = {"time_underground": tu, "is_logbook_entry_author": (name==author)} + pt_list.append(PersonLogEntry(**otherAttribs, **coUniqueAttribs)) except: # This should not happen. We do not raise exceptions in that function diff --git a/parsers/QMs.py b/parsers/QMs.py index 56c97e4..9691ce7 100644 --- a/parsers/QMs.py +++ b/parsers/QMs.py @@ -173,7 +173,7 @@ def parse_KH_QMs(kh, inputFile, ticked): resolution_station_name = station_name.replace("<a href=\"","<a href=\"/1623/161/") else: nearest_station_name = station_name.replace("<a href=\"","<a href=\"/1623/161/") - lookupAttribs = { + coUniqueAttribs = { #'found_by':placeholder, "blockname": "", "expoyear": year, @@ -181,7 +181,7 @@ def parse_KH_QMs(kh, inputFile, ticked): "cave": kh, "grade": res["grade"], } - nonLookupAttribs = { + otherAttribs = { "ticked": ticked, "page_ref": "", "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/"), } # 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 else: if dataline.startswith("<dt><a href"): diff --git a/parsers/logbooks.py b/parsers/logbooks.py index 50324a1..90c13aa 100644 --- a/parsers/logbooks.py +++ b/parsers/logbooks.py @@ -262,7 +262,7 @@ def store_entry_into_database(date, place, tripcave, title, text, trippersons, a # if guests: # print(f" {date} - {guests}") - nonLookupAttribs = { + otherAttribs = { "place": place, "other_people": other_people, # *Ol's Mum, foreigners.. "text": text, @@ -270,20 +270,20 @@ def store_entry_into_database(date, place, tripcave, title, text, trippersons, a "time_underground": logtime_underground, "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(): # oops. Our code should already have ensured this is unique. message = " ! - DUPLICATE SLUG for logbook entry " + tripdate + " - " + slug DataIssue.objects.create(parser="logbooks", message=message) slug = slug + "_" + unique_slug(text,2) - lbo = LogbookEntry.objects.create(**nonLookupAttribs, **lookupAttribs) + lbo = LogbookEntry.objects.create(**otherAttribs, **coUniqueAttribs) pt_list = [] for tripperson, nickname_used, time_underground in trippersons: - lookupAttribs = {"personexpedition": tripperson, "nickname_used": nickname_used, "logbook_entry": lbo} # lbo is primary key - nonLookupAttribs = {"time_underground": time_underground, "is_logbook_entry_author": (tripperson == author)} - pt_list.append(PersonLogEntry(**nonLookupAttribs, **lookupAttribs)) + coUniqueAttribs = {"personexpedition": tripperson, "nickname_used": nickname_used, "logbook_entry": lbo} # lbo is primary key + otherAttribs = {"time_underground": time_underground, "is_logbook_entry_author": (tripperson == author)} + pt_list.append(PersonLogEntry(**otherAttribs, **coUniqueAttribs)) PersonLogEntry.objects.bulk_create(pt_list) def parser_date(tripdate, year): diff --git a/parsers/people.py b/parsers/people.py index ef6984b..4799ebf 100644 --- a/parsers/people.py +++ b/parsers/people.py @@ -78,15 +78,16 @@ def load_people_expos(): if nexpos <= 0: print(" - Creating expeditions") for year in years: - lookupAttribs = {"year": year} - nonLookupAttribs = {"name": f"CUCC expo {year}"} - e = Expedition.objects.create(**nonLookupAttribs, **lookupAttribs) + coUniqueAttribs = {"year": year} + otherAttribs = {"name": f"CUCC expo {year}"} + e = Expedition.objects.create(**otherAttribs, **coUniqueAttribs) print(" - Loading personexpeditions") for personline in personreader: name = personline[header["Name"]] name = re.sub(r"<.*?>", "", name) + slug = slugify(name) firstname = "" nick = "" @@ -111,9 +112,9 @@ def load_people_expos(): else: vfho = True - lookupAttribs = {"first_name": firstname, "last_name": (lastname or "")} - nonLookupAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nick} - person = Person.objects.create(**nonLookupAttribs, **lookupAttribs) + coUniqueAttribs = {"first_name": firstname, "last_name": (lastname or "")} + otherAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nick} + person = Person.objects.create(**otherAttribs, **coUniqueAttribs) 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:]: expedition = Expedition.objects.get(year=year) if attended == "1" or attended == "-1": - lookupAttribs = {"person": person, "expedition": expedition} - nonLookupAttribs = {"is_guest": (personline[header["Guest"]] == "1")} - pe = PersonExpedition.objects.create(**nonLookupAttribs, **lookupAttribs) + coUniqueAttribs = {"person": person, "expedition": expedition} + otherAttribs = {"is_guest": (personline[header["Guest"]] == "1")} + pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs) print("", flush=True) |