Skip to content

Commit

Permalink
feat: Improve get_templates method in Database.py
Browse files Browse the repository at this point in the history
The `get_templates` method in `Database.py` has been updated to improve the retrieval of templates from the database. The method now returns a dictionary of templates, with each template containing its corresponding plugin ID, name, settings, configurations, and steps. This change enhances the functionality and flexibility of the `get_templates` method.
  • Loading branch information
TheophileDiot committed Aug 7, 2024
1 parent 23856da commit 5ee844c
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/common/db/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3182,15 +3182,59 @@ def get_plugin_page(self, plugin_id: str) -> Optional[bytes]:

return page.data

def get_templates(self, plugin: Optional[str] = None) -> List[Dict[str, Any]]:
def get_templates(self, plugin: Optional[str] = None) -> Dict[str, dict]:
"""Get templates."""
with self._db_session() as session:
query = session.query(Templates).with_entities(Templates.id, Templates.plugin_id, Templates.name)

if plugin:
query = query.filter_by(plugin_id=plugin)

return [{"id": template.id, "plugin_id": template.plugin_id, "name": template.name} for template in query]
templates = {}
for template in query:
templates[template.id] = {"plugin_id": template.plugin_id, "name": template.name, "settings": {}, "configs": {}, "steps": {}}

steps_settings = {}
for setting in (
session.query(Template_settings)
.with_entities(Template_settings.setting_id, Template_settings.step_id, Template_settings.default, Template_settings.suffix)
.filter_by(template_id=template.id)
):
key = f"{setting.setting_id}_{setting.suffix}" if setting.suffix else setting.setting_id
templates[template.id]["settings"][key] = setting.default

if setting.step_id:
if setting.step_id not in steps_settings:
steps_settings[setting.step_id] = []
steps_settings[setting.step_id].append(key)

steps_configs = {}
for config in (
session.query(Template_custom_configs)
.with_entities(Template_custom_configs.step_id, Template_custom_configs.type, Template_custom_configs.name, Template_custom_configs.data)
.filter_by(template_id=template.id)
):
key = f"{config.type}/{config.name}.conf"
templates[template.id]["configs"][key] = config.data.decode("utf-8")

if config.step_id:
if config.step_id not in steps_configs:
steps_configs[config.step_id] = []
steps_configs[config.step_id].append(key)

for step in (
session.query(Template_steps)
.with_entities(Template_steps.id, Template_steps.title, Template_steps.subtitle)
.filter_by(template_id=template.id)
):
templates[template.id]["steps"][step.id] = {"title": step.title, "subtitle": step.subtitle}

if step.id in steps_settings:
templates[template.id]["steps"][step.id]["settings"] = steps_settings[step.id]
if step.id in steps_configs:
templates[template.id]["steps"][step.id]["configs"] = steps_configs[step.id]

return templates

def get_template_settings(self, template_id: str) -> Dict[str, Any]:
"""Get templates settings."""
Expand Down

0 comments on commit 5ee844c

Please sign in to comment.