summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLauréline Guérin <zebuline@entrouvert.com>2019-10-22 08:01:29 (GMT)
committerLauréline Guérin <zebuline@entrouvert.com>2019-10-28 08:03:37 (GMT)
commit8f0bc9a0599e54ca66e666358466daba453797c1 (patch)
tree10f06ada46087d1aa32744f323f64213db1d83e0
parent3be27ce16bc73378dae0350a3578c11c49017bc8 (diff)
downloadcombo-wip/37111-groups-init.zip
combo-wip/37111-groups-init.tar.gz
combo-wip/37111-groups-init.tar.bz2
page: fix initial values for groups in visibility form (#37111)wip/37111-groups-init
-rw-r--r--combo/manager/forms.py9
-rw-r--r--tests/test_pages.py18
2 files changed, 22 insertions, 5 deletions
diff --git a/combo/manager/forms.py b/combo/manager/forms.py
index 9ca5441..97f6d82 100644
--- a/combo/manager/forms.py
+++ b/combo/manager/forms.py
@@ -65,15 +65,14 @@ class PageEditPictureForm(forms.ModelForm):
class PageVisibilityForm(forms.ModelForm):
- groups = forms.MultipleChoiceField(
- label=_('Groups'),
- required=False,
- choices=get_groups_as_choices)
-
class Meta:
model = Page
fields = ('public', 'groups')
+ def __init__(self, *args, **kwargs):
+ super(PageVisibilityForm, self).__init__(*args, **kwargs)
+ self.fields['groups'].queryset = Group.objects.order_by('name')
+
class PageSelectTemplateForm(forms.ModelForm):
class Meta:
diff --git a/tests/test_pages.py b/tests/test_pages.py
index b8f40a9..158aba3 100644
--- a/tests/test_pages.py
+++ b/tests/test_pages.py
@@ -12,6 +12,7 @@ from django.utils.timezone import now
from combo.data.models import Page, CellBase, TextCell, LinkCell
from combo.data.management.commands.import_site import Command as ImportSiteCommand
from combo.data.management.commands.export_site import Command as ExportSiteCommand
+from combo.manager.forms import PageVisibilityForm
pytestmark = pytest.mark.django_db
@@ -123,6 +124,23 @@ def test_page_visibility():
assert page.is_visible(user1)
assert not page.is_visible(user2)
+
+def test_page_visibility_groups_init():
+ page = Page.objects.create()
+ group1 = Group.objects.create(name='foobar')
+ group2 = Group.objects.create(name='another group')
+
+ form = PageVisibilityForm(instance=page)
+ assert form['groups'].initial == []
+
+ page.groups.set([group1])
+ form = PageVisibilityForm(instance=page)
+ assert form['groups'].initial == [group1]
+
+ # test sorting
+ assert list(form.fields['groups'].queryset) == [group2, group1]
+
+
def test_import_export_pages():
page = Page(title=u'foo', slug='foo', order=0, description="Foo's page")
page.save()