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

parsing JSON logentries OK as valid

This commit is contained in:
2025-11-22 13:50:50 +02:00
parent 2807ed5c21
commit e2c1bc3516
2 changed files with 53 additions and 40 deletions

View File

@@ -411,9 +411,9 @@ def write_entries(entries, year, editor):
raise e
def serialize_logentry(le):
# REPLACE this with hand-built serializer which includes .author, .who which were added to the entries but re not in the model Class directly
# see below for Gemini code to do that. Going to bed now.
"""When doing JSON output of objects which have foreign keys to other objects in the database,
we need to use a custom serializer instead of just "json.dump()
"""
author_link = PersonLogEntry.objects.select_related('personexpedition').get(
logbook_entry=le,
is_logbook_entry_author=True
@@ -428,11 +428,11 @@ def write_entries(entries, year, editor):
for pl in participants_links:
participants.append(pl.personexpedition.person)
author_data = model_to_dict(author, fields=['id', 'slug'])
author_data = model_to_dict(author, fields=['id', 'slug', 'nickname_used'])
participants_data = []
for p in participants:
participants_data.append(model_to_dict(p, fields=['id', 'slug']))
participants_data.append(model_to_dict(p, fields=['id', 'slug', 'nickname_used']))
entrydict = model_to_dict(le, fields=('slug', 'date', 'expedition', 'title', 'cave', 'place', 'other_people', 'time_underground', 'text'))
entrydict['author'] = author_data
@@ -444,7 +444,6 @@ def write_entries(entries, year, editor):
for le in entries:
filename = f"{le.slug}-{le.pk:03}.json"
filepath = dirpath / filename
# description = f" {le.slug} :: {le.date} - {le.title}"
ensure_dir_exists(filepath)
entrydict = serialize_logentry(le)
@@ -456,28 +455,3 @@ def write_entries(entries, year, editor):
return True
def export_entry_with_author_details(request, entry_id):
# 3. Manually create the nested dictionary structure
# Use model_to_dict for easy extraction of the simple fields
# Author data (specify fields you want to expose)
author_data = model_to_dict(author, fields=['id', 'first_name', 'last_name', 'email'])
# Entry data (specify fields you want to expose)
entry_data = model_to_dict(entry, fields=['id', 'title', 'content', 'date_created'])
# Nest the author data inside the entry data
entry_data['author'] = author_data
# Add data from the intermediate model if needed (e.g., the date the person was added)
entry_data['author_assignment_date'] = author_link.date_assigned.isoformat()
# 4. Return the custom dictionary using JsonResponse
return JsonResponse(
entry_data,
encoder=CustomJSONEncoder,
safe=False # Set to True if entry_data was a list/QuerySet
)