diff --git a/ecospheres/bouquets.py b/ecospheres/bouquets.py index 9e799e7..b960e81 100644 --- a/ecospheres/bouquets.py +++ b/ecospheres/bouquets.py @@ -46,7 +46,7 @@ def copy(slug: str, source: str = "prod", destination: str = "demo"): destination_data["owner"] = source_data["owner"] else: print(f"Owner does not exist on {destination}") - elif source_data["owner"]: + elif source_data["organization"]: r = api_destination._get(f"/api/1/organization/{source_data['organization']['id']}/") if r.ok: destination_data["organization"] = source_data["organization"] diff --git a/ecospheres/migrations/20250106_1_themes_as_tags.py b/ecospheres/migrations/20250106_1_themes_as_tags.py new file mode 100644 index 0000000..8ea4086 --- /dev/null +++ b/ecospheres/migrations/20250106_1_themes_as_tags.py @@ -0,0 +1,50 @@ +""" +Copies or move themes/subthemes from extras to slugified tags +Idempotent when using move=False +""" +import json + +from minicli import cli, run +from slugify import slugify +from typing import Literal + +from ecospheres.api import DatagouvfrAPI + + +def compute_slug(value: str, prefix: Literal["theme", "subtheme"]) -> str: + return slugify(f"ecospheres-{prefix}-{value.lower()}") + + +@cli +def migrate(slug: str = "", dry_run: bool = False, move: bool = False, env: str = "demo"): + api = DatagouvfrAPI(env) + bouquets = api.get_bouquets() + bouquets = [b for b in bouquets if b["slug"] == slug] if slug else bouquets + for bouquet in bouquets: + print(f"--> Handling {bouquet['slug']}...") + original_theme = bouquet["extras"]["ecospheres"].get("theme") + original_subtheme = bouquet["extras"]["ecospheres"].get("subtheme") + if not original_theme or not original_subtheme: + print("No theme or subtheme to migrate, skipping.") + continue + theme = compute_slug(original_theme, "theme") + subtheme = compute_slug(original_subtheme, "subtheme") + tags = [ + *[t for t in bouquet["tags"] if compute_slug("", "theme") not in t and compute_slug("", "subtheme") not in t], + theme, + subtheme, + ] + payload = {"tags": tags} + if move: + payload["extras"] = bouquet["extras"] + payload["extras"]["ecospheres"].pop("theme", None) + payload["extras"]["ecospheres"].pop("subtheme", None) + if not dry_run: + api.put(f"/api/1/topics/{bouquet['id']}/", json=payload) + else: + print("Would have updated with:") + print(json.dumps(payload, indent=2)) + + +if __name__ == "__main__": + run() diff --git a/requirements.txt b/requirements.txt index e902eee..c9290ff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests minicli PyYAML +awesome-slugify