Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration: themes as tags #16

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ecospheres/bouquets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix an unrelated issue.

r = api_destination._get(f"/api/1/organization/{source_data['organization']['id']}/")
if r.ok:
destination_data["organization"] = source_data["organization"]
Expand Down
45 changes: 45 additions & 0 deletions ecospheres/migrations/20250106_1_themes_as_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Copies or move themes/subthemes from extras to slugified tags
Idempotent
"""
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, clean: 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']}...")
theme = compute_slug(bouquet["extras"]["ecospheres"]["theme"], "theme")
subtheme = compute_slug(bouquet["extras"]["ecospheres"]["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 clean:
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()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
minicli
PyYAML
awesome-slugify