/tests/regressiontests/admin_inlines/tests.py

https://code.google.com/p/mango-py/ · Python · 157 lines · 131 code · 12 blank · 14 comment · 2 complexity · cf95380694e7af87cad78b5769998bd8 MD5 · raw file

  1. from django.contrib.admin.helpers import InlineAdminForm
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.test import TestCase
  4. # local test models
  5. from models import (Holder, Inner, InnerInline, Holder2, Inner2, Holder3,
  6. Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
  7. class TestInline(TestCase):
  8. fixtures = ['admin-views-users.xml']
  9. def setUp(self):
  10. holder = Holder(dummy=13)
  11. holder.save()
  12. Inner(dummy=42, holder=holder).save()
  13. self.change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id
  14. result = self.client.login(username='super', password='secret')
  15. self.assertEqual(result, True)
  16. def tearDown(self):
  17. self.client.logout()
  18. def test_can_delete(self):
  19. """
  20. can_delete should be passed to inlineformset factory.
  21. """
  22. response = self.client.get(self.change_url)
  23. inner_formset = response.context[-1]['inline_admin_formsets'][0].formset
  24. expected = InnerInline.can_delete
  25. actual = inner_formset.can_delete
  26. self.assertEqual(expected, actual, 'can_delete must be equal')
  27. def test_readonly_stacked_inline_label(self):
  28. """Bug #13174."""
  29. holder = Holder.objects.create(dummy=42)
  30. inner = Inner.objects.create(holder=holder, dummy=42, readonly='')
  31. response = self.client.get('/test_admin/admin/admin_inlines/holder/%i/'
  32. % holder.id)
  33. self.assertContains(response, '<label>Inner readonly label:</label>')
  34. def test_many_to_many_inlines(self):
  35. "Autogenerated many-to-many inlines are displayed correctly (#13407)"
  36. response = self.client.get('/test_admin/admin/admin_inlines/author/add/')
  37. # The heading for the m2m inline block uses the right text
  38. self.assertContains(response, '<h2>Author-book relationships</h2>')
  39. # The "add another" label is correct
  40. self.assertContains(response, 'Add another Author-Book Relationship')
  41. # The '+' is dropped from the autogenerated form prefix (Author_books+)
  42. self.assertContains(response, 'id="id_Author_books-TOTAL_FORMS"')
  43. def test_inline_primary(self):
  44. person = Person.objects.create(firstname='Imelda')
  45. item = OutfitItem.objects.create(name='Shoes')
  46. # Imelda likes shoes, but can't cary her own bags.
  47. data = {
  48. 'shoppingweakness_set-TOTAL_FORMS': 1,
  49. 'shoppingweakness_set-INITIAL_FORMS': 0,
  50. 'shoppingweakness_set-MAX_NUM_FORMS': 0,
  51. '_save': u'Save',
  52. 'person': person.id,
  53. 'max_weight': 0,
  54. 'shoppingweakness_set-0-item': item.id,
  55. }
  56. response = self.client.post('/test_admin/admin/admin_inlines/fashionista/add/', data)
  57. self.assertEqual(response.status_code, 302)
  58. self.assertEqual(len(Fashionista.objects.filter(person__firstname='Imelda')), 1)
  59. def test_tabular_non_field_errors(self):
  60. """
  61. Ensure that non_field_errors are displayed correctly, including the
  62. right value for colspan. Refs #13510.
  63. """
  64. data = {
  65. 'title_set-TOTAL_FORMS': 1,
  66. 'title_set-INITIAL_FORMS': 0,
  67. 'title_set-MAX_NUM_FORMS': 0,
  68. '_save': u'Save',
  69. 'title_set-0-title1': 'a title',
  70. 'title_set-0-title2': 'a different title',
  71. }
  72. response = self.client.post('/test_admin/admin/admin_inlines/titlecollection/add/', data)
  73. # Here colspan is "4": two fields (title1 and title2), one hidden field and the delete checkbock.
  74. self.assertContains(response, '<tr><td colspan="4"><ul class="errorlist"><li>The two titles must be the same</li></ul></td></tr>')
  75. def test_no_parent_callable_lookup(self):
  76. """Admin inline `readonly_field` shouldn't invoke parent ModelAdmin callable"""
  77. # Identically named callable isn't present in the parent ModelAdmin,
  78. # rendering of the add view shouldn't explode
  79. response = self.client.get('/test_admin/admin/admin_inlines/novel/add/')
  80. self.assertEqual(response.status_code, 200)
  81. # View should have the child inlines section
  82. self.assertContains(response, '<div class="inline-group" id="chapter_set-group">')
  83. def test_callable_lookup(self):
  84. """Admin inline should invoke local callable when its name is listed in readonly_fields"""
  85. response = self.client.get('/test_admin/admin/admin_inlines/poll/add/')
  86. self.assertEqual(response.status_code, 200)
  87. # Add parent object view should have the child inlines section
  88. self.assertContains(response, '<div class="inline-group" id="question_set-group">')
  89. # The right callabe should be used for the inline readonly_fields
  90. # column cells
  91. self.assertContains(response, '<p>Callable in QuestionInline</p>')
  92. class TestInlineMedia(TestCase):
  93. fixtures = ['admin-views-users.xml']
  94. def setUp(self):
  95. result = self.client.login(username='super', password='secret')
  96. self.assertEqual(result, True)
  97. def tearDown(self):
  98. self.client.logout()
  99. def test_inline_media_only_base(self):
  100. holder = Holder(dummy=13)
  101. holder.save()
  102. Inner(dummy=42, holder=holder).save()
  103. change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id
  104. response = self.client.get(change_url)
  105. self.assertContains(response, 'my_awesome_admin_scripts.js')
  106. def test_inline_media_only_inline(self):
  107. holder = Holder3(dummy=13)
  108. holder.save()
  109. Inner3(dummy=42, holder=holder).save()
  110. change_url = '/test_admin/admin/admin_inlines/holder3/%i/' % holder.id
  111. response = self.client.get(change_url)
  112. self.assertContains(response, 'my_awesome_inline_scripts.js')
  113. def test_all_inline_media(self):
  114. holder = Holder2(dummy=13)
  115. holder.save()
  116. Inner2(dummy=42, holder=holder).save()
  117. change_url = '/test_admin/admin/admin_inlines/holder2/%i/' % holder.id
  118. response = self.client.get(change_url)
  119. self.assertContains(response, 'my_awesome_admin_scripts.js')
  120. self.assertContains(response, 'my_awesome_inline_scripts.js')
  121. class TestInlineAdminForm(TestCase):
  122. def test_immutable_content_type(self):
  123. """Regression for #9362
  124. The problem depends only on InlineAdminForm and its "original"
  125. argument, so we can safely set the other arguments to None/{}. We just
  126. need to check that the content_type argument of Child isn't altered by
  127. the internals of the inline form."""
  128. sally = Teacher.objects.create(name='Sally')
  129. john = Parent.objects.create(name='John')
  130. joe = Child.objects.create(name='Joe', teacher=sally, parent=john)
  131. iaf = InlineAdminForm(None, None, {}, {}, joe)
  132. parent_ct = ContentType.objects.get_for_model(Parent)
  133. self.assertEqual(iaf.original.content_type, parent_ct)