Skip to content

Commit

Permalink
apps/projects: add topics as m2m relation
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk authored and m4ra committed Dec 4, 2023
1 parent 8bb42e3 commit ec5c807
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ lint:
$(VIRTUAL_ENV)/bin/isort --diff -c $(SOURCE_DIRS) || EXIT_STATUS=$$?; \
$(VIRTUAL_ENV)/bin/flake8 $(SOURCE_DIRS) --exclude migrations,settings || EXIT_STATUS=$$?; \
npm run lint || EXIT_STATUS=$$?; \
$(VIRTUAL_ENV)/bin/python manage.py makemigrations --dry-run --check --noinput || EXIT_STATUS=$$?; \
exit $${EXIT_STATUS}

.PHONY: lint-quick
lint-quick:
EXIT_STATUS=0; \
npm run lint-staged || EXIT_STATUS=$$?; \
$(VIRTUAL_ENV)/bin/python manage.py makemigrations --dry-run --check --noinput || EXIT_STATUS=$$?; \
exit $${EXIT_STATUS}

.PHONY: lint-python-files
lint-python-files:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.2 on 2023-11-29 13:18

import adhocracy4.projects.fields
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("a4projects", "0041_ckeditor5_iframes"),
]

operations = [
migrations.CreateModel(
name="Topic",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("code", models.CharField(blank=True, max_length=10)),
("name", models.CharField(max_length=120, verbose_name="Topic")),
],
),
migrations.AddField(
model_name="project",
name="m2mtopics",
field=models.ManyToManyField(to="a4projects.topic"),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2 on 2023-11-29 13:20

from django.db import migrations
from django.conf import settings


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")
for project in project.objects.all():
for topic_code in project.topics:
project.m2mtopics.create(
code=topic_code,
name=[item[1] for item in topicsenum if item[0] == topic_code][0],
)
else:
pass


def reverse_func(apps, schema_editor):
if hasattr(settings, "A4_PROJECT_TOPICS"):
project = apps.get_model("a4projects", "Project")
for project in project.objects.all():
for topic in project.m2mtopics.all():
project.m2mtopics.remove(topic)
else:
pass


class Migration(migrations.Migration):
dependencies = [
("a4projects", "0042_topic_alter_project_topics_project_m2mtopics"),
]

operations = [
migrations.RunPython(add_topics_to_m2m_table, reverse_func),
]
16 changes: 16 additions & 0 deletions adhocracy4/projects/migrations/0044_remove_project_field_topics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 4.2 on 2023-11-29 15:03

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("a4projects", "0043_migrate_topics_to_m2m_topics"),
]

operations = [
migrations.RemoveField(
model_name="project",
name="topics",
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Django 4.2 on 2023-11-29 15:14

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("a4projects", "0044_remove_project_field_topics"),
]

operations = [
migrations.RenameField(
model_name="project", old_name="m2mtopics", new_name="topics"
),
]
17 changes: 13 additions & 4 deletions adhocracy4/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@
from adhocracy4.models import base

from .enums import Access
from .fields import TopicField
from .utils import get_module_clusters
from .utils import get_module_clusters_dict


class Topic(models.Model):
code = models.CharField(blank=True, max_length=10)
name = models.CharField(
max_length=120,
verbose_name=_("Topic"),
)

def __str__(self):
return self.name


class ProjectManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
Expand Down Expand Up @@ -259,9 +269,8 @@ class Project(
"dashboard."
),
)
topics = TopicField(
verbose_name=_("Project topics"), help_text=_("Add topics to your project.")
)
topics = models.ManyToManyField(Topic)

project_type = models.CharField(
blank=True, max_length=256, default="a4projects.Project"
)
Expand Down
6 changes: 6 additions & 0 deletions changelog/0007.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changed

- apps/projects:
add topics model/table
make topics a m2m relation to projects
stop making use of django-multiselectfield as it's not maintained

0 comments on commit ec5c807

Please sign in to comment.