From 7e0a95317c74769dc99e4de8bd86649ec7cd6619 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 27 Jul 2023 12:55:06 -0700 Subject: [PATCH] Update API model conversion --- intake/app.py | 36 ++++++++++++++++++++++++++++++++---- intake/crontab.py | 1 - intake/source.py | 3 --- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/intake/app.py b/intake/app.py index 2542bd1..ed000e1 100644 --- a/intake/app.py +++ b/intake/app.py @@ -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 diff --git a/intake/crontab.py b/intake/crontab.py index 934a5c0..f48bc37 100644 --- a/intake/crontab.py +++ b/intake/crontab.py @@ -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 diff --git a/intake/source.py b/intake/source.py index 50b83af..e544919 100755 --- a/intake/source.py +++ b/intake/source.py @@ -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",