Update API model conversion

This commit is contained in:
Tim Van Baak 2023-07-27 12:55:06 -07:00
parent 0ae5a77844
commit 7e0a95317c
3 changed files with 32 additions and 8 deletions

View File

@ -422,6 +422,34 @@ def wsgi():
#
def to_api_model(item: Item) -> dict:
"""
Convert an item to a JSON representation for the frontend.
"""
result = {
"source": item.source.source_name,
"id": item["id"],
"created": item["created"],
"active": item["active"],
}
for unchanged_field in (
"title",
"author",
"body",
"link",
"time",
"tags",
"tts",
"ttl",
"ttd",
):
if val := item.get(unchanged_field):
result[unchanged_field] = val
if actions := item.get("action"):
result["actions"] = list(actions.keys())
return result
@app.get("/api/v1/items")
# @auth_check
def items():
@ -449,9 +477,9 @@ def items():
response = jsonify({"error": "One of channel and source may be specified"})
response.status_code = 400
return response
source_names = []
# If the channel was specified, load the channel defs to get the sources
if filter_channel:
channels_config_path = data_path / "channels.json"
@ -485,7 +513,7 @@ def items():
for item in source.get_all_items()
if not item.is_hidden or show_hidden
],
key=item_sort_key
key=item_sort_key,
)
# Apply paging filters
@ -496,7 +524,7 @@ def items():
# Return the result set
response_params = {
"count": len(paged_items),
"items": list(map(lambda item: item.as_json(), paged_items)),
"items": list(map(to_api_model, paged_items)),
}
if page > 0:
response_params["prev"] = page - 1

View File

@ -53,7 +53,6 @@ def update_crontab_entries(data_path: Path):
section_found = False
in_section = False
for i in range(len(crontab_lines)):
if not section_found and crontab_lines[i] == INTAKE_CRON_BEGIN:
section_found = True
in_section = True

View File

@ -109,9 +109,6 @@ class Item:
def serialize(self, indent=True):
return json.dumps(self._item, indent=2 if indent else None)
def as_json(self):
return {"source": self.source.source_name, **self._item}
def update_from(self, updated: "Item") -> None:
for field in (
"title",