Repository: liqd/a4-meinberlin

Issue:
project_list in container add project form contains all projects
@CarolingerSeilchenspringer at the moment all projects (not only the ones belonging to the organisation) can be added to a container. Should that stay that way?



Discussion:

Resolving patch:
diff --git a/meinberlin/apps/projectcontainers/forms.py b/meinberlin/apps/projectcontainers/forms.py
--- a/meinberlin/apps/projectcontainers/forms.py
+++ b/meinberlin/apps/projectcontainers/forms.py
@@ -62,12 +62,20 @@ class Meta:
 class ContainerProjectsForm(ProjectDashboardForm):
 
     def __init__(self, *args, **kwargs):
+        self.user = kwargs.pop('user')
+        self.organisation = kwargs.pop('organisation')
         super().__init__(*args, **kwargs)
 
-        # We tried to find a good balance between a short list and
-        # all necessary projects. The details may change over time.
-        # Projects that are already selected should remain in the queryset.
-        self.fields['projects'].queryset = self.fields['projects'].queryset \
+        projects = self.fields['projects']\
+            .queryset.filter(organisation=self.organisation)
+        if not self.organisation.has_initiator(self.user):
+            user_groups = self.user.groups.all()
+            org_groups = self.organisation.groups.all()
+            shared_groups = user_groups & org_groups
+            group = shared_groups.distinct().first()
+            projects = projects.filter(group=group)
+
+        self.fields['projects'].queryset = projects \
             .filter(projectcontainer=None)\
             .filter(Q(containers=self.instance) |
                     (Q(containers=None) &
diff --git a/meinberlin/apps/projectcontainers/views.py b/meinberlin/apps/projectcontainers/views.py
--- a/meinberlin/apps/projectcontainers/views.py
+++ b/meinberlin/apps/projectcontainers/views.py
@@ -52,6 +52,12 @@ def project(self):
     def get_object(self, queryset=None):
         return self.project
 
+    def get_form_kwargs(self):
+        kwargs = super().get_form_kwargs()
+        kwargs['user'] = self.request.user
+        kwargs['organisation'] = self.project.organisation
+        return kwargs
+
 
 class ContainerListView(dashboard_mixins.DashboardBaseMixin,
                         generic.ListView):


Test patch:
diff --git a/tests/projectcontainers/dashboard_components/test_views_project_container_projects.py b/tests/projectcontainers/dashboard_components/test_views_project_container_projects.py
--- a/tests/projectcontainers/dashboard_components/test_views_project_container_projects.py
+++ b/tests/projectcontainers/dashboard_components/test_views_project_container_projects.py
@@ -10,12 +10,13 @@
 @pytest.mark.django_db
 def test_edit_view(client, project_factory, project_container):
     initiator = project_container.organisation.initiators.first()
+    organisation = project_container.organisation
+    project = project_factory(is_public=True, organisation=organisation)
     url = component.get_base_url(project_container)
     client.login(username=initiator.email, password='password')
     response = client.get(url)
     assert_dashboard_form_component_response(response, component)
 
-    project = project_factory(is_public=True)
     data = {
         'projects': project.pk,
     }