Repository: liqd/a4-meinberlin

Issue:
changing group of plan in django does not work
I cannot change the group of a plan in django. Dropdown is there but changes won't be saved.


Discussion:

Resolving patch:
diff --git a/meinberlin/apps/plans/admin.py b/meinberlin/apps/plans/admin.py
--- a/meinberlin/apps/plans/admin.py
+++ b/meinberlin/apps/plans/admin.py
@@ -4,7 +4,7 @@
 
 
 @admin.register(models.Plan)
-class OfflineEventAdmin(admin.ModelAdmin):
+class PlanAdmin(admin.ModelAdmin):
     list_display = ('__str__', 'organisation', 'created')
 
     date_hierarchy = 'created'
diff --git a/meinberlin/apps/plans/forms.py b/meinberlin/apps/plans/forms.py
--- a/meinberlin/apps/plans/forms.py
+++ b/meinberlin/apps/plans/forms.py
@@ -39,6 +39,14 @@ def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.fields['district'].empty_label = _('City wide')
 
+    def save(self, commit=True):
+        plan = super().save(commit=False)
+        group = plan._get_group(plan.creator, plan.organisation)
+        plan.group = group
+        if commit:
+            plan.save()
+        return plan
+
 
 class CustomMultipleChoiceField(forms.ModelMultipleChoiceField):
 
diff --git a/meinberlin/apps/plans/models.py b/meinberlin/apps/plans/models.py
--- a/meinberlin/apps/plans/models.py
+++ b/meinberlin/apps/plans/models.py
@@ -148,7 +148,6 @@ def get_absolute_url(self):
 
     def save(self, *args, **kwargs):
         self.description = transforms.clean_html_field(self.description)
-        self.group = self._get_group(self.creator, self.organisation)
         super().save(*args, **kwargs)
 
     def _get_group(self, user, organisation):


Test patch:
diff --git a/meinberlin/test/factories/plans.py b/meinberlin/test/factories/plans.py
--- a/meinberlin/test/factories/plans.py
+++ b/meinberlin/test/factories/plans.py
@@ -13,6 +13,7 @@ class Meta:
     title = factory.Faker('sentence')
     creator = factory.SubFactory(a4_factories.USER_FACTORY)
     organisation = factory.SubFactory(a4_factories.ORGANISATION_FACTORY)
+    group = factory.SubFactory(a4_factories.GroupFactory)
     district = factory.SubFactory(AdministrativeDistrictFactory)
     point = {
         'type': 'Feature',
diff --git a/tests/conftest.py b/tests/conftest.py
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -12,6 +12,7 @@
 from meinberlin.test.factories.bplan import BplanFactory
 from meinberlin.test.factories.extprojects import ExternalProjectFactory
 from meinberlin.test.factories.organisations import OrganisationFactory
+from meinberlin.test.factories.plans import PlanFactory
 from meinberlin.test.factories.projectcontainers import ProjectContainerFactory
 
 
@@ -41,6 +42,7 @@ def clear_caches():
 register(factories.CategoryFactory)
 register(ActivityFactory)
 register(ProjectContainerFactory)
+register(PlanFactory)
 register(ExternalProjectFactory)
 register(BplanFactory)
 
diff --git a/tests/organisations/test_admin.py b/tests/organisations/test_admin.py
--- a/tests/organisations/test_admin.py
+++ b/tests/organisations/test_admin.py
@@ -3,6 +3,7 @@
 
 from adhocracy4.projects.models import Project
 from meinberlin.apps.organisations.models import Organisation
+from meinberlin.apps.plans.models import Plan
 
 
 @pytest.mark.django_db
@@ -75,6 +76,7 @@ def test_organisation_admin_form(client, user_factory, group_factory):
 def test_group_removal(client,
                        organisation,
                        project_factory,
+                       plan_factory,
                        user_factory,
                        group_factory):
 
@@ -82,9 +84,12 @@ def test_group_removal(client,
     group2 = group_factory()
     organisation.groups.add(group1)
     project = project_factory(group=group1, organisation=organisation)
+    plan = plan_factory(group=group1, organisation=organisation)
 
     assert Project.objects.all().count() == 1
     assert Project.objects.get(slug=project.slug).group == group1
+    assert Plan.objects.all().count() == 1
+    assert Plan.objects.get(id=plan.id).group == group1
 
     admin = user_factory(is_superuser=True, is_staff=True)
     client.force_login(admin)
@@ -99,3 +104,4 @@ def test_group_removal(client,
 
     assert response.status_code == 302
     assert Project.objects.get(slug=project.slug).group is None
+    assert Plan.objects.get(id=plan.id).group is None