From c29c12ea765eefcd2ac41a7f475c8d870adcb793 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 31 Aug 2023 18:55:20 +0300 Subject: Edit Logbook Entry mostly working --- core/models/logbooks.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'core/models/logbooks.py') diff --git a/core/models/logbooks.py b/core/models/logbooks.py index 3498de1..aef21c1 100644 --- a/core/models/logbooks.py +++ b/core/models/logbooks.py @@ -1,8 +1,11 @@ +import re + from pathlib import Path from urllib.parse import urljoin from django.db import models from django.urls import reverse +from django.template import loader import settings from troggle.core.models.troggle import Expedition, TroggleModel @@ -50,7 +53,7 @@ class LogbookEntry(TroggleModel): max_length=100, blank=True, null=True, help_text="Only use this if you haven't chosen a cave" ) text = models.TextField() - slug = models.SlugField(max_length=50) + slug = models.SlugField(max_length=50) # this is tripid time_underground = models.FloatField(null=True, help_text="In decimal hours") class Meta: @@ -93,7 +96,47 @@ class LogbookEntry(TroggleModel): index = index % mx return index +def writelogbook(year, filename): + current_expedition = Expedition.objects.get(year=year) + logbook_entries = LogbookEntry.objects.filter(expedition=current_expedition).order_by( + "slug" + ) # now that slug, aka tripid, is in our standard date form, this will preserve ordering. + + print(f"Logbook exported has {len(logbook_entries)} entries in it.") + extension = "html" + template = "logbook2005style.html" + + t = loader.get_template(template) + logbookfile = t.render({"logbook_entries": logbook_entries}) + + endpath = Path(settings.EXPOWEB, "years", year, "endmatter.html") + endmatter = "" + if endpath.is_file(): + try: + with open(endpath, "r") as end: + endmatter = end.read() + except: + print(" ! Very Bad Error opening " + endpath) + + frontpath = Path(settings.EXPOWEB, "years", year, "frontmatter.html") + if frontpath.is_file(): + try: + with open(frontpath, "r") as front: + frontmatter = front.read() + except: + print(" ! Very Bad Error opening " + frontpath) + logbookfile = re.sub(r"", "\n" + frontmatter + endmatter, logbookfile) + else: + logbookfile = re.sub(r"", f"\n

Expo {year}

\n" + endmatter, logbookfile) + + dir = Path(settings.EXPOWEB) / "years" / year + filepath = Path(dir, filename) + with (open(filepath, "w")) as lb: + lb.writelines(logbookfile) + + # print(f'Logbook exported to {filepath}') + class PersonLogEntry(TroggleModel): """Single Person going on a trip, which may or may not be written up. It could account for different T/U for people in same logbook entry. -- cgit v1.2.3