diff options
author | Philip Sargent <philip.sargent@klebos.com> | 2021-04-26 02:10:45 +0100 |
---|---|---|
committer | Philip Sargent <philip.sargent@klebos.com> | 2021-04-26 02:10:45 +0100 |
commit | 72fa8a5883a12e73402a4d4fda74a7b27faba49d (patch) | |
tree | ac2ff9507a36152a0b2a73e88878acbd568dda01 /parsers/caves.py | |
parent | a656ada67a5b7ff81634f0adcb553de295d624f4 (diff) | |
download | troggle-72fa8a5883a12e73402a4d4fda74a7b27faba49d.tar.gz troggle-72fa8a5883a12e73402a4d4fda74a7b27faba49d.tar.bz2 troggle-72fa8a5883a12e73402a4d4fda74a7b27faba49d.zip |
Making entrances work for pending caves
Diffstat (limited to 'parsers/caves.py')
-rw-r--r-- | parsers/caves.py | 157 |
1 files changed, 107 insertions, 50 deletions
diff --git a/parsers/caves.py b/parsers/caves.py index 8ccaa9d..ce2eaa7 100644 --- a/parsers/caves.py +++ b/parsers/caves.py @@ -23,9 +23,104 @@ entrances_xslug = {} caves_xslug = {} areas_xslug = {} +def do_pending_cave(k, url, area_1623): + ''' + default for a PENDING cave, should be overwritten in the db later if a real cave of the same name exists + in expoweb/cave_data/1623-"k".html + ''' + default_note = f"_Survex file found in loser repo but no description in expoweb <br><br><br>\n" + default_note += f"INSTRUCTIONS: First open 'This survex file' (link above the CaveView panel) to find the date and info. Then " + default_note += f"<br>\n - search in the Expo for that year e.g. <a href='/expedition/{k[0:4]}'>{k[0:4]}</a> to find a " + default_note += f"relevant logbook entry, then <br>\n - " + default_note += f"click on 'Edit this cave' and copy the information you find in the survex file and the logbook" + default_note += f"<br>\n - " + default_note += f"When you Submit it will create a file file in expoweb/cave_data/ " + default_note += f"<br>\n - Now you can edit the entrance info: click on Edit below for the dummy entrance. " + default_note += f"and then Submit to save it. NB your entrance info will not be visible after a reboot of the server until a programmer has edited parser/caves.py (python code) to remove the cave form the [pending] list. But it won't be lost." + + slug = "1623-" + k + + cave = Cave( + unofficial_number = k, + underground_description = "Pending cave write-up - creating as empty object. No XML file available yet.", + survex_file = "caves-1623/" + k + "/" + k +".svx", + url = url, + notes = default_note) + if cave: + cave.save() # must save to have id before foreign keys work. This is also a ManyToMany key. + cave.area.add(area_1623[0]) + cave.save() + message = f" ! {k:12} {cave.underground_description}" + DataIssue.objects.create(parser='caves', message=message, url=url) + print(message) + + try: # Now create a cave slug ID + cs = CaveSlug.objects.update_or_create(cave = cave, + slug = slug, primary = False) + except: + message = f" ! {k:11s} PENDING cave SLUG create failure" + DataIssue.objects.create(parser='caves', message=message) + print(message) + else: + message = f' ! {k:11s} PENDING cave create failure' + DataIssue.objects.create(parser='caves', message=message) + print(message) + + ent = Entrance( + name = k, + entrance_description = "Dummy entrance: auto-created when registering a new cave." + + "This file WILL NOT BE LOADED while the cave is in the pending[] list in parsers/caves.py", + marking = '?') + if ent: + ent.save() # must save to have id before foreign keys work. + try: # Now create a entrance slug ID + es = EntranceSlug.objects.update_or_create(entrance = ent, + slug = slug, primary = False) + except: + message = f" ! {k:11s} PENDING entrance create failure" + DataIssue.objects.create(parser='caves', message=message) + print(message) + + # Now we will actually write this default entrance slugfile. Yes it's naughty, but an extra + # entrance file is less hassle than a missing one! And people always forget to save the entrance file + # when they are saving the edited cave file. + + #This WILL then casue an error when the parser then tries to import this file and we get an import failure because + # the entrance exists in the db. + ent.cached_primary_slug = slug + ent.filename = slug + ".html" + ent.save() + if not ent.get_file_path().is_file(): + # don't overwrite if by some chnace it exists + # but the pre-existing file won't be parsed later until the cave is removed from the pending list + ent.writeDataFile() + else: + message = f" ! {k:11s} PENDING cave SLUG '{slug}' create failure" + DataIssue.objects.create(parser='caves', message=message) + print(message) + + try: + ceinsts = CaveAndEntrance.objects.update_or_create(cave = cave, entrance_letter = "", entrance = ent) + for ceinst in ceinsts: + if str(ceinst) == str(cave): # magic runes... why is the next value a Bool? + ceinst.cave = cave + ceinst.save() + break + except: + message = f" ! {k:11s} PENDING entrance + cave UNION create failure '{cave}' [{ent}]" + DataIssue.objects.create(parser='caves', message=message) + print(message) + + def readcaves(): '''Reads the xml-format HTML files in the EXPOWEB repo, not from the loser repo. ''' + # For those caves which do not have cave_data/1623-xxx.html XML files even though they exist and have surveys + pending = ["2007-06", "2009-02", + "2010-06", "2010-07", "2012-ns-01", "2012-ns-02", "2010-04", "2012-ns-05", "2012-ns-06", + "2012-ns-07", "2012-ns-08", "2012-ns-12", "2012-ns-14", "2012-ns-15", "2014-bl888", + "2018-pf-01", "2018-pf-02"] + with transaction.atomic(): print(" - Deleting Caves and Entrances") Cave.objects.all().delete() @@ -33,7 +128,6 @@ def readcaves(): # Clear the cave data issues and the caves as we are reloading DataIssue.objects.filter(parser='caves').delete() DataIssue.objects.filter(parser='entrances').delete() - area_1623 = Area.objects.update_or_create(short_name = "1623", parent = None) # This seems to return a tuple, not a single object! i.e. (<Area: 1623>, True) @@ -44,64 +138,26 @@ def readcaves(): print (" - Setting pending caves") # Do this first, so that these empty entries are overwritten as they get properly created. - # For those caves which do not have cave_data/1623-xxx.html XML files even though they exist and have surveys - pending = ["2007-06", "2009-01", "2009-02", - "2010-06", "2010-07", "2012-ns-01", "2012-ns-02", "2010-04", "2012-ns-05", "2012-ns-06", - "2012-ns-07", "2012-ns-08", "2012-ns-12", "2012-ns-14", "2012-ns-15", "2014-bl888", - "2018-pf-01", "2018-pf-02", "haldenloch"] for k in pending: - url = "1623/" + k + url = "1623/" + k # Note we are not appending the .htm as we are modern folks now. try: - # default for a PENDING cave, ooverwritten if a real cave exists - cave = Cave( - unofficial_number = k, - # official_name = "", - underground_description = "Pending cave write-up - creating as empty object. No XML file available yet.", - survex_file = "caves-1623/" + k + "/" + k +".svx", - url = url, - notes=f"_Survex file found in loser repo but no description in expoweb <br><br><br>\n"+ - f"INSTRUCTIONS: First open 'This survex file' (link above the CaveView panel) to find the date and info. Then " + - f"<br>\n - search in the Expo for that year e.g. <a href='/expedition/{k[0:4]}'>{k[0:4]}</a> to find a relevant logbook entry, then \n - " + - f"click on 'Edit this cave' and copy the information you find in the survex file and the logbook"+ - f"<br>\n - " + - f"When you Submit it will create a file file in expoweb/cave_data/ " + - f"<br>\n - but you have not finished yet. You MUST go and create the entrance: click on New Entrance. Then this will no longer be 'Pending' once the flag has been removed from the code") - if cave: - cave.save() # must save to have id before foreign keys work. This is also a ManyToMany key. - #print(f' ! - READ CAVES: cave {k} {cave}') - cave.area.add(area_1623[0]) - cave.save() - message = f" ! {k:12} {cave.underground_description}" - DataIssue.objects.create(parser='caves', message=message, url=url) - print(message) - - try: # Now create a cave slug ID - cs = CaveSlug.objects.update_or_create(cave = cave, - slug = "1623-" + k, - primary = False) - except: - message = " ! {:11s} {} PENDING cave slug create failure".format(k) - DataIssue.objects.create(parser='caves', message=message) - print(message) - else: - message = f' ! {k:11s} PENDING cave slug create failure' - DataIssue.objects.create(parser='caves', message=message) - print(message) - - + do_pending_cave(k, url, area_1623) except: - message = " ! Error. Cannot create pending cave, pending-id:{}".format(k) + message = " ! Error. Cannot create pending cave and entrance, pending-id:{}".format(k) DataIssue.objects.create(parser='caves', message=message) print(message) raise with transaction.atomic(): - print(" - Reading Entrances from entrance descriptions xml files") print(" - settings.CAVEDESCRIPTIONS: ", settings.CAVEDESCRIPTIONS) + print(" - Reading Entrances from entrance descriptions xml files") for filename in next(os.walk(settings.ENTRANCEDESCRIPTIONS))[2]: #Should be a better way of getting a list of files if filename.endswith('.html'): - readentrance(filename) + if Path(filename).stem[5:] in pending: + print(f'Skipping pending entrance dummy file <{filename}>') + else: + readentrance(filename) print(" - Reading Caves from cave descriptions xml files") for filename in next(os.walk(settings.CAVEDESCRIPTIONS))[2]: #Should be a better way of getting a list of files @@ -182,13 +238,14 @@ def readentrance(filename): primary = primary) except: # need to cope with duplicates - print(" ! FAILED to get only one ENTRANCE when updating using: "+filename) + message = f" ! FAILED to get precisely one ENTRANCE when updating using: cave_entrance/{filename}" + DataIssue.objects.create(parser='caves', message=message, url=f'/cave/{slug}/edit/') kents = EntranceSlug.objects.all().filter(entrance = e, slug = slug, primary = primary) for k in kents: message = " ! - DUPLICATE in db. entrance:"+ str(k.entrance) + ", slug:" + str(k.slug()) - DataIssue.objects.create(parser='caves', message=message) + DataIssue.objects.create(parser='caves', message=message, url=f'/cave/{slug}/edit/') print(message) for k in kents: if k.slug() != None: @@ -300,7 +357,7 @@ def readcave(filename): primary = primary) caves_xslug[slug] = cs except Exception as ex: - message = " ! Cave update/create failure : %s, skipping file %s\nException: %s" % (slug, context, ex.__class__) + message = " ! Cave update/create failure : %s, skipping file cave_data/%s with exception\nException: %s" % (slug, context, ex.__class__) DataIssue.objects.create(parser='caves', message=message) print(message) |