Repository: openfun/richie

Issue:
Person plugin form list every pages, not only Person pages
## Bug Report

**Expected behavior/code**
Select box in PersonPlugin form should list only extended page with Person model.

**Actual Behavior**
Currently the select box is listing every CMS pages.

**Steps to Reproduce**
1. Edit a page;
2. Try to add a Person plugin into a placeholder which allow it;
3. Click to open the select box from opened form for added Person plugin.

**Environment**
- Richie version: 0.1.0 (from my own branch synchronized from master 200c8a3) 
- Platform: Ubuntu 18.04 LTS

**Possible Solution**
Adding a filter inside plugin form machinery to retain only the extend page with Person.



Discussion:

Resolving patch:
diff --git a/src/richie/apps/persons/models.py b/src/richie/apps/persons/models.py
--- a/src/richie/apps/persons/models.py
+++ b/src/richie/apps/persons/models.py
@@ -86,7 +86,7 @@ class PersonPluginModel(CMSPlugin):
     to their Person instance
     """
 
-    page = models.ForeignKey(Page)
+    page = models.ForeignKey(Page, limit_choices_to={"person__isnull": False})
 
     class Meta:
         verbose_name = _("person plugin model")


Test patch:
diff --git a/src/richie/apps/persons/tests/test_person_plugin.py b/tests/apps/persons/test_person_plugin.py
similarity index 80%
rename from src/richie/apps/persons/tests/test_person_plugin.py
rename to tests/apps/persons/test_person_plugin.py
--- a/src/richie/apps/persons/tests/test_person_plugin.py
+++ b/tests/apps/persons/test_person_plugin.py
@@ -2,17 +2,19 @@
 """
 Unit tests for the Person plugin and its model
 """
+from django import forms
+from django.conf import settings
 from django.test import TestCase
 
-from cms.api import add_plugin
+from cms.api import add_plugin, create_page
 from cmsplugin_plain_text.cms_plugins import PlaintextPlugin
 from djangocms_picture.cms_plugins import PicturePlugin
 
 from richie.apps.core.factories import FilerImageFactory, UserFactory
 from richie.apps.core.helpers import create_i18n_page
-
-from ..cms_plugins import PersonPlugin
-from ..factories import PersonFactory
+from richie.apps.persons.cms_plugins import PersonPlugin
+from richie.apps.persons.factories import PersonFactory
+from richie.apps.persons.models import PersonPluginModel
 
 
 class PersonPluginTestCase(TestCase):
@@ -20,6 +22,24 @@ class PersonPluginTestCase(TestCase):
     Test that PersonPlugin correctly displays a Person's page placeholders content
     """
 
+    def test_person_plugin_form_page_choices(self):
+        """
+        The form to create a person plugin should only list person pages in the select box.
+        """
+
+        class PersonPluginModelForm(forms.ModelForm):
+            class Meta:
+                model = PersonPluginModel
+                exclude = ()
+
+        person = PersonFactory()
+        other_page_title = "other page"
+        create_page(other_page_title, "richie/fullwidth.html", settings.LANGUAGE_CODE)
+        plugin_form = PersonPluginModelForm()
+        print(plugin_form)
+        self.assertIn(person.get_full_name(), plugin_form.as_table())
+        self.assertNotIn(other_page_title, plugin_form.as_table())
+
     def test_person_plugin_render(self):
         """
         Test that a PersonPlugin correctly renders person's page specific information