2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-02-08 16:12:36 +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: