diff --git a/changelog/7778.md b/changelog/7778.md index 2f1c2f9042..0926dd3b8b 100644 --- a/changelog/7778.md +++ b/changelog/7778.md @@ -1,5 +1,9 @@ ### Added -- settings: move A4_PROJECT_TOPICS to Enum class +- settings: move A4_PROJECT_TOPICS to Enum class - apps/plans: topics as an m2m relation to plans, replacing django-multiselectfield - apps/projects: topics from TopicEnum, topic form and serializer for projects +- **Breaking Change** Plan topics with a code longer than 3 chars will be dropped. + If you need to keep them modify the check in + `meinberlin/apps/plans/migratiosn/0059_migrate_topics_to_m2m_topics.py` or + migrate them to shorter codes. Note: the max. possible length is still limited to 10. diff --git a/meinberlin/apps/plans/migrations/0059_migrate_topics_to_m2m_topics.py b/meinberlin/apps/plans/migrations/0059_migrate_topics_to_m2m_topics.py index 8057a56c1d..b2b40d34f9 100644 --- a/meinberlin/apps/plans/migrations/0059_migrate_topics_to_m2m_topics.py +++ b/meinberlin/apps/plans/migrations/0059_migrate_topics_to_m2m_topics.py @@ -1,19 +1,36 @@ # Generated by Django 4.2 on 2023-11-29 13:20 +import logging from django.db import migrations from django.conf import settings +logger = logging.getLogger(__name__) + def add_topics_to_m2m_table(apps, schema_editor): if hasattr(settings, "A4_PROJECT_TOPICS"): plans = apps.get_model("meinberlin_plans", "Plan") topic = apps.get_model("a4projects", "Topic") for plan in plans.objects.all(): - for topic_code in plan.topics: - plan_topic, _ = topic.objects.get_or_create( - code=topic_code, + try: + for topic_code in plan.topics.split(","): + if not topic_code: + continue + if len(topic_code) > 3: + logger.warning( + "warning: dropping unknown topic:" + + topic_code + + ". Max length is 3" + ) + continue + plan_topic, _ = topic.objects.get_or_create( + code=topic_code, + ) + plan.m2mtopics.add(plan_topic) + except Exception as e: + logger.warning( + "error migrating topics for plan " + plan.title + ": " + str(e) ) - plan.m2mtopics.add(plan_topic) else: pass