def test_label_tag_with_read_only_field(self):
        """Testing ChangeFormField.label_tag with read-only field"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(form_row=row,
                                admin_field=AdminReadonlyField(
                                    self.form,
                                    field='username',
                                    is_first=True,
                                    model_admin=self.model_admin))
        html = field.label_tag()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html, '<label class="rb-c-form-field__label">Username:</label>')
    def test_render_with_read_only_field(self):
        """Testing ChangeFormField.render with read-only field"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(
            form_row=row,
            admin_field=AdminReadonlyField(self.form,
                                           field='username',
                                           is_first=True,
                                           model_admin=self.model_admin))
        html = field.render()

        self.assertIsInstance(html, SafeText)
        self.assertHTMLEqual(
            html,
            '<div class="rb-c-form-field__readonly-value">test-user</div>')
    def test_init_with_read_only(self):
        """Testing ChangeFormField.__init__ with read-only field"""
        row = ChangeFormRow(self.form, field='username')
        field = ChangeFormField(
            form_row=row,
            admin_field=AdminReadonlyField(self.form,
                                           field='username',
                                           is_first=True,
                                           model_admin=self.model_admin))

        self.assertEqual(field.classes, 'rb-c-form-field -is-read-only')
        self.assertTrue(field.is_first)
        self.assertTrue(field.is_readonly)
        self.assertFalse(field.has_field_first)
        self.assertFalse(field.is_checkbox)
        self.assertFalse(field.show_errors)
Пример #4
0
 def __iter__(self):
     for i, field in enumerate(self.fields):
         if field in self.readonly_fields:
             yield AdminReadonlyField(
                 self.form,
                 field,
                 is_first=(i == 0),
                 model_admin=self.model_admin
             )
         else:
             yield AdminField(
                 self.form,
                 field,
                 is_first=(i == 0),
                 prepopulated_fields=self.prepopulated_fields  # noqa
             )
Пример #5
0
    def test_field_contents_foreign_linked(self):
        country = Country(pk=1, name='France')
        city = City(pk=1, name='Paris', country=country)

        ma = CityAdmin(City, admin.site)

        # Create form
        request = None
        form = ma.get_form(request, city)
        form.instance = city
        ro_field = AdminReadonlyField(form, 'country', True, ma)

        self.assertEqual(country.name, field_contents_foreign_linked(ro_field))

        # Now it should return as link
        ro_field.model_admin.linked_readonly_fields = ('country', )
        assert admin_url(country) in field_contents_foreign_linked(ro_field)