def test_render_js_init_with_can_choose_root(self): widget = widgets.AdminPageChooser(can_choose_root=True) js_init = widget.render_js_init('test-id', 'test', self.child_page) self.assertEqual( js_init, "createPageChooser(\"test-id\", [\"wagtailcore.page\"], %d, true, null);" % self.root_page.id )
def test_render_html_with_value(self): widget = widgets.AdminPageChooser() html = widget.render_html('test', self.child_page, {}) self.assertInHTML( """<input name="test" type="hidden" value="%d" />""" % self.child_page.id, html)
def test_render_js_init_with_user_perm(self): widget = widgets.AdminPageChooser(user_perms='copy_to') html = widget.render('test', None, {'id': 'test-id'}) self.assertIn( 'createPageChooser("test-id", null, {"model_names": ["wagtailcore.page"], "can_choose_root": false, "user_perms": "copy_to"});', html)
def test_render_js_init_with_can_choose_root(self): widget = widgets.AdminPageChooser(can_choose_root=True) html = widget.render('test', self.child_page, {'id': 'test-id'}) self.assertIn( 'createPageChooser("test-id", %d, {"model_names": ["wagtailcore.page"], "can_choose_root": true, "user_perms": null});' % self.root_page.id, html)
def test_adapt_with_target_model(self): widget = widgets.AdminPageChooser(target_models=[SimplePage, EventPage]) js_args = widgets.PageChooserAdapter().js_args(widget) self.assertEqual( js_args[2]["model_names"], ["tests.simplepage", "tests.eventpage"] )
def test_render_js_init(self): widget = widgets.AdminPageChooser() html = widget.render("test", None, {"id": "test-id"}) self.assertIn( 'createPageChooser("test-id", null, {"model_names": ["wagtailcore.page"], "can_choose_root": false, "user_perms": null});', html, )
def test_render_with_target_model(self): widget = widgets.AdminPageChooser(target_models=[SimplePage]) html = widget.render('test', None, {'id': 'test-id'}) self.assertIn('createPageChooser("test-id", null, {"model_names": ["tests.simplepage"], "can_choose_root": false, "user_perms": null});', html) html = widget.render('test', self.child_page, {'id': 'test-id'}) self.assertIn(">Choose a page (Simple Page)<", html)
def test_render_js_init(self): widget = widgets.AdminPageChooser() js_init = widget.render_js_init('test-id', 'test', None) self.assertEqual( js_init, "createPageChooser(\"test-id\", [\"wagtailcore.page\"], null, false, null);" )
def test_render_js_init_with_user_perm(self): widget = widgets.AdminPageChooser(user_perms='copy_to') js_init = widget.render_js_init('test-id', 'test', None) self.assertEqual( js_init, "createPageChooser(\"test-id\", [\"wagtailcore.page\"], null, false, \"copy_to\");" )
def test_render_js_init_with_target_model(self): widget = widgets.AdminPageChooser(target_models=[SimplePage]) js_init = widget.render_js_init('test-id', 'test', None) self.assertEqual(js_init, "createPageChooser(\"test-id\", [\"tests.simplepage\"], null, false, null);") html = widget.render_html('test', self.child_page, {}) self.assertIn(">Choose a page (Simple Page)<", html)
def test_render_js_init_with_user_perm(self): widget = widgets.AdminPageChooser(user_perms="copy_to") html = widget.render("test", None, {"id": "test-id"}) self.assertIn( 'new PageChooser("test-id", null, {"model_names": ["wagtailcore.page"], "can_choose_root": false, "user_perms": "copy_to"});', html, )
def test_render_html_with_value(self): widget = widgets.AdminPageChooser() html = widget.render_html('test', self.child_page, {}) self.assertInHTML( """<input name="test" type="hidden" value="%d" />""" % self.child_page.id, html) # SimplePage has a custom get_admin_display_title method which should be reflected here self.assertInHTML("foobarbaz (simple page)", html)
def widget_overrides(self): if self.page_type or self.can_choose_root: return { self.field_name: widgets.AdminPageChooser( target_models=self.page_type, can_choose_root=self.can_choose_root ) } else: return {}
def test_render_html(self): # render_html is mostly an internal API, but we do want to support calling it with None as # a value, to render a blank field without the JS initialiser (so that we can call that # separately in our own context and hold on to the return value) widget = widgets.AdminPageChooser() html = widget.render_html("test", None, {}) self.assertInHTML("""<input name="test" type="hidden" />""", html) self.assertIn(">Choose a page<", html)
def test_render_with_target_model_as_single_string(self): widget = widgets.AdminPageChooser(target_models="tests.SimplePage") html = widget.render("test", None, {"id": "test-id"}) self.assertIn( 'createPageChooser("test-id", null, {"model_names": ["tests.simplepage"], "can_choose_root": false, "user_perms": null});', html, ) html = widget.render("test", self.child_page, {"id": "test-id"}) self.assertIn(">Choose a page (Simple Page)<", html)
def __init__(self, *args, **kwargs): # CopyPage must be passed a 'page' kwarg indicating the page to be copied self.page = kwargs.pop('page') self.user = kwargs.pop('user', None) can_publish = kwargs.pop('can_publish') super().__init__(*args, **kwargs) self.fields['new_title'] = forms.CharField(initial=self.page.title, label=_("New title")) allow_unicode = getattr(settings, 'WAGTAIL_ALLOW_UNICODE_SLUGS', True) self.fields['new_slug'] = forms.SlugField(initial=self.page.slug, label=_("New slug"), allow_unicode=allow_unicode) self.fields['new_parent_page'] = forms.ModelChoiceField( initial=self.page.get_parent(), queryset=Page.objects.all(), widget=widgets.AdminPageChooser(can_choose_root=True, user_perms='copy_to'), label=_("New parent page"), help_text=_( "This copy will be a child of this given parent page.")) pages_to_copy = self.page.get_descendants(inclusive=True) subpage_count = pages_to_copy.count() - 1 if subpage_count > 0: self.fields['copy_subpages'] = forms.BooleanField( required=False, initial=True, label=_("Copy subpages"), help_text=ngettext("This will copy %(count)s subpage.", "This will copy %(count)s subpages.", subpage_count) % {'count': subpage_count}) if can_publish: pages_to_publish_count = pages_to_copy.live().count() if pages_to_publish_count > 0: # In the specific case that there are no subpages, customise the field label and help text if subpage_count == 0: label = _("Publish copied page") help_text = _( "This page is live. Would you like to publish its copy as well?" ) else: label = _("Publish copies") help_text = ngettext( "%(count)s of the pages being copied is live. Would you like to publish its copy?", "%(count)s of the pages being copied are live. Would you like to publish their copies?", pages_to_publish_count) % { 'count': pages_to_publish_count } self.fields['publish_copies'] = forms.BooleanField( required=False, initial=True, label=label, help_text=help_text)
def test_render_with_value(self): widget = widgets.AdminPageChooser() html = widget.render('test', self.child_page, {'id': 'test-id'}) self.assertInHTML("""<input id="test-id" name="test" type="hidden" value="%d" />""" % self.child_page.id, html) # SimplePage has a custom get_admin_display_title method which should be reflected here self.assertInHTML("foobarbaz (simple page)", html) self.assertIn( 'createPageChooser("test-id", %d, {"model_names": ["wagtailcore.page"], "can_choose_root": false, "user_perms": null});' % self.root_page.id, html )
def test_render_with_multiple_target_models(self): target_models = [SimplePage, "tests.eventpage"] widget = widgets.AdminPageChooser(target_models=target_models) html = widget.render("test", None, {"id": "test-id"}) self.assertIn( 'createPageChooser("test-id", null, {"model_names": ["tests.simplepage", "tests.eventpage"], "can_choose_root": false, "user_perms": null});', html, ) html = widget.render("test", self.child_page, {"id": "test-id"}) self.assertIn(">Choose a page<", html)
def test_adapt(self): widget = widgets.AdminPageChooser() js_args = widgets.PageChooserAdapter().js_args(widget) self.assertInHTML( """<input id="__ID__" name="__NAME__" type="hidden" />""", js_args[0]) self.assertIn(">Choose a page<", js_args[0]) self.assertEqual(js_args[1], '__ID__') self.assertEqual( js_args[2], { 'can_choose_root': False, 'model_names': ['wagtailcore.page'], 'user_perms': None })
def test_adapt(self): widget = widgets.AdminPageChooser() js_args = widgets.PageChooserAdapter().js_args(widget) self.assertInHTML( """<input id="__ID__" name="__NAME__" type="hidden" />""", js_args[0] ) self.assertIn(">Choose a page<", js_args[0]) self.assertEqual(js_args[1], "__ID__") self.assertEqual( js_args[2], { "can_choose_root": False, "model_names": ["wagtailcore.page"], "user_perms": None, }, )
class WorkflowPageForm(forms.ModelForm): page = forms.ModelChoiceField( queryset=Page.objects.all(), widget=widgets.AdminPageChooser(target_models=[Page], can_choose_root=True), ) class Meta: model = WorkflowPage fields = ["page"] def clean(self): page = self.cleaned_data.get("page") try: existing_workflow = page.workflowpage.workflow if not self.errors and existing_workflow != self.cleaned_data[ "workflow"]: # If the form has no errors, Page has an existing Workflow assigned, that Workflow is not # the selected Workflow, and overwrite_existing is not True, add a new error. This should be used to # trigger the confirmation message in the view. This is why this error is only added if there are no # other errors - confirmation should be the final step. self.add_error( "page", ValidationError( _("This page already has workflow '{0}' assigned."). format(existing_workflow), code="existing_workflow", ), ) except AttributeError: pass def save(self, commit=False): page = self.cleaned_data["page"] if commit: WorkflowPage.objects.update_or_create( page=page, defaults={"workflow": self.cleaned_data["workflow"]}, )
def __init__(self, *args, **kwargs): # CopyPage must be passed a 'page' kwarg indicating the page to be copied self.page = kwargs.pop("page") self.user = kwargs.pop("user", None) can_publish = kwargs.pop("can_publish") super().__init__(*args, **kwargs) self.fields["new_title"] = forms.CharField(initial=self.page.title, label=_("New title")) allow_unicode = getattr(settings, "WAGTAIL_ALLOW_UNICODE_SLUGS", True) self.fields["new_slug"] = forms.SlugField(initial=self.page.slug, label=_("New slug"), allow_unicode=allow_unicode) self.fields["new_parent_page"] = forms.ModelChoiceField( initial=self.page.get_parent(), queryset=Page.objects.all(), widget=widgets.AdminPageChooser(can_choose_root=True, user_perms="copy_to"), label=_("New parent page"), help_text=_( "This copy will be a child of this given parent page."), ) pages_to_copy = self.page.get_descendants(inclusive=True) subpage_count = pages_to_copy.count() - 1 if subpage_count > 0: self.fields["copy_subpages"] = forms.BooleanField( required=False, initial=True, label=_("Copy subpages"), help_text=ngettext( "This will copy %(count)s subpage.", "This will copy %(count)s subpages.", subpage_count, ) % {"count": subpage_count}, ) if can_publish: pages_to_publish_count = pages_to_copy.live().count() if pages_to_publish_count > 0: # In the specific case that there are no subpages, customise the field label and help text if subpage_count == 0: label = _("Publish copied page") help_text = _( "This page is live. Would you like to publish its copy as well?" ) else: label = _("Publish copies") help_text = ngettext( "%(count)s of the pages being copied is live. Would you like to publish its copy?", "%(count)s of the pages being copied are live. Would you like to publish their copies?", pages_to_publish_count, ) % { "count": pages_to_publish_count } self.fields["publish_copies"] = forms.BooleanField( required=False, initial=False, label=label, help_text=help_text) # Note that only users who can publish in the new parent page can create an alias. # This is because alias pages must always match their original page's state. self.fields["alias"] = forms.BooleanField( required=False, initial=False, label=_("Alias"), help_text=_("Keep the new pages updated with future changes"), )
def __init__(self, *args, **kwargs): # CopyPage must be passed a 'page' kwarg indicating the page to be copied self.page = kwargs.pop('page') self.user = kwargs.pop('user', None) can_publish = kwargs.pop('can_publish') super(CopyForm, self).__init__(*args, **kwargs) for lang in mt_settings.AVAILABLE_LANGUAGES: new_title_field = build_localized_fieldname("new_title", lang) title_field = build_localized_fieldname("title", lang) new_slug_field = build_localized_fieldname("new_slug", lang) slug_field = build_localized_fieldname("slug", lang) if getattr(self.page, title_field): self.fields[new_title_field] = forms.CharField( initial=getattr(self.page, title_field), label=_("New title [{}]".format(lang))) self.fields[new_slug_field] = forms.SlugField( initial=getattr(self.page, slug_field), label=_("New slug [{}]".format(lang))) self.fields['new_parent_page'] = forms.ModelChoiceField( initial=self.page.get_parent(), queryset=Page.objects.all(), widget=widgets.AdminPageChooser(can_choose_root=True, user_perms='copy_to'), label=_("New parent page"), help_text=_( "This copy will be a child of this given parent page.")) pages_to_copy = self.page.get_descendants(inclusive=True) subpage_count = pages_to_copy.count() - 1 if subpage_count > 0: self.fields['copy_subpages'] = forms.BooleanField( required=False, initial=True, label=_("Copy subpages"), help_text=ungettext("This will copy %(count)s subpage.", "This will copy %(count)s subpages.", subpage_count) % {'count': subpage_count}) if can_publish: pages_to_publish_count = pages_to_copy.live().count() if pages_to_publish_count > 0: # In the specific case that there are no subpages, customise the field label and help text if subpage_count == 0: label = _("Publish copied page") help_text = _( "This page is live. Would you like to publish its copy as well?" ) else: label = _("Publish copies") help_text = ungettext( "%(count)s of the pages being copied is live. Would you like to publish its copy?", "%(count)s of the pages being copied are live. Would you like to publish their copies?", pages_to_publish_count) % { 'count': pages_to_publish_count } self.fields['publish_copies'] = forms.BooleanField( required=False, initial=True, label=label, help_text=help_text)
def widget_overrides(self): return { self.field_name: widgets.AdminPageChooser(target_models=self.target_models(), can_choose_root=self.can_choose_root) }
def widget_overrides(cls): return { cls.field_name: widgets.AdminPageChooser(target_models=cls.target_models(), can_choose_root=cls.can_choose_root) }
def test_render_html(self): widget = widgets.AdminPageChooser() html = widget.render_html('test', None, {}) self.assertInHTML("""<input name="test" type="hidden" />""", html) self.assertIn(">Choose a page<", html)
def test_not_hidden(self): widget = widgets.AdminPageChooser() self.assertFalse(widget.is_hidden)
override={"widget": widgets.AdminDateTimeInput}) # Auto-height text fields (defined as exact_class=True so that it doesn't take effect for RichTextField) register_form_field_override( models.TextField, override={"widget": widgets.AdminAutoHeightTextInput}, exact_class=True, ) # Page chooser register_form_field_override( models.ForeignKey, to=Page, override=lambda db_field: { "widget": widgets.AdminPageChooser(target_models=[db_field.remote_field.model]) }, ) # Tag fields register_form_field_override( TaggableManager, override=(lambda db_field: { "form_class": TagField, "tag_model": db_field.related_model }), ) # Callback to allow us to override the default form fields provided for each model field. def formfield_for_dbfield(db_field, **kwargs):
def test_adapt_with_can_choose_root(self): widget = widgets.AdminPageChooser(can_choose_root=True) js_args = widgets.PageChooserAdapter().js_args(widget) self.assertTrue(js_args[2]["can_choose_root"])