2012-06-07 16:08:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2013-09-04 01:01:45 +00:00
|
|
|
import warnings
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2011-08-12 14:14:58 +00:00
|
|
|
from django.contrib import admin
|
2011-06-16 16:35:04 +00:00
|
|
|
from django.contrib.admin.sites import AdminSite
|
2014-03-05 20:19:40 +00:00
|
|
|
from django.contrib.auth.models import User
|
2014-01-22 06:43:33 +00:00
|
|
|
from django.contrib.contenttypes.admin import GenericTabularInline
|
|
|
|
from django.contrib.contenttypes.forms import generic_inlineformset_factory
|
2013-02-12 10:22:41 +00:00
|
|
|
from django.forms.formsets import DEFAULT_MAX_NUM
|
2011-08-12 14:14:58 +00:00
|
|
|
from django.forms.models import ModelForm
|
2014-03-05 20:19:40 +00:00
|
|
|
from django.test import TestCase, override_settings, RequestFactory
|
2014-03-22 20:08:59 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango19Warning
|
2008-11-13 19:03:44 +00:00
|
|
|
|
|
|
|
# local test models
|
2014-03-05 20:19:40 +00:00
|
|
|
from .admin import MediaInline, MediaPermanentInline, site as admin_site
|
|
|
|
from .models import Episode, Media, EpisodePermanent, Category
|
2008-11-13 19:03:44 +00:00
|
|
|
|
2010-12-04 07:28:12 +00:00
|
|
|
|
2013-10-14 13:14:17 +00:00
|
|
|
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
2014-04-05 06:04:46 +00:00
|
|
|
TEMPLATE_DEBUG=True,
|
|
|
|
ROOT_URLCONF="generic_inline_admin.urls")
|
2008-11-13 19:03:44 +00:00
|
|
|
class GenericAdminViewTest(TestCase):
|
2008-11-14 14:47:46 +00:00
|
|
|
fixtures = ['users.xml']
|
2008-11-13 19:03:44 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# set TEMPLATE_DEBUG to True to ensure {% include %} will raise
|
|
|
|
# exceptions since that is how inlines are rendered and #9498 will
|
|
|
|
# bubble up if it is an issue.
|
2013-10-14 13:14:17 +00:00
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
self.client.login(username='super', password='secret')
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2008-11-14 14:47:46 +00:00
|
|
|
# Can't load content via a fixture (since the GenericForeignKey
|
|
|
|
# relies on content type IDs, which will vary depending on what
|
|
|
|
# other tests have been run), thus we do it here.
|
|
|
|
e = Episode.objects.create(name='This Week in Django')
|
2009-01-16 02:30:22 +00:00
|
|
|
self.episode_pk = e.pk
|
2008-11-14 14:47:46 +00:00
|
|
|
m = Media(content_object=e, url='http://example.com/podcast.mp3')
|
|
|
|
m.save()
|
2009-12-17 07:27:26 +00:00
|
|
|
self.mp3_media_pk = m.pk
|
|
|
|
|
|
|
|
m = Media(content_object=e, url='http://example.com/logo.png')
|
|
|
|
m.save()
|
|
|
|
self.png_media_pk = m.pk
|
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
def testBasicAddGet(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure GET on the add_view works.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/add/')
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
def testBasicEditGet(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure GET on the change_view works.
|
|
|
|
"""
|
2009-01-16 02:30:22 +00:00
|
|
|
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk)
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
def testBasicAddPost(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on add_view works.
|
|
|
|
"""
|
|
|
|
post_data = {
|
2012-06-07 16:08:47 +00:00
|
|
|
"name": "This Week in Django",
|
2008-11-13 19:03:44 +00:00
|
|
|
# inline data
|
2012-06-07 16:08:47 +00:00
|
|
|
"generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": "1",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": "0",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": "0",
|
2008-11-13 19:03:44 +00:00
|
|
|
}
|
|
|
|
response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/add/', post_data)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2008-11-13 19:03:44 +00:00
|
|
|
def testBasicEditPost(self):
|
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on edit_view works.
|
|
|
|
"""
|
|
|
|
post_data = {
|
2012-06-07 16:08:47 +00:00
|
|
|
"name": "This Week in Django",
|
2008-11-13 19:03:44 +00:00
|
|
|
# inline data
|
2012-06-07 16:08:47 +00:00
|
|
|
"generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": "3",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": "2",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": "0",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-0-id": "%d" % self.mp3_media_pk,
|
|
|
|
"generic_inline_admin-media-content_type-object_id-0-url": "http://example.com/podcast.mp3",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-1-id": "%d" % self.png_media_pk,
|
|
|
|
"generic_inline_admin-media-content_type-object_id-1-url": "http://example.com/logo.png",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-2-id": "",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-2-url": "",
|
2008-11-13 19:03:44 +00:00
|
|
|
}
|
2009-01-16 02:30:22 +00:00
|
|
|
url = '/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk
|
|
|
|
response = self.client.post(url, post_data)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
2009-12-17 07:27:26 +00:00
|
|
|
|
|
|
|
def testGenericInlineFormset(self):
|
2011-08-12 14:14:58 +00:00
|
|
|
EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, exclude=['description', 'keywords'], extra=3)
|
2009-12-17 07:27:26 +00:00
|
|
|
e = Episode.objects.get(name='This Week in Django')
|
|
|
|
|
|
|
|
# Works with no queryset
|
|
|
|
formset = EpisodeMediaFormSet(instance=e)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(len(formset.forms), 5)
|
2013-01-28 13:24:48 +00:00
|
|
|
self.assertHTMLEqual(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="url" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.mp3_media_pk)
|
|
|
|
self.assertHTMLEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="url" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.png_media_pk)
|
|
|
|
self.assertHTMLEqual(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="url" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>')
|
2009-12-17 07:27:26 +00:00
|
|
|
|
|
|
|
# A queryset can be used to alter display ordering
|
|
|
|
formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.order_by('url'))
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(len(formset.forms), 5)
|
2013-01-28 13:24:48 +00:00
|
|
|
self.assertHTMLEqual(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="url" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.png_media_pk)
|
|
|
|
self.assertHTMLEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="url" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.mp3_media_pk)
|
|
|
|
self.assertHTMLEqual(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="url" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>')
|
2009-12-17 07:27:26 +00:00
|
|
|
|
|
|
|
# Works with a queryset that omits items
|
|
|
|
formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png"))
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(len(formset.forms), 4)
|
2013-01-28 13:24:48 +00:00
|
|
|
self.assertHTMLEqual(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="url" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.png_media_pk)
|
|
|
|
self.assertHTMLEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="url" name="generic_inline_admin-media-content_type-object_id-1-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>')
|
2009-12-17 07:27:26 +00:00
|
|
|
|
2009-04-03 20:03:46 +00:00
|
|
|
def testGenericInlineFormsetFactory(self):
|
|
|
|
# Regression test for #10522.
|
|
|
|
inline_formset = generic_inlineformset_factory(Media,
|
|
|
|
exclude=('url',))
|
2009-04-18 20:12:53 +00:00
|
|
|
|
2009-12-09 03:49:25 +00:00
|
|
|
# Regression test for #12340.
|
|
|
|
e = Episode.objects.get(name='This Week in Django')
|
|
|
|
formset = inline_formset(instance=e)
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertTrue(formset.get_queryset().ordered)
|
2009-12-09 03:49:25 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
|
|
|
ROOT_URLCONF="generic_inline_admin.urls")
|
2009-04-18 20:12:53 +00:00
|
|
|
class GenericInlineAdminParametersTest(TestCase):
|
|
|
|
fixtures = ['users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
2014-03-05 20:19:40 +00:00
|
|
|
self.factory = RequestFactory()
|
2009-04-22 22:38:14 +00:00
|
|
|
|
2009-04-18 20:12:53 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
2009-04-22 22:38:14 +00:00
|
|
|
def _create_object(self, model):
|
|
|
|
"""
|
|
|
|
Create a model with an attached Media object via GFK. We can't
|
|
|
|
load content via a fixture (since the GenericForeignKey relies on
|
|
|
|
content type IDs, which will vary depending on what other tests
|
|
|
|
have been run), thus we do it here.
|
|
|
|
"""
|
|
|
|
e = model.objects.create(name='This Week in Django')
|
|
|
|
Media.objects.create(content_object=e, url='http://example.com/podcast.mp3')
|
|
|
|
return e
|
|
|
|
|
2009-04-18 20:12:53 +00:00
|
|
|
def testNoParam(self):
|
|
|
|
"""
|
|
|
|
With one initial form, extra (default) at 3, there should be 4 forms.
|
|
|
|
"""
|
2009-04-22 22:38:14 +00:00
|
|
|
e = self._create_object(Episode)
|
|
|
|
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk)
|
2009-04-18 20:12:53 +00:00
|
|
|
formset = response.context['inline_admin_formsets'][0].formset
|
|
|
|
self.assertEqual(formset.total_form_count(), 4)
|
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
|
|
|
def testExtraParam(self):
|
|
|
|
"""
|
|
|
|
With extra=0, there should be one form.
|
|
|
|
"""
|
2014-03-05 20:19:40 +00:00
|
|
|
class ExtraInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 0
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [ExtraInline]
|
|
|
|
|
|
|
|
e = self._create_object(Episode)
|
|
|
|
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk)
|
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
2009-04-18 20:12:53 +00:00
|
|
|
self.assertEqual(formset.total_form_count(), 1)
|
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
|
|
|
def testMaxNumParam(self):
|
|
|
|
"""
|
|
|
|
With extra=5 and max_num=2, there should be only 2 forms.
|
|
|
|
"""
|
2014-03-05 20:19:40 +00:00
|
|
|
class MaxNumInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 5
|
|
|
|
max_num = 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [MaxNumInline]
|
|
|
|
|
|
|
|
e = self._create_object(Episode)
|
|
|
|
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk)
|
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
2009-04-18 20:12:53 +00:00
|
|
|
self.assertEqual(formset.total_form_count(), 2)
|
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
2014-03-05 20:19:40 +00:00
|
|
|
def testMinNumParam(self):
|
|
|
|
"""
|
2014-05-16 03:12:32 +00:00
|
|
|
With extra=3 and min_num=2, there should be five forms.
|
2014-03-05 20:19:40 +00:00
|
|
|
"""
|
|
|
|
class MinNumInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 3
|
|
|
|
min_num = 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [MinNumInline]
|
|
|
|
|
|
|
|
e = self._create_object(Episode)
|
|
|
|
request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk)
|
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
2014-05-16 03:12:32 +00:00
|
|
|
self.assertEqual(formset.total_form_count(), 5)
|
2014-03-05 20:19:40 +00:00
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
2010-01-21 01:40:21 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',),
|
|
|
|
ROOT_URLCONF="generic_inline_admin.urls")
|
2010-01-21 01:40:21 +00:00
|
|
|
class GenericInlineAdminWithUniqueTogetherTest(TestCase):
|
|
|
|
fixtures = ['users.xml']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='super', password='secret')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def testAdd(self):
|
2010-11-13 18:42:31 +00:00
|
|
|
category_id = Category.objects.create(name='male').pk
|
2010-01-21 01:40:21 +00:00
|
|
|
post_data = {
|
2012-06-07 16:08:47 +00:00
|
|
|
"name": "John Doe",
|
2010-01-21 01:40:21 +00:00
|
|
|
# inline data
|
2012-06-07 16:08:47 +00:00
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-TOTAL_FORMS": "1",
|
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-INITIAL_FORMS": "0",
|
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-MAX_NUM_FORMS": "0",
|
2010-01-21 01:40:21 +00:00
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-0-id": "",
|
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555",
|
2010-11-13 18:42:31 +00:00
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-0-category": "%s" % category_id,
|
2010-01-21 01:40:21 +00:00
|
|
|
}
|
|
|
|
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/contact/add/')
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-01-21 01:40:21 +00:00
|
|
|
response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/contact/add/', post_data)
|
2013-11-02 21:02:56 +00:00
|
|
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
2010-02-23 17:14:50 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF="generic_inline_admin.urls")
|
2010-02-23 17:14:50 +00:00
|
|
|
class NoInlineDeletionTest(TestCase):
|
2011-09-20 18:16:49 +00:00
|
|
|
|
2010-02-23 17:14:50 +00:00
|
|
|
def test_no_deletion(self):
|
|
|
|
fake_site = object()
|
|
|
|
inline = MediaPermanentInline(EpisodePermanent, fake_site)
|
|
|
|
fake_request = object()
|
|
|
|
formset = inline.get_formset(fake_request)
|
|
|
|
self.assertFalse(formset.can_delete)
|
2011-06-16 16:35:04 +00:00
|
|
|
|
2011-10-07 00:41:25 +00:00
|
|
|
|
|
|
|
class MockRequest(object):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2011-10-07 00:41:25 +00:00
|
|
|
class MockSuperUser(object):
|
|
|
|
def has_perm(self, perm):
|
|
|
|
return True
|
|
|
|
|
|
|
|
request = MockRequest()
|
|
|
|
request.user = MockSuperUser()
|
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF="generic_inline_admin.urls")
|
2011-06-16 16:35:04 +00:00
|
|
|
class GenericInlineModelAdminTest(TestCase):
|
|
|
|
|
2011-08-12 14:14:58 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.site = AdminSite()
|
|
|
|
|
2011-06-16 16:35:04 +00:00
|
|
|
def test_get_formset_kwargs(self):
|
|
|
|
media_inline = MediaInline(Media, AdminSite())
|
|
|
|
|
|
|
|
# Create a formset with default arguments
|
2011-10-07 00:41:25 +00:00
|
|
|
formset = media_inline.get_formset(request)
|
2013-02-12 10:22:41 +00:00
|
|
|
self.assertEqual(formset.max_num, DEFAULT_MAX_NUM)
|
2011-06-16 16:35:04 +00:00
|
|
|
self.assertEqual(formset.can_order, False)
|
|
|
|
|
|
|
|
# Create a formset with custom keyword arguments
|
2011-10-07 00:41:25 +00:00
|
|
|
formset = media_inline.get_formset(request, max_num=100, can_order=True)
|
2011-06-16 16:35:04 +00:00
|
|
|
self.assertEqual(formset.max_num, 100)
|
|
|
|
self.assertEqual(formset.can_order, True)
|
2011-08-12 14:14:58 +00:00
|
|
|
|
|
|
|
def test_custom_form_meta_exclude_with_readonly(self):
|
|
|
|
"""
|
|
|
|
Ensure that the custom ModelForm's `Meta.exclude` is respected when
|
|
|
|
used in conjunction with `GenericInlineModelAdmin.readonly_fields`
|
|
|
|
and when no `ModelAdmin.exclude` is defined.
|
|
|
|
"""
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
readonly_fields = ['description']
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(
|
2013-09-04 01:01:45 +00:00
|
|
|
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
|
2011-08-12 14:14:58 +00:00
|
|
|
['keywords', 'id', 'DELETE'])
|
|
|
|
|
|
|
|
def test_custom_form_meta_exclude(self):
|
|
|
|
"""
|
|
|
|
Ensure that the custom ModelForm's `Meta.exclude` is respected by
|
|
|
|
`GenericInlineModelAdmin.get_formset`, and overridden if
|
|
|
|
`ModelAdmin.exclude` or `GenericInlineModelAdmin.exclude` are defined.
|
|
|
|
Refs #15907.
|
|
|
|
"""
|
|
|
|
# First with `GenericInlineModelAdmin` -----------------
|
|
|
|
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
exclude = ['description']
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(
|
2013-09-04 01:01:45 +00:00
|
|
|
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
|
2011-08-12 14:14:58 +00:00
|
|
|
['url', 'keywords', 'id', 'DELETE'])
|
|
|
|
|
|
|
|
# Then, only with `ModelForm` -----------------
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(
|
2013-09-04 01:01:45 +00:00
|
|
|
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
|
2011-08-12 14:14:58 +00:00
|
|
|
['description', 'keywords', 'id', 'DELETE'])
|
2013-07-31 05:50:39 +00:00
|
|
|
|
|
|
|
def test_get_fieldsets(self):
|
|
|
|
# Test that get_fieldsets is called when figuring out form fields.
|
|
|
|
# Refs #18681.
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
can_delete = False
|
|
|
|
|
|
|
|
def get_fieldsets(self, request, obj=None):
|
|
|
|
return [(None, {'fields': ['url', 'description']})]
|
|
|
|
|
|
|
|
ma = MediaInline(Media, self.site)
|
|
|
|
form = ma.get_formset(None).form
|
|
|
|
self.assertEqual(form._meta.fields, ['url', 'description'])
|
2013-09-04 01:01:45 +00:00
|
|
|
|
|
|
|
def test_get_formsets_with_inlines(self):
|
|
|
|
"""
|
|
|
|
get_formsets() triggers a deprecation warning when get_formsets is
|
|
|
|
overridden.
|
|
|
|
"""
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
exclude = ['description']
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_formsets(self, request, obj=None):
|
|
|
|
return []
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
list(ma.get_formsets_with_inlines(request))
|
|
|
|
# Verify that the deprecation warning was triggered when get_formsets was called
|
|
|
|
# This verifies that we called that method.
|
|
|
|
self.assertEqual(len(w), 1)
|
2014-03-22 20:08:59 +00:00
|
|
|
self.assertTrue(issubclass(w[0].category, RemovedInDjango19Warning))
|
2013-09-04 01:01:45 +00:00
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
list(ma.get_formsets_with_inlines(request))
|
|
|
|
self.assertEqual(len(w), 0)
|
|
|
|
|
|
|
|
def test_get_formsets_with_inlines_returns_tuples(self):
|
|
|
|
"""
|
|
|
|
Ensure that get_formsets_with_inlines() returns the correct tuples.
|
|
|
|
"""
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class AlternateInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
AlternateInline, MediaInline
|
|
|
|
]
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
inlines = ma.get_inline_instances(request)
|
|
|
|
for (formset, inline), other_inline in zip(ma.get_formsets_with_inlines(request), inlines):
|
|
|
|
self.assertIsInstance(formset, other_inline.get_formset(request).__class__)
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
AlternateInline, MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_formsets(self, request, obj=None):
|
|
|
|
# Catch the deprecation warning to force the usage of get_formsets
|
2013-10-19 12:31:38 +00:00
|
|
|
with warnings.catch_warnings(record=True):
|
2013-09-04 01:01:45 +00:00
|
|
|
warnings.simplefilter("always")
|
|
|
|
return super(EpisodeAdmin, self).get_formsets(request, obj)
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
inlines = ma.get_inline_instances(request)
|
2013-10-19 12:31:38 +00:00
|
|
|
with warnings.catch_warnings(record=True):
|
2013-09-04 01:01:45 +00:00
|
|
|
warnings.simplefilter("always")
|
|
|
|
for (formset, inline), other_inline in zip(ma.get_formsets_with_inlines(request), inlines):
|
|
|
|
self.assertIsInstance(formset, other_inline.get_formset(request).__class__)
|