2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-02-08 07:40:19 +00:00

SHould be nearly working, but crashes on saving edited entry

This commit is contained in:
2025-11-26 01:22:38 +02:00
parent 0e6a3e457d
commit ced9a7b024
13 changed files with 187 additions and 98 deletions

View File

@@ -25,6 +25,7 @@ from troggle.core.utils import (
write_and_commit,
wrap_text,
)
from troggle.core.views.logbooks import write_entries_json
from troggle.parsers.people import GetPersonExpeditionNameLookup, known_foreigner
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
@@ -301,70 +302,76 @@ def logbookedit(request, year=None, slug=None):
return render(request, "errors/generic.html", {"message": message})
store_edited_entry_into_database(date, place, title, entry, others, author, tu, slug)
# Successful POST so save to fiesystem
json_entries_dir = settings.EXPOWEB / "years" / year / settings.JSON_LOG_ENTRIES
if json_entries_dir.is_dir(): # only 2025 currently, or the current expo
print(f"- Rewriting JUST this edited logbook entry to a JSON file. ")
this_entry = LogbookEntry.objects.get(slug=slug)
write_entries_json([this_entry], year, editor)
else:
print(f"- Rewriting the entire {year} logbook to disc ")
filename= "logbook.html"
try:
print(f" - Logbook for {year} to be exported and written out.")
writelogbook(year, filename) # uses a template, not the code fragment below which is just a visible hint to logged on user
except:
message = f'! - Logbook saving failed - \n!! Permissions failure ?! on attempting to save file "logbook.html"'
print(message)
return render(request, "errors/generic.html", {"message": message})
# So save to database and then write out whole new logbook.html file
print(f"- Rewriting the entire {year} logbook to disc ")
filename= "logbook.html"
try:
print(f" - Logbook for {year} to be exported and written out.")
writelogbook(year, filename) # uses a template, not the code fragment below which is just a visible hint to logged on user
except:
message = f'! - Logbook saving failed - \n!! Permissions failure ?! on attempting to save file "logbook.html"'
print(message)
return render(request, "errors/generic.html", {"message": message})
# Code fragment illustration - not actually what gets saved to database
output = f'''
# We do author validation on the form as displayed by GET, not at the moment of POST.
# If we had JS validation then we could be more timely.
dirpath = Path(settings.EXPOWEB) / "years" / str(year)
contents_path = dirpath / filename
commit_msg = f"Online edit of logbookentry {slug}"
add_commit(contents_path, commit_msg, editor)
# This does not change the URL in the browser, so despite a new slug being created,
# the next time this code is run it thinks a new slug needs to be created. So we should
# actually redirect to a new URL (an edit not a create) not simply return a render object.
# logbookedit/2022-08-21a
# HOWEVER by doing a redirect rather than returning a rendered page, we lose all the
# error settings e.g dateflag and authroflag so the user gets no feedback about bad data entered.
# so we need to pass the flags explicitly in the url and then extract them from the request in the GET bit. sigh.
response = HttpResponseRedirect(f"/logbookedit/{slug}?dateflag={dateflag}&authorflag={authorflag}")
response.set_cookie('editor_id', editor, max_age=get_cookie_max_age(request)) # cookie expires after get_cookie_max_age(request) seconds
return response
# Do the redirect instead of this:
# Code fragment illustration - not actually what gets saved to database
# output = f'''
<div class="tripdate" id="{slug}">{date}</div>
<div class="trippeople"><u>{author}</u>, {others}</div>
<div class="triptitle">{place} - {title}</div>
# <div class="tripdate" id="{slug}">{date}</div>
# <div class="trippeople"><u>{author}</u>, {others}</div>
# <div class="triptitle">{place} - {title}</div>
{entry}
# {entry}
<div class="timeug">T/U {tu} hrs</div>
<hr />
# <div class="timeug">T/U {tu} hrs</div>
# <hr />
'''
# Successful POST
# So save to database and then write out whole new logbook.html file
# We do author validation on the form as displayed by GET, not at the moment of POST.
# If we had JS validation then we could be more timely.
dirpath = Path(settings.EXPOWEB) / "years" / str(year)
contents_path = dirpath / filename
commit_msg = f"Online edit of logbookentry {slug}"
add_commit(contents_path, commit_msg, editor)
# This does not change the URL in the browser, so despite a new slug being created,
# the next time this code is run it thinks a new slug needs to be created. So we should
# actually redirect to a new URL (an edit not a create) not simply return a render object.
# logbookedit/2022-08-21a
# HOWEVER by doing a redirect rather than returning a rendered page, we lose all the
# error settings e.g dateflag and authroflag so the user gets no feedback about bad data entered.
# so we need to pass the flags explicitly in the url and then extract them from the request in the GET bit. sigh.
response = HttpResponseRedirect(f"/logbookedit/{slug}?dateflag={dateflag}&authorflag={authorflag}")
response.set_cookie('editor_id', editor, max_age=get_cookie_max_age(request)) # cookie expires after get_cookie_max_age(request) seconds
return response
# Do the redirect instead of this:
# return render(
# request,
# "logbookform.html",
# {
# "form": form,
# "year": year,
# "date": date, "dateflag": dateflag,
# "author": author, "authorflag": authorflag,
# "others": others,
# "place": place,
# "title": title,
# "tu": tu,
# "entry": entry,
# "output": output,
# "slug": slug,
# },
# )
# ''' # return render(
# request,
# "logbookform.html",
# {
# "form": form,
# "year": year,
# "date": date, "dateflag": dateflag,
# "author": author, "authorflag": authorflag,
# "others": others,
# "place": place,
# "title": title,
# "tu": tu,
# "entry": entry,
# "output": output,
# "slug": slug,
# },
# )
# GET here. Does not fall-through from the POST section.
else:

View File

@@ -30,8 +30,6 @@ todo = """- Fix the get_person_chronology() display bug.
- Fix id= value preservation on editing
"""
LOGBOOK_ENTRIES = "log_entries" # directory name
def notablepersons(request):
def notabilitykey(person):
return person.notability()
@@ -247,7 +245,7 @@ def logentrydelete(request, year):
"""This only gets called by a POST from the logreport page
This function is dedicated to James Waite who managed to make so many duplicate logbook entries
that we needed a sopecial mechanism to delete them.
that we needed a special mechanism to delete them.
"""
for i in request.POST:
print(f" - '{i}' {request.POST[i]}")
@@ -261,6 +259,7 @@ def logentrydelete(request, year):
filename= "logbook.html"
try:
writelogbook(year, filename) # uses a template
except:
message = f'! - Logbook saving failed - \n!! Permissions failure ?! on attempting to save file "logbook.html"'
print(message)
@@ -368,6 +367,32 @@ def logbookentry(request, date, slug):
print(msg)
return render(request, "errors/generic.html", {"message": msg})
def logbookfile(request, year):
"""This was just a link to logbook.html, an ordinary HTML file handbook-style, but in Nov.2025 we
changed to have individual JSON files per entry. So this "whole logbook" is now only
re-generated when anyone want to see it.
"""
json_entries_dir = settings.EXPOWEB / "years" / year / settings.JSON_LOG_ENTRIES
exp = Expedition.objects.get(year=year)
if json_entries_dir.is_dir(): # only 2025 currently, or the current expo
# Re-generate the logbook.html because it won't have been done by
# indovidual entry edits for this year
contents_path = settings.EXPOWEB / "years" / year / exp.logbookfile
try:
print(f" - Logbook for {year} to be exported and written out.")
writelogbook(year, exp.logbookfile, generate_on_view=True) # uses a template
commit_msg = f"Request to see whole logbook triggers re-exporting."
editor = get_editor(request)
add_commit(contents_path, commit_msg, editor)
except:
message = f'! - Logbook saving to file from database failed - \n!! Permissions failure ?! on attempting to save file {contents_path}'
print(message)
raise # got a one-off failure here, we need to know why..
return render(request, "errors/generic.html", {"message": message})
return redirect(f"/years/{year}/{exp.logbookfile}")
def get_people(request, expeditionslug):
exp = Expedition.objects.get(year=expeditionslug)
@@ -385,11 +410,11 @@ def logbook_entries_export(request, year):
entries = get_entries(year)
editor = get_editor(request)
write_entries(entries, year, editor)
write_entries_json(entries, year, editor)
return redirect(f"/logreport/{year}")
def write_entries(entries, year, editor):
def write_entries_json(entries, year, editor):
"""Exports logentries from the live database to JSON files.
entries - a list, use a list of one member if writing a single entry
@@ -438,13 +463,16 @@ def write_entries(entries, year, editor):
expedition_data = model_to_dict(le.expedition, fields=['year', 'name'])
entrydict = model_to_dict(le, fields=('slug', 'date', 'title', 'cave', 'place', 'other_people', 'time_underground', 'text'))
entrydict = model_to_dict(le, fields=('slug', 'date', 'title', 'place', 'other_people', 'time_underground', 'text'))
entrydict['author'] = author_data
entrydict['trippersons'] = participants
entrydict['expedition'] = expedition_data
if le.cave:
cave_data = model_to_dict(le.cave, fields=['areacode', 'kataster_number', 'unofficial_number'])
entrydict['cave'] = cave_data
return entrydict
dirpath = settings.EXPOWEB / "years" / year / LOGBOOK_ENTRIES
dirpath = settings.EXPOWEB / "years" / year / settings.JSON_LOG_ENTRIES
for le in entries:
# filename = f"{le.slug}-{le.pk:03}.json"