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

Edit Logbook Entry mostly working

This commit is contained in:
2023-08-31 18:55:20 +03:00
parent bbb821e2f9
commit c29c12ea76
7 changed files with 276 additions and 100 deletions

View File

@@ -7,7 +7,7 @@ from django.shortcuts import render
from django.template import loader
from troggle.core.models.caves import Cave
from troggle.core.models.logbooks import LogbookEntry # , PersonLogEntry
from troggle.core.models.logbooks import LogbookEntry, writelogbook # , PersonLogEntry
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
from troggle.core.models.troggle import Expedition
@@ -169,69 +169,31 @@ def controlpanel(request):
)
def exportlogbook(request, year=None, extension=None):
def exportlogbook(request, year=None):
"""Constructs, from the database, a complete HTML formatted logbook
for the current year. Formats available are HTML2005. Other formats
have been retired.
for the current year. Format available is now just HTML2005.
Other formats have been retired.
There are no images stored in the database. However links to images work in the HTML text of a logbook entry.
This function is the recipient of the POST action os the export form in the control panel
This function is the recipient of the POST action as the export form in the control panel
"""
def lbeKey(lbe):
"""This function goes into a lexicographic sort function"""
return str(lbe.date)
"""This function goes into a lexicographic sort function - but where?!"""
return str(lbe.slug) # now that slugs are tripid such as 2023-07-30b
if not request.method == "POST":
return render(request, "controlPanel.html", {"expeditions": Expedition.objects.all(), "jobs_completed": ""})
else:
print(f"Logbook export {request.POST}")
# print(f"Logbook export {request.POST}")
year = request.POST["year"]
current_expedition = Expedition.objects.get(year=year)
logbook_entries = LogbookEntry.objects.filter(expedition=current_expedition).order_by(
"date"
) # need to be sorted by date!
filename = "logbook-new-format.html"
print(f"Logbook has {len(logbook_entries)} entries in it.")
extension = "html"
response = HttpResponse(content_type="text/html")
style = "2005"
filename = "logbook-new-format." + extension
template = "logbook" + style + "style." + extension
response["Content-Disposition"] = "attachment; filename=" + filename
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}')
writelogbook(year, filename)
#response = HttpResponse(content_type="text/html")
#response["Content-Disposition"] = "attachment; filename=" + filename
completed = f'Logbook exported to <a href="/years/{filename}">{filename}</a>'
return render(