mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-12 17:23:15 +00:00
Added admin_form_fields to Image model
In Django 1.7, creating forms from a model without specifying a list of fields is deprecated. This commit makes the get_image_form function look for a field called admin_form_fields on the Image model and use that for building the form.
This commit is contained in:
parent
01534c99c0
commit
ef8de6180a
3 changed files with 96 additions and 1 deletions
|
|
@ -19,6 +19,7 @@ def formfield_for_dbfield(db_field, **kwargs):
|
|||
def get_image_form(model):
|
||||
return modelform_factory(
|
||||
model,
|
||||
fields=model.admin_form_fields,
|
||||
formfield_callback=formfield_for_dbfield,
|
||||
# set the 'file' widget to a FileInput rather than the default ClearableFileInput
|
||||
# so that when editing, we don't get the 'currently: ...' banner which is
|
||||
|
|
|
|||
|
|
@ -67,6 +67,16 @@ class AbstractImage(models.Model, TagSearchable):
|
|||
focal_point_width = models.PositiveIntegerField(null=True, blank=True)
|
||||
focal_point_height = models.PositiveIntegerField(null=True, blank=True)
|
||||
|
||||
admin_form_fields = (
|
||||
'title',
|
||||
'file',
|
||||
'tags',
|
||||
'focal_point_x',
|
||||
'focal_point_y',
|
||||
'focal_point_width',
|
||||
'focal_point_height',
|
||||
)
|
||||
|
||||
def get_usage(self):
|
||||
return get_object_usage(self)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,13 +3,19 @@ import datetime
|
|||
from mock import MagicMock
|
||||
|
||||
from django.test import TestCase
|
||||
from django import template
|
||||
from django import template, forms
|
||||
from django.utils import six
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
|
||||
from taggit.forms import TagField, TagWidget
|
||||
|
||||
from wagtail.wagtailimages.utils import generate_signature, verify_signature
|
||||
from wagtail.wagtailimages.rect import Rect
|
||||
from wagtail.wagtailimages.formats import Format, get_image_format, register_image_format
|
||||
from wagtail.wagtailimages.models import Image as WagtailImage
|
||||
from wagtail.wagtailimages.forms import get_image_form
|
||||
from wagtail.wagtailimages.fields import WagtailImageField
|
||||
|
||||
from .utils import Image, get_test_image_file
|
||||
|
||||
|
|
@ -241,3 +247,81 @@ class TestRect(TestCase):
|
|||
def test_get_key(self):
|
||||
rect = Rect(100, 150, 200, 250)
|
||||
self.assertEqual(rect.get_key(), '150-200-100x100')
|
||||
|
||||
|
||||
class TestGetImageForm(TestCase):
|
||||
def test_fields(self):
|
||||
form = get_image_form(Image)
|
||||
|
||||
self.assertEqual(list(form.base_fields.keys()), [
|
||||
'title',
|
||||
'file',
|
||||
'tags',
|
||||
'focal_point_x',
|
||||
'focal_point_y',
|
||||
'focal_point_width',
|
||||
'focal_point_height',
|
||||
])
|
||||
|
||||
def test_fields_custom_image_model_without_admin_fields(self):
|
||||
class CustomImage(Image):
|
||||
caption = models.CharField(max_length=255)
|
||||
|
||||
form = get_image_form(CustomImage)
|
||||
|
||||
# Caption was not put in admin_form_fields so it will not appear in the form
|
||||
self.assertEqual(list(form.base_fields.keys()), [
|
||||
'title',
|
||||
'file',
|
||||
'tags',
|
||||
'focal_point_x',
|
||||
'focal_point_y',
|
||||
'focal_point_width',
|
||||
'focal_point_height',
|
||||
])
|
||||
|
||||
def test_fields_custom_image_model_with_admin_fields(self):
|
||||
class CustomImage(Image):
|
||||
caption = models.CharField(max_length=255)
|
||||
|
||||
admin_form_fields = Image.admin_form_fields + (
|
||||
'caption',
|
||||
)
|
||||
|
||||
form = get_image_form(CustomImage)
|
||||
|
||||
self.assertEqual(list(form.base_fields.keys()), [
|
||||
'title',
|
||||
'file',
|
||||
'tags',
|
||||
'focal_point_x',
|
||||
'focal_point_y',
|
||||
'focal_point_width',
|
||||
'focal_point_height',
|
||||
'caption',
|
||||
])
|
||||
|
||||
def test_file_field(self):
|
||||
form = get_image_form(Image)
|
||||
|
||||
self.assertIsInstance(form.base_fields['file'], WagtailImageField)
|
||||
self.assertIsInstance(form.base_fields['file'].widget, forms.FileInput)
|
||||
|
||||
def test_tags_field(self):
|
||||
form = get_image_form(Image)
|
||||
|
||||
self.assertIsInstance(form.base_fields['tags'], TagField)
|
||||
self.assertIsInstance(form.base_fields['tags'].widget, TagWidget)
|
||||
|
||||
def test_focal_point_fields(self):
|
||||
form = get_image_form(Image)
|
||||
|
||||
self.assertIsInstance(form.base_fields['focal_point_x'], forms.IntegerField)
|
||||
self.assertIsInstance(form.base_fields['focal_point_y'], forms.IntegerField)
|
||||
self.assertIsInstance(form.base_fields['focal_point_width'], forms.IntegerField)
|
||||
self.assertIsInstance(form.base_fields['focal_point_height'], forms.IntegerField)
|
||||
|
||||
self.assertIsInstance(form.base_fields['focal_point_x'].widget, forms.HiddenInput)
|
||||
self.assertIsInstance(form.base_fields['focal_point_y'].widget, forms.HiddenInput)
|
||||
self.assertIsInstance(form.base_fields['focal_point_width'].widget, forms.HiddenInput)
|
||||
self.assertIsInstance(form.base_fields['focal_point_height'].widget, forms.HiddenInput)
|
||||
|
|
|
|||
Loading…
Reference in a new issue