2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-18 22:57:09 +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 import re
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.serializers import serialize
from django.db.models import Q from django.db.models import Q
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.contrib.auth.models import User from django.contrib.auth.models import User
import troggle.settings as settings import troggle.settings as settings
from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry, writelogbook from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry, writelogbook
from troggle.core.models.survex import SurvexBlock, SurvexFile from troggle.core.models.survex import SurvexBlock, SurvexFile
from troggle.core.models.troggle import Expedition, Person from troggle.core.models.troggle import Expedition, Person
from troggle.core.models.wallets import Wallet 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.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 """These views are for logbook items when they appear in an 'expedition' page
and for persons: their individual pages and their perseonexpedition pages. and for persons: their individual pages and their perseonexpedition pages.
@@ -375,3 +378,44 @@ def get_logbook_entries(request, expeditionslug):
return render( return render(
request, "options.html", {"items": [(le.slug, f"{le.date} - {le.title}") for le in exp.logbookentry_set.all()]} 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

View File

@@ -19,7 +19,10 @@
</p> </p>
<p>(Hover mouse over the date to see the slug for the entry.) <p>(Hover mouse over the date to see the slug for the entry.)
{% if logged_in %}<font color="red">Logged in as expoadmin</font>{% endif %} {% if logged_in %}<font color="red">Logged in as expoadmin<br />
<a href="/logbook_entries/{{expedition.year}}">Export logbook entries as individual files</a>
</font>
{% endif %}
<table class="expeditionlogbooks"> <table class="expeditionlogbooks">
@@ -82,6 +85,8 @@
<li> <a href="/aliases/{{expedition.year}}">alias names</a> for this Expo <li> <a href="/aliases/{{expedition.year}}">alias names</a> for this Expo
<li> <a href="/years/{{expedition.year}}/{{expedition.logbookfile}}">full logbook</a> for this Expo <li> <a href="/years/{{expedition.year}}/{{expedition.logbookfile}}">full logbook</a> for this Expo
<li> <a href="/logbookedit/">new logbook entry</a> for this Expo <li> <a href="/logbookedit/">new logbook entry</a> for this Expo
{% if logged_in %}<li><font color="red"><a href="/logbook_entries/{{expedition.year}}">export logbook entries</a> as individual files in /years/{{expedition.year}}/entries/</font>
{% endif %}
</ul> </ul>
{% endblock %} {% endblock %}

View File

@@ -51,6 +51,7 @@ from troggle.core.views.logbooks import (
expedition, expedition,
get_logbook_entries, get_logbook_entries,
get_people, get_people,
logbook_entries_export,
logbookentry, logbookentry,
logentrydelete, logentrydelete,
logreport, logreport,
@@ -240,6 +241,7 @@ trogglepatterns = [
# Logbook entries # Logbook entries
re_path(r'^logbookentry/(?P<date>.*)/(?P<slug>.*)/?$', logbookentry,name="logbookentry"), re_path(r'^logbookentry/(?P<date>.*)/(?P<slug>.*)/?$', logbookentry,name="logbookentry"),
re_path(r'^logbook$', exportlogbook, name='exportlogbook'), re_path(r'^logbook$', exportlogbook, name='exportlogbook'),
path('logbook_entries/<slug:year>', logbook_entries_export, name='logbook_entries_export'),
path('logreport/<slug:year>', logreport, name='logreport'), path('logreport/<slug:year>', logreport, name='logreport'),
path('logentrydelete/<slug:year>', logentrydelete, name='logentrydelete'), path('logentrydelete/<slug:year>', logentrydelete, name='logentrydelete'),