summaryrefslogtreecommitdiffstats
path: root/core/models/logbooks.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2023-08-31 18:55:20 +0300
committerPhilip Sargent <philip.sargent@gmail.com>2023-08-31 18:55:20 +0300
commitc29c12ea765eefcd2ac41a7f475c8d870adcb793 (patch)
treed5da2cc80bb15e913835bedfd70f340413af17fe /core/models/logbooks.py
parentbbb821e2f9a55eaaf636909341b8c7799f079ce2 (diff)
downloadtroggle-c29c12ea765eefcd2ac41a7f475c8d870adcb793.tar.gz
troggle-c29c12ea765eefcd2ac41a7f475c8d870adcb793.tar.bz2
troggle-c29c12ea765eefcd2ac41a7f475c8d870adcb793.zip
Edit Logbook Entry mostly working
Diffstat (limited to 'core/models/logbooks.py')
-rw-r--r--core/models/logbooks.py45
1 files changed, 44 insertions, 1 deletions
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"<body>", "<body>\n" + frontmatter + endmatter, logbookfile)
+ else:
+ logbookfile = re.sub(r"<body>", f"<body>\n<h1>Expo {year}</h1>\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.