From 68cbb8d8b80d2f7c02d007151e6af3ac8024eade Mon Sep 17 00:00:00 2001 From: Mara Karagianni Date: Thu, 7 Dec 2023 15:19:35 +0200 Subject: [PATCH] projects/migrations: use topics enum instead of tuples --- adhocracy4/projects/fields.py | 10 ++++----- ..._alter_project_topics_project_m2mtopics.py | 1 - .../0043_migrate_topics_to_m2m_topics.py | 4 ++-- adhocracy4/projects/models.py | 10 ++++----- tests/project/enums.py | 22 +++++++++++++++++++ tests/project/settings.py | 6 +---- 6 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 tests/project/enums.py diff --git a/adhocracy4/projects/fields.py b/adhocracy4/projects/fields.py index e07a3523a..ff3b1beb3 100644 --- a/adhocracy4/projects/fields.py +++ b/adhocracy4/projects/fields.py @@ -1,8 +1,10 @@ -from django.conf import settings from multiselectfield import MultiSelectField class TopicField(MultiSelectField): + """Deprecated, don't use""" + + # TODO: remove once topic migrations are rolled out def __init__(self, *args, **kwargs): kwargs["max_length"] = 254 kwargs["max_choices"] = 2 @@ -11,11 +13,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def contribute_to_class(self, cls, name, **kwargs): - """Initialize the choices from the project's settings if they exist.""" - if hasattr(settings, "A4_PROJECT_TOPICS"): - self.choices = settings.A4_PROJECT_TOPICS - else: - self.choices = () + self.choices = () # Call the super method at last so that choices are already initialized super().contribute_to_class(cls, name, **kwargs) diff --git a/adhocracy4/projects/migrations/0042_topic_alter_project_topics_project_m2mtopics.py b/adhocracy4/projects/migrations/0042_topic_alter_project_topics_project_m2mtopics.py index f9a9f9ab3..4a3ffad7e 100644 --- a/adhocracy4/projects/migrations/0042_topic_alter_project_topics_project_m2mtopics.py +++ b/adhocracy4/projects/migrations/0042_topic_alter_project_topics_project_m2mtopics.py @@ -23,7 +23,6 @@ class Migration(migrations.Migration): ), ), ("code", models.CharField(blank=True, max_length=10, unique=True)), - ("name", models.CharField(max_length=120, verbose_name="Topic")), ], ), migrations.AddField( diff --git a/adhocracy4/projects/migrations/0043_migrate_topics_to_m2m_topics.py b/adhocracy4/projects/migrations/0043_migrate_topics_to_m2m_topics.py index d749efd2e..36c8a090c 100644 --- a/adhocracy4/projects/migrations/0043_migrate_topics_to_m2m_topics.py +++ b/adhocracy4/projects/migrations/0043_migrate_topics_to_m2m_topics.py @@ -1,4 +1,6 @@ # Generated by Django 4.2 on 2023-11-29 13:20 +import sys +import importlib from django.db import migrations from django.conf import settings @@ -6,14 +8,12 @@ def add_topics_to_m2m_table(apps, schema_editor): if hasattr(settings, "A4_PROJECT_TOPICS"): - topicsenum = settings.A4_PROJECT_TOPICS project = apps.get_model("a4projects", "Project") topic = apps.get_model("a4projects", "Topic") for project in project.objects.all(): for topic_code in project.topics: proj_topic, _ = topic.objects.get_or_create( code=topic_code, - name=[item[1] for item in topicsenum if item[0] == topic_code][0], ) project.m2mtopics.add(proj_topic) else: diff --git a/adhocracy4/projects/models.py b/adhocracy4/projects/models.py index 59fdc061b..13870b9bc 100644 --- a/adhocracy4/projects/models.py +++ b/adhocracy4/projects/models.py @@ -8,6 +8,7 @@ from django.urls import reverse from django.utils import timezone from django.utils.functional import cached_property +from django.utils.module_loading import import_string from django.utils.translation import gettext_lazy as _ from django_ckeditor_5.fields import CKEditor5Field from django_enumfield.enum import EnumField @@ -25,13 +26,12 @@ class Topic(models.Model): code = models.CharField(blank=True, max_length=10, unique=True) - name = models.CharField( - max_length=120, - verbose_name=_("Topic"), - ) def __str__(self): - return self.name + if hasattr(settings, "A4_PROJECT_TOPICS"): + topics_enum = import_string(settings.A4_PROJECT_TOPICS) + return str(topics_enum(self.code).label) + return self.code class ProjectManager(models.Manager): diff --git a/tests/project/enums.py b/tests/project/enums.py new file mode 100644 index 000000000..2f891b8bf --- /dev/null +++ b/tests/project/enums.py @@ -0,0 +1,22 @@ +# fmt: off + +from django.db import models +from django.utils.translation import gettext_lazy as _ + + +class TopicEnum(models.TextChoices): + """Choices for project topics.""" + + ANT = "ANT", _("Anti-discrimination"), + WOR = "WOR", _("Work & economy"), + BUI = "BUI", _("Building & living"), + EDU = "EDU", _("Education & research"), + CHI = "CHI", _("Children, youth & family"), + FIN = "FIN", _("Finances"), + HEA = "HEA", _("Health & sports"), + INT = "INT", _("Integration"), + CUL = "CUL", _("Culture & leisure"), + NEI = "NEI", _("Neighborhood & participation"), + URB = "URB", _("Urban development"), + ENV = "ENV", _("Environment & public green space"), + TRA = "TRA", _("Traffic") diff --git a/tests/project/settings.py b/tests/project/settings.py index ccd3dbe02..9d0da6ee1 100644 --- a/tests/project/settings.py +++ b/tests/project/settings.py @@ -226,11 +226,7 @@ }, } -A4_PROJECT_TOPICS = ( - ("ANT", "Anti-discrimination"), - ("WOR", "Work & economy"), - ("BUI", "Building & living"), -) +A4_PROJECT_TOPICS = "tests.project.enums.TopicEnum" A4_COMMENT_CATEGORIES = ( ("QUE", "Question"),