diff --git a/core/views/logbooks.py b/core/views/logbooks.py index 716e17c..68b2c1e 100644 --- a/core/views/logbooks.py +++ b/core/views/logbooks.py @@ -28,6 +28,7 @@ todo = """- Fix the get_person_chronology() display bug. - Fix id= value preservation on editing """ +LOGBOOK_ENTRIES = "log_entries" # directory name def notablepersons(request): def notabilitykey(person): @@ -383,34 +384,21 @@ def logbook_entries_export(request, year): entries = get_entries(year) # for e in entries: # print(f"{e.pk:03} {e}") - editor = get_editor(request) + write_entries(entries, year, editor) return redirect(f"/logreport/{year}") -def write_entries(entries, year, editor): +def write_entries(entries, year, editor): + """Exports logentries from the live database to JSON files. + + entries - a list, use a list of one member if writing a single entry + + year - the year of the expo. + """ - dirpath = settings.EXPOWEB / "years" / year / "log_entries" - try: - dirpath.mkdir(parents=True, exist_ok=True) - except PermissionError as e: - raise PermissionError( - f"CANNOT make the directory.\nPERMISSIONS incorrectly set on server for this file {filepath}. Ask a nerd to fix this: {e}" - ) - except Exception as e: - raise OSError( - f"CANNOT make the directory for {filepath}. Ask a nerd to fix this: {e}" - ) - - for le in entries: - # REPLACE this with hand-built serializer which includes .author, .who which were added to the entries but re not in the model Class directly - # see below for Gemini code to do that. Going to bed now. - jsondict = serialize("json", [le], fields=('slug', 'date', 'expedition', 'title', 'cave', 'place', 'other_people', 'time_underground', 'text')) - - filename = f"{le.slug}-{le.pk:03}.json" - filepath = dirpath / filename - description = f" {le.slug} :: {le.date} - {le.title}" + def write_json_file(): try: with open(filepath, 'w', encoding='utf-8') as json_f: json.dump(jsondict, json_f, indent=1) @@ -420,8 +408,21 @@ def write_entries(entries, year, editor): ) except Exception as e: print(f"CANNOT write this file {filepath}. Exception dumping json. Ask a nerd to fix this: {e}") - raise e + raise e + + dirpath = settings.EXPOWEB / "years" / year / LOGBOOK_ENTRIES + dirpath.mkdir(parents=True, exist_ok=True) + for le in entries: + filename = f"{le.slug}-{le.pk:03}.json" + filepath = dirpath / filename + # description = f" {le.slug} :: {le.date} - {le.title}" + + # REPLACE this with hand-built serializer which includes .author, .who which were added to the entries but re not in the model Class directly + # see below for Gemini code to do that. Going to bed now. + jsondict = serialize("json", [le], fields=('slug', 'date', 'expedition', 'title', 'cave', 'place', 'other_people', 'time_underground', 'text')) + + write_json_file() git_add(filename, dirpath) commit_msg = f"Exporting logbook entries as individual files"