From d4ec144434e4c8d88a890d4734d5f38893b3254f Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 21 Nov 2025 23:57:00 +0200 Subject: [PATCH] logbook entries json exporter --- core/views/logbooks.py | 50 +++++++++++++++++++++++++++++++++++++--- templates/logreport.html | 7 +++++- urls.py | 2 ++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/core/views/logbooks.py b/core/views/logbooks.py index d7d6b87..c042d6a 100644 --- a/core/views/logbooks.py +++ b/core/views/logbooks.py @@ -1,19 +1,22 @@ +import json import re from django.core.exceptions import ValidationError +from django.core.serializers import serialize from django.db.models import Q from django.shortcuts import redirect, render from django.views.generic.list import ListView from django.contrib.auth.models import User + import troggle.settings as settings from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry, writelogbook from troggle.core.models.survex import SurvexBlock, SurvexFile from troggle.core.models.troggle import Expedition, Person from troggle.core.models.wallets import Wallet -from troggle.core.utils import TROG, current_expo +from troggle.core.utils import TROG, current_expo, add_commit, git_commit from troggle.parsers.imports import import_logbook -from troggle.parsers.people import ensure_users_are_persons + """These views are for logbook items when they appear in an 'expedition' page and for persons: their individual pages and their perseonexpedition pages. @@ -325,7 +328,7 @@ def logreport(request, year=1999): msg = f' Logbook report for year:"{year}" not implemented yet\n{e}\n {context}' print(msg) return render(request, "errors/generic.html", {"message": msg}) - + def logbookentry(request, date, slug): """Displays a single logbook entry however, if an author has not used the correct URL in an image or a reference, then a link from @@ -375,3 +378,44 @@ def get_logbook_entries(request, expeditionslug): return render( request, "options.html", {"items": [(le.slug, f"{le.date} - {le.title}") for le in exp.logbookentry_set.all()]} ) + +def logbook_entries_export(request, year): + exp = Expedition.objects.get(year=year) + + entries = exp.logbookentry_set.all() + for e in entries[:3]: + print(f"{e}") + + write_entries(entries[:3], year) + return redirect(f"/logreport/{year}") + + +def write_entries(entries, year, git_string=None): + if not git_string: + git_string = f"troggle " + + dirpath = settings.EXPOWEB / "years" / year + + for le in entries[:4]: + jsondict = { "logbook_entry": le } + #print(jsondict) + + jsondict = serialize("json", [le], fields=('date', 'expedition', 'title', 'cave', 'place', 'other_people', 'date_field', 'text', 'slug', 'time_underground')) + print(jsondict) + filepath = dirpath / le.slug + description = f" {le.slug} :: {le.date} - {le.title}" + print(filepath, description) + try: + with open(filepath, 'w', encoding='utf-8') as json_f: + json.dump(jsondict, json_f, indent=1) + except PermissionError as e: + raise PermissionError( + f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filepath}. Ask a nerd to fix this: {e}" + ) + except Exception as e: + print(f"CANNOT write this file {filepath}. Exception dumping json. Ask a nerd to fix this: {e}") + raise e + + commit_msg = f"Exporting logbook entries as individual files" + git_commit(dirpath, commit_msg, git_string, commands=[]) + return True \ No newline at end of file diff --git a/templates/logreport.html b/templates/logreport.html index 44a0333..c13d941 100644 --- a/templates/logreport.html +++ b/templates/logreport.html @@ -19,7 +19,10 @@

(Hover mouse over the date to see the slug for the entry.) -{% if logged_in %}Logged in as expoadmin{% endif %} +{% if logged_in %}Logged in as expoadmin
+Export logbook entries as individual files +
+{% endif %} @@ -82,6 +85,8 @@
  • alias names for this Expo
  • full logbook for this Expo
  • new logbook entry for this Expo +{% if logged_in %}
  • export logbook entries as individual files in /years/{{expedition.year}}/entries/ +{% endif %} {% endblock %} diff --git a/urls.py b/urls.py index b36de61..d5c2873 100644 --- a/urls.py +++ b/urls.py @@ -51,6 +51,7 @@ from troggle.core.views.logbooks import ( expedition, get_logbook_entries, get_people, + logbook_entries_export, logbookentry, logentrydelete, logreport, @@ -240,6 +241,7 @@ trogglepatterns = [ # Logbook entries re_path(r'^logbookentry/(?P.*)/(?P.*)/?$', logbookentry,name="logbookentry"), re_path(r'^logbook$', exportlogbook, name='exportlogbook'), + path('logbook_entries/', logbook_entries_export, name='logbook_entries_export'), path('logreport/', logreport, name='logreport'), path('logentrydelete/', logentrydelete, name='logentrydelete'),