Skip to content

Commit

Permalink
apps/plans: fix topic migration failing without django-multiselectfield
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Feb 20, 2024
1 parent 896a7b8 commit d3d9733
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 5 additions & 1 deletion changelog/7778.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit d3d9733

Please sign in to comment.