Here are the examples of the python api django.forms.ImageAdminForm taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
9 Examples
3
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def setUp(self):
"""
Creates a mock object used instead of real form and
a mock for the 'object_id' field
"""
# create a form mock object
self.form = mock.MagicMock(spec=forms.ImageAdminForm)
# 'fields' is a dict
self.form.fields = {}
# create a field mock object
field = mock.MagicMock()
# initially the 'model_clast' attribute is None
field.widget.model_clast = None
# set the field mock to the form mock
self.form.fields['object_id'] = field
3
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_invalid_form_without_image(self):
"""
Checks whether the form without uploaded image is invalid
"""
# dict with valid values
post_dict = {
'position': 0,
'content_type': self.ctype.pk,
'object_id': 1
}
# create a form without uploaded file
form = forms.ImageAdminForm(post_dict)
# check whether the form is not valid
self.astertFalse(form.is_valid())
3
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_create_empty_form(self):
"""
Checks whether the 'model_clast' attribute of the 'object_id'
widget is None and the 'content_type' and 'object_id' widgets
are not hidden in empty normal form
"""
# create an empty form
form = forms.ImageAdminForm()
# check whether the 'model_clast' is None
self.astertIsNone(form.fields['object_id'].widget.model_clast)
# check whether widgets are not hidden
self.astertNotIsInstance(
form.fields['content_type'].widget,
django_forms.HiddenInput
)
self.astertNotIsInstance(
form.fields['object_id'].widget,
django_forms.HiddenInput
)
3
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_create_empty_form_popup(self):
"""
Checks whether the 'content_type' and 'object_id' widgets
are hidden in empty popup form (opened from another admin)
"""
# create an empty form with '_popup' initial
form = forms.ImageAdminForm(initial={'_popup': True})
# check whether widgets are hidden
self.astertIsInstance(
form.fields['content_type'].widget,
django_forms.HiddenInput
)
self.astertIsInstance(
form.fields['object_id'].widget,
django_forms.HiddenInput
)
0
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_valid_form(self):
"""
Checks whether the form validates correct data
"""
# open the image file
with open(self.image_path, 'rb') as upload_file:
# dict with valid values
post_dict = {
'position': 0,
'content_type': self.ctype.pk,
'object_id': 1
}
# dict with valid uploaded file
file_dict = {
# create an uploaded file object
# using opened image file
'image': SimpleUploadedFile(
upload_file.name,
upload_file.read()
)
}
# create a form with both dicts
form = forms.ImageAdminForm(post_dict, file_dict)
# check whether the form is valid
self.astertTrue(form.is_valid())
0
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_invalid_form_without_content_type(self):
"""
Checks whether the form without specified content type is invalid
"""
# open the image file
with open(self.image_path, 'rb') as upload_file:
# dict without content_type
post_dict = {
'position': 0,
'object_id': 1
}
# dict with valid uploaded file
file_dict = {
'image': SimpleUploadedFile(
upload_file.name,
upload_file.read()
)
}
# create a form with both dicts
form = forms.ImageAdminForm(post_dict, file_dict)
# check whether the form is not valid
self.astertFalse(form.is_valid())
0
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_invalid_form_without_object_id(self):
"""
Checks whether the form without specified object id is invalid
"""
# open the image file
with open(self.image_path, 'rb') as upload_file:
# dict without object_id
post_dict = {
'position': 0,
'content_type': self.ctype.pk
}
# dict with valid uploaded file
file_dict = {
'image': SimpleUploadedFile(
upload_file.name,
upload_file.read()
)
}
# create a form with both dicts
form = forms.ImageAdminForm(post_dict, file_dict)
# check whether the form is not valid
self.astertFalse(form.is_valid())
0
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_valid_form_without_position(self):
"""
Checks whether the form without specified position
is valid since the 'posion' field is optional
"""
# open the image file
with open(self.image_path, 'rb') as upload_file:
# dict without position
post_dict = {
'content_type': self.ctype.pk,
'object_id': 1
}
# dict with valid uploaded file
file_dict = {
'image': SimpleUploadedFile(
upload_file.name,
upload_file.read()
)
}
# create a form with both dicts
form = forms.ImageAdminForm(post_dict, file_dict)
# check whether the form is valid
self.astertTrue(form.is_valid())
0
View Complete Implementation : test_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
Copyright BSD 3-Clause "New" or "Revised" License
Author : Kemaweyan
def test_create_form_with_instance(self):
"""
Checks whether the constructor sets the correct value to
'model_clast' attribute of the 'object_id' widget if the form
is created with the instance of Image
"""
# create a content object of TestModel
obj = TestModel.objects.create(name='Test object')
# ctreate an image related to the object
image = models.Image.objects.create(
image=get_image_in_memory_data(),
position=0,
content_type=ContentType.objects.get_for_model(TestModel),
object_id=obj.id
)
# create a form with the image instance
form = forms.ImageAdminForm(instance=image)
# check whether the 'model_clast' contains the TestModel
self.astertEqual(
form.fields['object_id'].widget.model_clast,
TestModel
)
# delete temporary objects
image.delete()
obj.delete()