2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 00:17:07 +00:00

logbook entries json exporter

This commit is contained in:
2025-11-21 23:57:00 +02:00
parent f378406893
commit d4ec144434
3 changed files with 55 additions and 4 deletions

View File

@@ -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 <troggle@exposerver.expo>"
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