Debian Codegraph @

tests/admin_inlines/tests.py

raw
1 from django.contrib.admin import ModelAdmin, TabularInline
2 from django.contrib.admin.helpers import InlineAdminForm
3 from django.contrib.admin.tests import AdminSeleniumTestCase
4 from django.contrib.auth.models import Permission, User
5 from django.contrib.contenttypes.models import ContentType
6 from django.test import RequestFactory, TestCase, override_settings
7 from django.test.selenium import screenshot_cases
8 from django.urls import reverse
9 from django.utils.translation import gettext
10
11 from .admin import InnerInline
12 from .admin import site as admin_site
13 from .models import (
14 Author,
15 BinaryTree,
16 Book,
17 BothVerboseNameProfile,
18 Chapter,
19 Child,
20 ChildModel1,
21 ChildModel2,
22 ExtraTerrestrial,
23 Fashionista,
24 FootNote,
25 Holder,
26 Holder2,
27 Holder3,
28 Holder4,
29 Inner,
30 Inner2,
31 Inner3,
32 Inner4Stacked,
33 Inner4Tabular,
34 Novel,
35 OutfitItem,
36 Parent,
37 ParentModelWithCustomPk,
38 Person,
39 Poll,
40 Profile,
41 ProfileCollection,
42 Question,
43 ShowInlineParent,
44 Sighting,
45 SomeChildModel,
46 SomeParentModel,
47 Teacher,
48 UUIDChild,
49 UUIDParent,
50 VerboseNamePluralProfile,
51 VerboseNameProfile,
52 )
53
54 INLINE_CHANGELINK_HTML = 'class="inlinechangelink">Change</a>'
55
56
57 class TestDataMixin:
58 @classmethod
59 def setUpTestData(cls):
60 cls.superuser = User.objects.create_superuser(
61 username="super", email="[email protected]", password="secret"
62 )
63
64
65 @override_settings(ROOT_URLCONF="admin_inlines.urls")
66 class TestInline(TestDataMixin, TestCase):
67 factory = RequestFactory()
68
69 @classmethod
70 def setUpTestData(cls):
71 super().setUpTestData()
72 cls.holder = Holder.objects.create(dummy=13)
73 Inner.objects.create(dummy=42, holder=cls.holder)
74
75 cls.parent = SomeParentModel.objects.create(name="a")
76 SomeChildModel.objects.create(name="b", position="0", parent=cls.parent)
77 SomeChildModel.objects.create(name="c", position="1", parent=cls.parent)
78
79 cls.view_only_user = User.objects.create_user(
80 username="user",
81 password="pwd",
82 is_staff=True,
83 )
84 parent_ct = ContentType.objects.get_for_model(SomeParentModel)
85 child_ct = ContentType.objects.get_for_model(SomeChildModel)
86 permission = Permission.objects.get(
87 codename="view_someparentmodel",
88 content_type=parent_ct,
89 )
90 cls.view_only_user.user_permissions.add(permission)
91 permission = Permission.objects.get(
92 codename="view_somechildmodel",
93 content_type=child_ct,
94 )
95 cls.view_only_user.user_permissions.add(permission)
96
97 def setUp(self):
98 self.client.force_login(self.superuser)
99
100 def test_can_delete(self):
101 """
102 can_delete should be passed to inlineformset factory.
103 """
104 response = self.client.get(
105 reverse("admin:admin_inlines_holder_change", args=(self.holder.id,))
106 )
107 inner_formset = response.context["inline_admin_formsets"][0].formset
108 expected = InnerInline.can_delete
109 actual = inner_formset.can_delete
110 self.assertEqual(expected, actual, "can_delete must be equal")
111
112 def test_readonly_stacked_inline_label(self):
113 """Bug #13174."""
114 holder = Holder.objects.create(dummy=42)
115 Inner.objects.create(holder=holder, dummy=42, readonly="")
116 response = self.client.get(
117 reverse("admin:admin_inlines_holder_change", args=(holder.id,))
118 )
119 self.assertContains(response, "<label>Inner readonly label:</label>")
120
121 def test_excluded_id_for_inlines_uses_hidden_field(self):
122 parent = UUIDParent.objects.create()
123 child = UUIDChild.objects.create(title="foo", parent=parent)
124 response = self.client.get(
125 reverse("admin:admin_inlines_uuidparent_change", args=(parent.id,))
126 )
127 self.assertContains(
128 response,
129 f'<input type="hidden" name="uuidchild_set-0-id" value="{child.id}" '
130 'id="id_uuidchild_set-0-id">',
131 html=True,
132 )
133
134 def test_many_to_many_inlines(self):
135 "Autogenerated many-to-many inlines are displayed correctly (#13407)"
136 response = self.client.get(reverse("admin:admin_inlines_author_add"))
137 # The heading for the m2m inline block uses the right text
138 self.assertContains(
139 response,
140 (
141 '<h2 id="Author_books-heading" class="inline-heading">'
142 "Author-book relationships</h2>"
143 ),
144 html=True,
145 )
146 # The "add another" label is correct
147 self.assertContains(response, "Add another Author-book relationship")
148 # The '+' is dropped from the autogenerated form prefix (Author_books+)
149 self.assertContains(response, 'id="id_Author_books-TOTAL_FORMS"')
150
151 def test_inline_primary(self):
152 person = Person.objects.create(firstname="Imelda")
153 item = OutfitItem.objects.create(name="Shoes")
154 # Imelda likes shoes, but can't carry her own bags.
155 data = {
156 "shoppingweakness_set-TOTAL_FORMS": 1,
157 "shoppingweakness_set-INITIAL_FORMS": 0,
158 "shoppingweakness_set-MAX_NUM_FORMS": 0,
159 "_save": "Save",
160 "person": person.id,
161 "max_weight": 0,
162 "shoppingweakness_set-0-item": item.id,
163 }
164 response = self.client.post(
165 reverse("admin:admin_inlines_fashionista_add"), data
166 )
167 self.assertEqual(response.status_code, 302)
168 self.assertEqual(len(Fashionista.objects.filter(person__firstname="Imelda")), 1)
169
170 def test_tabular_inline_column_css_class(self):
171 """
172 Field names are included in the context to output a field-specific
173 CSS class name in the column headers.
174 """
175 response = self.client.get(reverse("admin:admin_inlines_poll_add"))
176 text_field, call_me_field = list(
177 response.context["inline_admin_formset"].fields()
178 )
179 # Editable field.
180 self.assertEqual(text_field["name"], "text")
181 self.assertContains(response, '<th class="column-text required">')
182 # Read-only field.
183 self.assertEqual(call_me_field["name"], "call_me")
184 self.assertContains(response, '<th class="column-call_me">')
185
186 def test_custom_form_tabular_inline_label(self):
187 """
188 A model form with a form field specified (TitleForm.title1) should have
189 its label rendered in the tabular inline.
190 """
191 response = self.client.get(reverse("admin:admin_inlines_titlecollection_add"))
192 self.assertContains(
193 response, '<th class="column-title1 required">Title1</th>', html=True
194 )
195
196 def test_custom_form_tabular_inline_extra_field_label(self):
197 response = self.client.get(reverse("admin:admin_inlines_outfititem_add"))
198 _, extra_field = list(response.context["inline_admin_formset"].fields())
199 self.assertEqual(extra_field["label"], "Extra field")
200
201 def test_non_editable_custom_form_tabular_inline_extra_field_label(self):
202 response = self.client.get(reverse("admin:admin_inlines_chapter_add"))
203 _, extra_field = list(response.context["inline_admin_formset"].fields())
204 self.assertEqual(extra_field["label"], "Extra field")
205
206 def test_custom_form_tabular_inline_overridden_label(self):
207 """
208 SomeChildModelForm.__init__() overrides the label of a form field.
209 That label is displayed in the TabularInline.
210 """
211 response = self.client.get(reverse("admin:admin_inlines_someparentmodel_add"))
212 field = list(response.context["inline_admin_formset"].fields())[0]
213 self.assertEqual(field["label"], "new label")
214 self.assertContains(
215 response, '<th class="column-name required">New label</th>', html=True
216 )
217
218 def test_tabular_non_field_errors(self):
219 """
220 non_field_errors are displayed correctly, including the correct value
221 for colspan.
222 """
223 data = {
224 "title_set-TOTAL_FORMS": 1,
225 "title_set-INITIAL_FORMS": 0,
226 "title_set-MAX_NUM_FORMS": 0,
227 "_save": "Save",
228 "title_set-0-title1": "a title",
229 "title_set-0-title2": "a different title",
230 }
231 response = self.client.post(
232 reverse("admin:admin_inlines_titlecollection_add"), data
233 )
234 # Here colspan is "4": two fields (title1 and title2), one hidden field
235 # and the delete checkbox.
236 self.assertContains(
237 response,
238 '<tr class="row-form-errors"><td colspan="4">'
239 '<ul class="errorlist nonfield">'
240 "<li>The two titles must be the same</li></ul></td></tr>",
241 )
242
243 def test_no_parent_callable_lookup(self):
244 """Admin inline `readonly_field` shouldn't invoke parent ModelAdmin callable"""
245 # Identically named callable isn't present in the parent ModelAdmin,
246 # rendering of the add view shouldn't explode
247 response = self.client.get(reverse("admin:admin_inlines_novel_add"))
248 # View should have the child inlines section
249 self.assertContains(
250 response,
251 '<div class="js-inline-admin-formset inline-group" id="chapter_set-group"',
252 )
253
254 def test_callable_lookup(self):
255 """
256 Admin inline should invoke local callable when its name is listed in
257 readonly_fields.
258 """
259 response = self.client.get(reverse("admin:admin_inlines_poll_add"))
260 # Add parent object view should have the child inlines section
261 self.assertContains(
262 response,
263 '<div class="js-inline-admin-formset inline-group" id="question_set-group"',
264 )
265 # The right callable should be used for the inline readonly_fields
266 # column cells
267 self.assertContains(response, "<p>Callable in QuestionInline</p>")
268
269 def test_model_error_inline_with_readonly_field(self):
270 poll = Poll.objects.create(name="Test poll")
271 data = {
272 "question_set-TOTAL_FORMS": 1,
273 "question_set-INITIAL_FORMS": 0,
274 "question_set-MAX_NUM_FORMS": 0,
275 "_save": "Save",
276 "question_set-0-text": "Question",
277 "question_set-0-poll": poll.pk,
278 }
279 response = self.client.post(
280 reverse("admin:admin_inlines_poll_change", args=(poll.pk,)),
281 data,
282 )
283 self.assertContains(response, "Always invalid model.")
284
285 def test_help_text(self):
286 """
287 The inlines' model field help texts are displayed when using both the
288 stacked and tabular layouts.
289 """
290 response = self.client.get(reverse("admin:admin_inlines_holder4_add"))
291 self.assertContains(response, "Awesome stacked help text is awesome.", 4)
292 self.assertContains(
293 response,
294 '<img src="/static/admin/img/icon-unknown.svg" '
295 'class="help help-tooltip" width="10" height="10" '
296 'alt="(Awesome tabular help text is awesome.)" '
297 'title="Awesome tabular help text is awesome.">',
298 1,
299 )
300 # ReadOnly fields
301 response = self.client.get(reverse("admin:admin_inlines_capofamiglia_add"))
302 self.assertContains(
303 response,
304 '<img src="/static/admin/img/icon-unknown.svg" '
305 'class="help help-tooltip" width="10" height="10" '
306 'alt="(Help text for ReadOnlyInline)" '
307 'title="Help text for ReadOnlyInline">',
308 1,
309 )
310
311 def test_tabular_model_form_meta_readonly_field(self):
312 """
313 Tabular inlines use ModelForm.Meta.help_texts and labels for read-only
314 fields.
315 """
316 response = self.client.get(reverse("admin:admin_inlines_someparentmodel_add"))
317 self.assertContains(
318 response,
319 '<img src="/static/admin/img/icon-unknown.svg" '
320 'class="help help-tooltip" width="10" height="10" '
321 'alt="(Help text from ModelForm.Meta)" '
322 'title="Help text from ModelForm.Meta">',
323 )
324 self.assertContains(response, "Label from ModelForm.Meta")
325
326 def test_inline_hidden_field_no_column(self):
327 """#18263 -- Make sure hidden fields don't get a column in tabular inlines"""
328 parent = SomeParentModel.objects.create(name="a")
329 SomeChildModel.objects.create(name="b", position="0", parent=parent)
330 SomeChildModel.objects.create(name="c", position="1", parent=parent)
331 response = self.client.get(
332 reverse("admin:admin_inlines_someparentmodel_change", args=(parent.pk,))
333 )
334 self.assertNotContains(response, '<td class="field-position">')
335 self.assertInHTML(
336 '<input id="id_somechildmodel_set-1-position" '
337 'name="somechildmodel_set-1-position" type="hidden" value="1">',
338 response.rendered_content,
339 )
340
341 def test_tabular_inline_hidden_field_with_view_only_permissions(self):
342 """
343 Content of hidden field is not visible in tabular inline when user has
344 view-only permission.
345 """
346 self.client.force_login(self.view_only_user)
347 url = reverse(
348 "tabular_inline_hidden_field_admin:admin_inlines_someparentmodel_change",
349 args=(self.parent.pk,),
350 )
351 response = self.client.get(url)
352 self.assertInHTML(
353 '<th class="column-position hidden">Position'
354 '<img src="/static/admin/img/icon-unknown.svg" '
355 'class="help help-tooltip" width="10" height="10" '
356 'alt="(Position help_text.)" '
357 'title="Position help_text.">'
358 "</th>",
359 response.rendered_content,
360 )
361 self.assertInHTML(
362 '<td class="field-position hidden"><p>0</p></td>', response.rendered_content
363 )
364 self.assertInHTML(
365 '<td class="field-position hidden"><p>1</p></td>', response.rendered_content
366 )
367
368 def test_stacked_inline_hidden_field_with_view_only_permissions(self):
369 """
370 Content of hidden field is not visible in stacked inline when user has
371 view-only permission.
372 """
373 self.client.force_login(self.view_only_user)
374 url = reverse(
375 "stacked_inline_hidden_field_in_group_admin:"
376 "admin_inlines_someparentmodel_change",
377 args=(self.parent.pk,),
378 )
379 response = self.client.get(url)
380 # The whole line containing name + position fields is not hidden.
381 self.assertContains(
382 response, '<div class="form-row field-name field-position">'
383 )
384 # The div containing the position field is hidden.
385 self.assertInHTML(
386 '<div class="flex-container fieldBox field-position hidden">'
387 '<label class="inline">Position:</label>'
388 '<div class="readonly">0</div></div>'
389 '<div class="help hidden"><div>Position help_text.</div></div>',
390 response.rendered_content,
391 )
392 self.assertInHTML(
393 '<div class="flex-container fieldBox field-position hidden">'
394 '<label class="inline">Position:</label>'
395 '<div class="readonly">1</div></div>'
396 '<div class="help hidden"><div>Position help_text.</div></div>',
397 response.rendered_content,
398 )
399
400 def test_stacked_inline_single_hidden_field_in_line_with_view_only_permissions(
401 self,
402 ):
403 """
404 Content of hidden field is not visible in stacked inline when user has
405 view-only permission and the field is grouped on a separate line.
406 """
407 self.client.force_login(self.view_only_user)
408 url = reverse(
409 "stacked_inline_hidden_field_on_single_line_admin:"
410 "admin_inlines_someparentmodel_change",
411 args=(self.parent.pk,),
412 )
413 response = self.client.get(url)
414 # The whole line containing position field is hidden.
415 self.assertInHTML(
416 '<div class="form-row hidden field-position">'
417 '<div><div class="flex-container"><label>Position:</label>'
418 '<div class="readonly">0</div></div>'
419 '<div class="help hidden"><div>Position help_text.</div></div>'
420 "</div></div>",
421 response.rendered_content,
422 )
423 self.assertInHTML(
424 '<div class="form-row hidden field-position">'
425 '<div><div class="flex-container"><label>Position:</label>'
426 '<div class="readonly">1</div></div>'
427 '<div class="help hidden"><div>Position help_text.</div></div>'
428 "</div></div>",
429 response.rendered_content,
430 )
431
432 def test_tabular_inline_with_hidden_field_non_field_errors_has_correct_colspan(
433 self,
434 ):
435 """
436 In tabular inlines, when a form has non-field errors, those errors
437 are rendered in a table line with a single cell spanning the whole
438 table width. Colspan must be equal to the number of visible columns.
439 """
440 parent = SomeParentModel.objects.create(name="a")
441 child = SomeChildModel.objects.create(name="b", position="0", parent=parent)
442 url = reverse(
443 "tabular_inline_hidden_field_admin:admin_inlines_someparentmodel_change",
444 args=(parent.id,),
445 )
446 data = {
447 "name": parent.name,
448 "somechildmodel_set-TOTAL_FORMS": 1,
449 "somechildmodel_set-INITIAL_FORMS": 1,
450 "somechildmodel_set-MIN_NUM_FORMS": 0,
451 "somechildmodel_set-MAX_NUM_FORMS": 1000,
452 "_save": "Save",
453 "somechildmodel_set-0-id": child.id,
454 "somechildmodel_set-0-parent": parent.id,
455 "somechildmodel_set-0-name": child.name,
456 "somechildmodel_set-0-position": 1,
457 }
458 response = self.client.post(url, data)
459 # Form has 3 visible columns and 1 hidden column.
460 self.assertInHTML(
461 '<thead><tr><th class="original"></th>'
462 '<th class="column-name required">Name</th>'
463 '<th class="column-position required hidden">Position'
464 '<img src="/static/admin/img/icon-unknown.svg" '
465 'class="help help-tooltip" width="10" height="10" '
466 'alt="(Position help_text.)" '
467 'title="Position help_text.">'
468 "</th>"
469 "<th>Delete?</th></tr></thead>",
470 response.rendered_content,
471 )
472 # The non-field error must be spanned on 3 (visible) columns.
473 self.assertInHTML(
474 '<tr class="row-form-errors"><td colspan="3">'
475 '<ul class="errorlist nonfield"><li>A non-field error</li></ul></td></tr>',
476 response.rendered_content,
477 )
478
479 def test_non_related_name_inline(self):
480 """
481 Multiple inlines with related_name='+' have correct form prefixes.
482 """
483 response = self.client.get(reverse("admin:admin_inlines_capofamiglia_add"))
484 self.assertContains(
485 response, '<input type="hidden" name="-1-0-id" id="id_-1-0-id">', html=True
486 )
487 self.assertContains(
488 response,
489 '<input type="hidden" name="-1-0-capo_famiglia" '
490 'id="id_-1-0-capo_famiglia">',
491 html=True,
492 )
493 self.assertContains(
494 response,
495 '<input id="id_-1-0-name" type="text" class="vTextField" name="-1-0-name" '
496 'maxlength="100" aria-describedby="id_-1-0-name_helptext">',
497 html=True,
498 )
499 self.assertContains(
500 response, '<input type="hidden" name="-2-0-id" id="id_-2-0-id">', html=True
501 )
502 self.assertContains(
503 response,
504 '<input type="hidden" name="-2-0-capo_famiglia" '
505 'id="id_-2-0-capo_famiglia">',
506 html=True,
507 )
508 self.assertContains(
509 response,
510 '<input id="id_-2-0-name" type="text" class="vTextField" name="-2-0-name" '
511 'maxlength="100">',
512 html=True,
513 )
514
515 @override_settings(USE_THOUSAND_SEPARATOR=True)
516 def test_localize_pk_shortcut(self):
517 """
518 The "View on Site" link is correct for locales that use thousand
519 separators.
520 """
521 holder = Holder.objects.create(pk=123456789, dummy=42)
522 inner = Inner.objects.create(pk=987654321, holder=holder, dummy=42, readonly="")
523 response = self.client.get(
524 reverse("admin:admin_inlines_holder_change", args=(holder.id,))
525 )
526 inner_shortcut = "r/%s/%s/" % (
527 ContentType.objects.get_for_model(inner).pk,
528 inner.pk,
529 )
530 self.assertContains(response, inner_shortcut)
531
532 def test_custom_pk_shortcut(self):
533 """
534 The "View on Site" link is correct for models with a custom primary key
535 field.
536 """
537 parent = ParentModelWithCustomPk.objects.create(my_own_pk="foo", name="Foo")
538 child1 = ChildModel1.objects.create(my_own_pk="bar", name="Bar", parent=parent)
539 child2 = ChildModel2.objects.create(my_own_pk="baz", name="Baz", parent=parent)
540 response = self.client.get(
541 reverse("admin:admin_inlines_parentmodelwithcustompk_change", args=("foo",))
542 )
543 child1_shortcut = "r/%s/%s/" % (
544 ContentType.objects.get_for_model(child1).pk,
545 child1.pk,
546 )
547 child2_shortcut = "r/%s/%s/" % (
548 ContentType.objects.get_for_model(child2).pk,
549 child2.pk,
550 )
551 self.assertContains(response, child1_shortcut)
552 self.assertContains(response, child2_shortcut)
553
554 def test_create_inlines_on_inherited_model(self):
555 """
556 An object can be created with inlines when it inherits another class.
557 """
558 data = {
559 "name": "Martian",
560 "sighting_set-TOTAL_FORMS": 1,
561 "sighting_set-INITIAL_FORMS": 0,
562 "sighting_set-MAX_NUM_FORMS": 0,
563 "sighting_set-0-place": "Zone 51",
564 "_save": "Save",
565 }
566 response = self.client.post(
567 reverse("admin:admin_inlines_extraterrestrial_add"), data
568 )
569 self.assertEqual(response.status_code, 302)
570 self.assertEqual(Sighting.objects.filter(et__name="Martian").count(), 1)
571
572 def test_custom_get_extra_form(self):
573 bt_head = BinaryTree.objects.create(name="Tree Head")
574 BinaryTree.objects.create(name="First Child", parent=bt_head)
575 # The maximum number of forms should respect 'get_max_num' on the
576 # ModelAdmin
577 max_forms_input = (
578 '<input id="id_binarytree_set-MAX_NUM_FORMS" '
579 'name="binarytree_set-MAX_NUM_FORMS" type="hidden" value="%d">'
580 )
581 # The total number of forms will remain the same in either case
582 total_forms_hidden = (
583 '<input id="id_binarytree_set-TOTAL_FORMS" '
584 'name="binarytree_set-TOTAL_FORMS" type="hidden" value="2">'
585 )
586 response = self.client.get(reverse("admin:admin_inlines_binarytree_add"))
587 self.assertInHTML(max_forms_input % 3, response.rendered_content)
588 self.assertInHTML(total_forms_hidden, response.rendered_content)
589
590 response = self.client.get(
591 reverse("admin:admin_inlines_binarytree_change", args=(bt_head.id,))
592 )
593 self.assertInHTML(max_forms_input % 2, response.rendered_content)
594 self.assertInHTML(total_forms_hidden, response.rendered_content)
595
596 def test_min_num(self):
597 """
598 min_num and extra determine number of forms.
599 """
600
601 class MinNumInline(TabularInline):
602 model = BinaryTree
603 min_num = 2
604 extra = 3
605
606 modeladmin = ModelAdmin(BinaryTree, admin_site)
607 modeladmin.inlines = [MinNumInline]
608 min_forms = (
609 '<input id="id_binarytree_set-MIN_NUM_FORMS" '
610 'name="binarytree_set-MIN_NUM_FORMS" type="hidden" value="2">'
611 )
612 total_forms = (
613 '<input id="id_binarytree_set-TOTAL_FORMS" '
614 'name="binarytree_set-TOTAL_FORMS" type="hidden" value="5">'
615 )
616 request = self.factory.get(reverse("admin:admin_inlines_binarytree_add"))
617 request.user = User(username="super", is_superuser=True)
618 response = modeladmin.changeform_view(request)
619 self.assertInHTML(min_forms, response.rendered_content)
620 self.assertInHTML(total_forms, response.rendered_content)
621
622 def test_custom_min_num(self):
623 bt_head = BinaryTree.objects.create(name="Tree Head")
624 BinaryTree.objects.create(name="First Child", parent=bt_head)
625
626 class MinNumInline(TabularInline):
627 model = BinaryTree
628 extra = 3
629
630 def get_min_num(self, request, obj=None, **kwargs):
631 if obj:
632 return 5
633 return 2
634
635 modeladmin = ModelAdmin(BinaryTree, admin_site)
636 modeladmin.inlines = [MinNumInline]
637 min_forms = (
638 '<input id="id_binarytree_set-MIN_NUM_FORMS" '
639 'name="binarytree_set-MIN_NUM_FORMS" type="hidden" value="%d">'
640 )
641 total_forms = (
642 '<input id="id_binarytree_set-TOTAL_FORMS" '
643 'name="binarytree_set-TOTAL_FORMS" type="hidden" value="%d">'
644 )
645 request = self.factory.get(reverse("admin:admin_inlines_binarytree_add"))
646 request.user = User(username="super", is_superuser=True)
647 response = modeladmin.changeform_view(request)
648 self.assertInHTML(min_forms % 2, response.rendered_content)
649 self.assertInHTML(total_forms % 5, response.rendered_content)
650
651 request = self.factory.get(
652 reverse("admin:admin_inlines_binarytree_change", args=(bt_head.id,))
653 )
654 request.user = User(username="super", is_superuser=True)
655 response = modeladmin.changeform_view(request, object_id=str(bt_head.id))
656 self.assertInHTML(min_forms % 5, response.rendered_content)
657 self.assertInHTML(total_forms % 8, response.rendered_content)
658
659 def test_inline_nonauto_noneditable_pk(self):
660 response = self.client.get(reverse("admin:admin_inlines_author_add"))
661 self.assertContains(
662 response,
663 '<input id="id_nonautopkbook_set-0-rand_pk" '
664 'name="nonautopkbook_set-0-rand_pk" type="hidden">',
665 html=True,
666 )
667 self.assertContains(
668 response,
669 '<input id="id_nonautopkbook_set-2-0-rand_pk" '
670 'name="nonautopkbook_set-2-0-rand_pk" type="hidden">',
671 html=True,
672 )
673
674 def test_inline_nonauto_noneditable_inherited_pk(self):
675 response = self.client.get(reverse("admin:admin_inlines_author_add"))
676 self.assertContains(
677 response,
678 '<input id="id_nonautopkbookchild_set-0-nonautopkbook_ptr" '
679 'name="nonautopkbookchild_set-0-nonautopkbook_ptr" type="hidden">',
680 html=True,
681 )
682 self.assertContains(
683 response,
684 '<input id="id_nonautopkbookchild_set-2-nonautopkbook_ptr" '
685 'name="nonautopkbookchild_set-2-nonautopkbook_ptr" type="hidden">',
686 html=True,
687 )
688
689 def test_inline_editable_pk(self):
690 response = self.client.get(reverse("admin:admin_inlines_author_add"))
691 self.assertContains(
692 response,
693 '<input class="vIntegerField" id="id_editablepkbook_set-0-manual_pk" '
694 'name="editablepkbook_set-0-manual_pk" type="number">',
695 html=True,
696 count=1,
697 )
698 self.assertContains(
699 response,
700 '<input class="vIntegerField" id="id_editablepkbook_set-2-0-manual_pk" '
701 'name="editablepkbook_set-2-0-manual_pk" type="number">',
702 html=True,
703 count=1,
704 )
705
706 def test_stacked_inline_edit_form_contains_has_original_class(self):
707 holder = Holder.objects.create(dummy=1)
708 holder.inner_set.create(dummy=1)
709 response = self.client.get(
710 reverse("admin:admin_inlines_holder_change", args=(holder.pk,))
711 )
712 self.assertContains(
713 response,
714 '<div class="inline-related has_original" id="inner_set-0">',
715 count=1,
716 )
717 self.assertContains(
718 response, '<div class="inline-related" id="inner_set-1">', count=1
719 )
720
721 def test_inlines_show_change_link_registered(self):
722 "Inlines `show_change_link` for registered models when enabled."
723 holder = Holder4.objects.create(dummy=1)
724 item1 = Inner4Stacked.objects.create(dummy=1, holder=holder)
725 item2 = Inner4Tabular.objects.create(dummy=1, holder=holder)
726 items = (
727 ("inner4stacked", item1.pk),
728 ("inner4tabular", item2.pk),
729 )
730 response = self.client.get(
731 reverse("admin:admin_inlines_holder4_change", args=(holder.pk,))
732 )
733 self.assertTrue(
734 response.context["inline_admin_formset"].opts.has_registered_model
735 )
736 for model, pk in items:
737 url = reverse("admin:admin_inlines_%s_change" % model, args=(pk,))
738 self.assertContains(
739 response, '<a href="%s" %s' % (url, INLINE_CHANGELINK_HTML)
740 )
741
742 def test_inlines_show_change_link_unregistered(self):
743 "Inlines `show_change_link` disabled for unregistered models."
744 parent = ParentModelWithCustomPk.objects.create(my_own_pk="foo", name="Foo")
745 ChildModel1.objects.create(my_own_pk="bar", name="Bar", parent=parent)
746 ChildModel2.objects.create(my_own_pk="baz", name="Baz", parent=parent)
747 response = self.client.get(
748 reverse("admin:admin_inlines_parentmodelwithcustompk_change", args=("foo",))
749 )
750 self.assertFalse(
751 response.context["inline_admin_formset"].opts.has_registered_model
752 )
753 self.assertNotContains(response, INLINE_CHANGELINK_HTML)
754
755 def test_tabular_inline_show_change_link_false_registered(self):
756 "Inlines `show_change_link` disabled by default."
757 poll = Poll.objects.create(name="New poll")
758 Question.objects.create(poll=poll)
759 response = self.client.get(
760 reverse("admin:admin_inlines_poll_change", args=(poll.pk,))
761 )
762 self.assertTrue(
763 response.context["inline_admin_formset"].opts.has_registered_model
764 )
765 self.assertNotContains(response, INLINE_CHANGELINK_HTML)
766
767 def test_noneditable_inline_has_field_inputs(self):
768 """Inlines without change permission shows field inputs on add form."""
769 response = self.client.get(
770 reverse("admin:admin_inlines_novelreadonlychapter_add")
771 )
772 self.assertContains(
773 response,
774 '<input type="text" name="chapter_set-0-name" '
775 'class="vTextField" maxlength="40" id="id_chapter_set-0-name">',
776 html=True,
777 )
778
779 def test_inlines_plural_heading_foreign_key(self):
780 response = self.client.get(reverse("admin:admin_inlines_holder4_add"))
781 self.assertContains(
782 response,
783 (
784 '<h2 id="inner4stacked_set-heading" class="inline-heading">'
785 "Inner4 stackeds</h2>"
786 ),
787 html=True,
788 )
789 self.assertContains(
790 response,
791 (
792 '<h2 id="inner4tabular_set-heading" class="inline-heading">'
793 "Inner4 tabulars</h2>"
794 ),
795 html=True,
796 )
797
798 def test_inlines_singular_heading_one_to_one(self):
799 response = self.client.get(reverse("admin:admin_inlines_person_add"))
800 self.assertContains(
801 response,
802 '<h2 id="author-heading" class="inline-heading">Author</h2>',
803 html=True,
804 ) # Tabular.
805 self.assertContains(
806 response,
807 '<h2 id="fashionista-heading" class="inline-heading">Fashionista</h2>',
808 html=True,
809 ) # Stacked.
810
811 def test_inlines_based_on_model_state(self):
812 parent = ShowInlineParent.objects.create(show_inlines=False)
813 data = {
814 "show_inlines": "on",
815 "_save": "Save",
816 }
817 change_url = reverse(
818 "admin:admin_inlines_showinlineparent_change",
819 args=(parent.id,),
820 )
821 response = self.client.post(change_url, data)
822 self.assertEqual(response.status_code, 302)
823 parent.refresh_from_db()
824 self.assertIs(parent.show_inlines, True)
825
826
827 @override_settings(ROOT_URLCONF="admin_inlines.urls")
828 class TestInlineMedia(TestDataMixin, TestCase):
829 def setUp(self):
830 self.client.force_login(self.superuser)
831
832 def test_inline_media_only_base(self):
833 holder = Holder(dummy=13)
834 holder.save()
835 Inner(dummy=42, holder=holder).save()
836 change_url = reverse("admin:admin_inlines_holder_change", args=(holder.id,))
837 response = self.client.get(change_url)
838 self.assertContains(response, "my_awesome_admin_scripts.js")
839
840 def test_inline_media_only_inline(self):
841 holder = Holder3(dummy=13)
842 holder.save()
843 Inner3(dummy=42, holder=holder).save()
844 change_url = reverse("admin:admin_inlines_holder3_change", args=(holder.id,))
845 response = self.client.get(change_url)
846 self.assertEqual(
847 response.context["inline_admin_formsets"][0].media._js,
848 [
849 "admin/js/vendor/jquery/jquery.min.js",
850 "my_awesome_inline_scripts.js",
851 "custom_number.js",
852 "admin/js/jquery.init.js",
853 "admin/js/inlines.js",
854 ],
855 )
856 self.assertContains(response, "my_awesome_inline_scripts.js")
857
858 def test_all_inline_media(self):
859 holder = Holder2(dummy=13)
860 holder.save()
861 Inner2(dummy=42, holder=holder).save()
862 change_url = reverse("admin:admin_inlines_holder2_change", args=(holder.id,))
863 response = self.client.get(change_url)
864 self.assertContains(response, "my_awesome_admin_scripts.js")
865 self.assertContains(response, "my_awesome_inline_scripts.js")
866
867
868 @override_settings(ROOT_URLCONF="admin_inlines.urls")
869 class TestInlineAdminForm(TestCase):
870 def test_immutable_content_type(self):
871 """Regression for #9362
872 The problem depends only on InlineAdminForm and its "original"
873 argument, so we can safely set the other arguments to None/{}. We just
874 need to check that the content_type argument of Child isn't altered by
875 the internals of the inline form."""
876
877 sally = Teacher.objects.create(name="Sally")
878 john = Parent.objects.create(name="John")
879 joe = Child.objects.create(name="Joe", teacher=sally, parent=john)
880
881 iaf = InlineAdminForm(None, None, {}, {}, joe)
882 parent_ct = ContentType.objects.get_for_model(Parent)
883 self.assertEqual(iaf.original.content_type, parent_ct)
884
885
886 @override_settings(ROOT_URLCONF="admin_inlines.urls")
887 class TestInlineProtectedOnDelete(TestDataMixin, TestCase):
888 def setUp(self):
889 self.client.force_login(self.superuser)
890
891 def test_deleting_inline_with_protected_delete_does_not_validate(self):
892 lotr = Novel.objects.create(name="Lord of the rings")
893 chapter = Chapter.objects.create(novel=lotr, name="Many Meetings")
894 foot_note = FootNote.objects.create(chapter=chapter, note="yadda yadda")
895
896 change_url = reverse("admin:admin_inlines_novel_change", args=(lotr.id,))
897 response = self.client.get(change_url)
898 data = {
899 "name": lotr.name,
900 "chapter_set-TOTAL_FORMS": 1,
901 "chapter_set-INITIAL_FORMS": 1,
902 "chapter_set-MAX_NUM_FORMS": 1000,
903 "_save": "Save",
904 "chapter_set-0-id": chapter.id,
905 "chapter_set-0-name": chapter.name,
906 "chapter_set-0-novel": lotr.id,
907 "chapter_set-0-DELETE": "on",
908 }
909 response = self.client.post(change_url, data)
910 self.assertContains(
911 response,
912 "Deleting chapter %s would require deleting "
913 "the following protected related objects: foot note %s"
914 % (chapter, foot_note),
915 )
916
917
918 @override_settings(ROOT_URLCONF="admin_inlines.urls")
919 class TestInlinePermissions(TestCase):
920 """
921 Make sure the admin respects permissions for objects that are edited
922 inline. Refs #8060.
923 """
924
925 @classmethod
926 def setUpTestData(cls):
927 cls.user = User(username="admin", is_staff=True, is_active=True)
928 cls.user.set_password("secret")
929 cls.user.save()
930
931 cls.author_ct = ContentType.objects.get_for_model(Author)
932 cls.holder_ct = ContentType.objects.get_for_model(Holder2)
933 cls.book_ct = ContentType.objects.get_for_model(Book)
934 cls.inner_ct = ContentType.objects.get_for_model(Inner2)
935
936 # User always has permissions to add and change Authors, and Holders,
937 # the main (parent) models of the inlines. Permissions on the inlines
938 # vary per test.
939 permission = Permission.objects.get(
940 codename="add_author", content_type=cls.author_ct
941 )
942 cls.user.user_permissions.add(permission)
943 permission = Permission.objects.get(
944 codename="change_author", content_type=cls.author_ct
945 )
946 cls.user.user_permissions.add(permission)
947 permission = Permission.objects.get(
948 codename="add_holder2", content_type=cls.holder_ct
949 )
950 cls.user.user_permissions.add(permission)
951 permission = Permission.objects.get(
952 codename="change_holder2", content_type=cls.holder_ct
953 )
954 cls.user.user_permissions.add(permission)
955
956 author = Author.objects.create(pk=1, name="The Author")
957 cls.book = author.books.create(name="The inline Book")
958 cls.author_change_url = reverse(
959 "admin:admin_inlines_author_change", args=(author.id,)
960 )
961 # Get the ID of the automatically created intermediate model for the
962 # Author-Book m2m.
963 author_book_auto_m2m_intermediate = Author.books.through.objects.get(
964 author=author, book=cls.book
965 )
966 cls.author_book_auto_m2m_intermediate_id = author_book_auto_m2m_intermediate.pk
967
968 cls.holder = Holder2.objects.create(dummy=13)
969 cls.inner2 = Inner2.objects.create(dummy=42, holder=cls.holder)
970
971 def setUp(self):
972 self.holder_change_url = reverse(
973 "admin:admin_inlines_holder2_change", args=(self.holder.id,)
974 )
975 self.client.force_login(self.user)
976
977 def test_inline_add_m2m_noperm(self):
978 response = self.client.get(reverse("admin:admin_inlines_author_add"))
979 # No change permission on books, so no inline
980 self.assertNotContains(
981 response,
982 (
983 '<h2 id="Author_books-heading" class="inline-heading">'
984 "Author-book relationships</h2>"
985 ),
986 html=True,
987 )
988 self.assertNotContains(response, "Add another Author-Book Relationship")
989 self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"')
990
991 def test_inline_add_fk_noperm(self):
992 response = self.client.get(reverse("admin:admin_inlines_holder2_add"))
993 # No permissions on Inner2s, so no inline
994 self.assertNotContains(
995 response,
996 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
997 html=True,
998 )
999 self.assertNotContains(response, "Add another Inner2")
1000 self.assertNotContains(response, 'id="id_inner2_set-TOTAL_FORMS"')
1001
1002 def test_inline_change_m2m_noperm(self):
1003 response = self.client.get(self.author_change_url)
1004 # No change permission on books, so no inline
1005 self.assertNotContains(
1006 response,
1007 (
1008 '<h2 id="Author_books-heading" class="inline-heading">'
1009 "Author-book relationships</h2>"
1010 ),
1011 html=True,
1012 )
1013 self.assertNotContains(response, "Add another Author-Book Relationship")
1014 self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"')
1015
1016 def test_inline_change_fk_noperm(self):
1017 response = self.client.get(self.holder_change_url)
1018 # No permissions on Inner2s, so no inline
1019 self.assertNotContains(
1020 response,
1021 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1022 html=True,
1023 )
1024 self.assertNotContains(response, "Add another Inner2")
1025 self.assertNotContains(response, 'id="id_inner2_set-TOTAL_FORMS"')
1026
1027 def test_inline_add_m2m_view_only_perm(self):
1028 permission = Permission.objects.get(
1029 codename="view_book", content_type=self.book_ct
1030 )
1031 self.user.user_permissions.add(permission)
1032 response = self.client.get(reverse("admin:admin_inlines_author_add"))
1033 # View-only inlines. (It could be nicer to hide the empty, non-editable
1034 # inlines on the add page.)
1035 self.assertIs(
1036 response.context["inline_admin_formset"].has_view_permission, True
1037 )
1038 self.assertIs(
1039 response.context["inline_admin_formset"].has_add_permission, False
1040 )
1041 self.assertIs(
1042 response.context["inline_admin_formset"].has_change_permission, False
1043 )
1044 self.assertIs(
1045 response.context["inline_admin_formset"].has_delete_permission, False
1046 )
1047 self.assertContains(
1048 response,
1049 (
1050 '<h2 id="Author_books-heading" class="inline-heading">'
1051 "Author-book relationships</h2>"
1052 ),
1053 html=True,
1054 )
1055 self.assertContains(
1056 response,
1057 '<input type="hidden" name="Author_books-TOTAL_FORMS" value="0" '
1058 'id="id_Author_books-TOTAL_FORMS">',
1059 html=True,
1060 )
1061 self.assertNotContains(response, "Add another Author-Book Relationship")
1062
1063 def test_inline_add_m2m_add_perm(self):
1064 permission = Permission.objects.get(
1065 codename="add_book", content_type=self.book_ct
1066 )
1067 self.user.user_permissions.add(permission)
1068 response = self.client.get(reverse("admin:admin_inlines_author_add"))
1069 # No change permission on Books, so no inline
1070 self.assertNotContains(
1071 response,
1072 (
1073 '<h2 id="Author_books-heading" class="inline-heading">'
1074 "Author-book relationships</h2>"
1075 ),
1076 html=True,
1077 )
1078 self.assertNotContains(response, "Add another Author-Book Relationship")
1079 self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"')
1080
1081 def test_inline_add_fk_add_perm(self):
1082 permission = Permission.objects.get(
1083 codename="add_inner2", content_type=self.inner_ct
1084 )
1085 self.user.user_permissions.add(permission)
1086 response = self.client.get(reverse("admin:admin_inlines_holder2_add"))
1087 # Add permission on inner2s, so we get the inline
1088 self.assertContains(
1089 response,
1090 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1091 html=True,
1092 )
1093 self.assertContains(response, "Add another Inner2")
1094 self.assertContains(
1095 response,
1096 '<input type="hidden" id="id_inner2_set-TOTAL_FORMS" '
1097 'value="3" name="inner2_set-TOTAL_FORMS">',
1098 html=True,
1099 )
1100
1101 def test_inline_change_m2m_add_perm(self):
1102 permission = Permission.objects.get(
1103 codename="add_book", content_type=self.book_ct
1104 )
1105 self.user.user_permissions.add(permission)
1106 response = self.client.get(self.author_change_url)
1107 # No change permission on books, so no inline
1108 self.assertNotContains(
1109 response,
1110 (
1111 '<h2 id="Author_books-heading" class="inline-heading">'
1112 "Author-book relationships</h2>"
1113 ),
1114 html=True,
1115 )
1116 self.assertNotContains(response, "Add another Author-Book Relationship")
1117 self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"')
1118 self.assertNotContains(response, 'id="id_Author_books-0-DELETE"')
1119
1120 def test_inline_change_m2m_view_only_perm(self):
1121 permission = Permission.objects.get(
1122 codename="view_book", content_type=self.book_ct
1123 )
1124 self.user.user_permissions.add(permission)
1125 response = self.client.get(self.author_change_url)
1126 # View-only inlines.
1127 self.assertIs(
1128 response.context["inline_admin_formset"].has_view_permission, True
1129 )
1130 self.assertIs(
1131 response.context["inline_admin_formset"].has_add_permission, False
1132 )
1133 self.assertIs(
1134 response.context["inline_admin_formset"].has_change_permission, False
1135 )
1136 self.assertIs(
1137 response.context["inline_admin_formset"].has_delete_permission, False
1138 )
1139 self.assertContains(
1140 response,
1141 (
1142 '<h2 id="Author_books-heading" class="inline-heading">'
1143 "Author-book relationships</h2>"
1144 ),
1145 html=True,
1146 )
1147 self.assertContains(
1148 response,
1149 '<input type="hidden" name="Author_books-TOTAL_FORMS" value="1" '
1150 'id="id_Author_books-TOTAL_FORMS">',
1151 html=True,
1152 )
1153 # The field in the inline is read-only.
1154 self.assertContains(response, "<p>%s</p>" % self.book)
1155 self.assertNotContains(
1156 response,
1157 '<input type="checkbox" name="Author_books-0-DELETE" '
1158 'id="id_Author_books-0-DELETE">',
1159 html=True,
1160 )
1161
1162 def test_inline_change_m2m_change_perm(self):
1163 permission = Permission.objects.get(
1164 codename="change_book", content_type=self.book_ct
1165 )
1166 self.user.user_permissions.add(permission)
1167 response = self.client.get(self.author_change_url)
1168 # We have change perm on books, so we can add/change/delete inlines
1169 self.assertIs(
1170 response.context["inline_admin_formset"].has_view_permission, True
1171 )
1172 self.assertIs(response.context["inline_admin_formset"].has_add_permission, True)
1173 self.assertIs(
1174 response.context["inline_admin_formset"].has_change_permission, True
1175 )
1176 self.assertIs(
1177 response.context["inline_admin_formset"].has_delete_permission, True
1178 )
1179 self.assertContains(
1180 response,
1181 (
1182 '<h2 id="Author_books-heading" class="inline-heading">'
1183 "Author-book relationships</h2>"
1184 ),
1185 html=True,
1186 )
1187 self.assertContains(response, "Add another Author-book relationship")
1188 self.assertContains(
1189 response,
1190 '<input type="hidden" id="id_Author_books-TOTAL_FORMS" '
1191 'value="4" name="Author_books-TOTAL_FORMS">',
1192 html=True,
1193 )
1194 self.assertContains(
1195 response,
1196 '<input type="hidden" id="id_Author_books-0-id" value="%i" '
1197 'name="Author_books-0-id">' % self.author_book_auto_m2m_intermediate_id,
1198 html=True,
1199 )
1200 self.assertContains(response, 'id="id_Author_books-0-DELETE"')
1201
1202 def test_inline_change_fk_add_perm(self):
1203 permission = Permission.objects.get(
1204 codename="add_inner2", content_type=self.inner_ct
1205 )
1206 self.user.user_permissions.add(permission)
1207 response = self.client.get(self.holder_change_url)
1208 # Add permission on inner2s, so we can add but not modify existing
1209 self.assertContains(
1210 response,
1211 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1212 html=True,
1213 )
1214 self.assertContains(response, "Add another Inner2")
1215 # 3 extra forms only, not the existing instance form
1216 self.assertContains(
1217 response,
1218 '<input type="hidden" id="id_inner2_set-TOTAL_FORMS" value="3" '
1219 'name="inner2_set-TOTAL_FORMS">',
1220 html=True,
1221 )
1222 self.assertNotContains(
1223 response,
1224 '<input type="hidden" id="id_inner2_set-0-id" value="%i" '
1225 'name="inner2_set-0-id">' % self.inner2.id,
1226 html=True,
1227 )
1228
1229 def test_inline_change_fk_change_perm(self):
1230 permission = Permission.objects.get(
1231 codename="change_inner2", content_type=self.inner_ct
1232 )
1233 self.user.user_permissions.add(permission)
1234 response = self.client.get(self.holder_change_url)
1235 # Change permission on inner2s, so we can change existing but not add new
1236 self.assertContains(
1237 response,
1238 '<h2 id="inner2_set-heading" class="inline-heading">Inner2s</h2>',
1239 html=True,
1240 )
1241 self.assertContains(
1242 response,
1243 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1244 html=True,
1245 )
1246 # Just the one form for existing instances
1247 self.assertContains(
1248 response,
1249 '<input type="hidden" id="id_inner2_set-TOTAL_FORMS" value="1" '
1250 'name="inner2_set-TOTAL_FORMS">',
1251 html=True,
1252 )
1253 self.assertContains(
1254 response,
1255 '<input type="hidden" id="id_inner2_set-0-id" value="%i" '
1256 'name="inner2_set-0-id">' % self.inner2.id,
1257 html=True,
1258 )
1259 # max-num 0 means we can't add new ones
1260 self.assertContains(
1261 response,
1262 '<input type="hidden" id="id_inner2_set-MAX_NUM_FORMS" value="0" '
1263 'name="inner2_set-MAX_NUM_FORMS">',
1264 html=True,
1265 )
1266 # TabularInline
1267 self.assertContains(
1268 response, '<th class="column-dummy required">Dummy</th>', html=True
1269 )
1270 self.assertContains(
1271 response,
1272 '<input type="number" name="inner2_set-2-0-dummy" value="%s" '
1273 'class="vIntegerField" id="id_inner2_set-2-0-dummy">' % self.inner2.dummy,
1274 html=True,
1275 )
1276
1277 def test_inline_change_fk_add_change_perm(self):
1278 permission = Permission.objects.get(
1279 codename="add_inner2", content_type=self.inner_ct
1280 )
1281 self.user.user_permissions.add(permission)
1282 permission = Permission.objects.get(
1283 codename="change_inner2", content_type=self.inner_ct
1284 )
1285 self.user.user_permissions.add(permission)
1286 response = self.client.get(self.holder_change_url)
1287 # Add/change perm, so we can add new and change existing
1288 self.assertContains(
1289 response,
1290 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1291 html=True,
1292 )
1293 # One form for existing instance and three extra for new
1294 self.assertContains(
1295 response,
1296 '<input type="hidden" id="id_inner2_set-TOTAL_FORMS" value="4" '
1297 'name="inner2_set-TOTAL_FORMS">',
1298 html=True,
1299 )
1300 self.assertContains(
1301 response,
1302 '<input type="hidden" id="id_inner2_set-0-id" value="%i" '
1303 'name="inner2_set-0-id">' % self.inner2.id,
1304 html=True,
1305 )
1306
1307 def test_inline_change_fk_change_del_perm(self):
1308 permission = Permission.objects.get(
1309 codename="change_inner2", content_type=self.inner_ct
1310 )
1311 self.user.user_permissions.add(permission)
1312 permission = Permission.objects.get(
1313 codename="delete_inner2", content_type=self.inner_ct
1314 )
1315 self.user.user_permissions.add(permission)
1316 response = self.client.get(self.holder_change_url)
1317 # Change/delete perm on inner2s, so we can change/delete existing
1318 self.assertContains(
1319 response,
1320 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1321 html=True,
1322 )
1323 # One form for existing instance only, no new
1324 self.assertContains(
1325 response,
1326 '<input type="hidden" id="id_inner2_set-TOTAL_FORMS" value="1" '
1327 'name="inner2_set-TOTAL_FORMS">',
1328 html=True,
1329 )
1330 self.assertContains(
1331 response,
1332 '<input type="hidden" id="id_inner2_set-0-id" value="%i" '
1333 'name="inner2_set-0-id">' % self.inner2.id,
1334 html=True,
1335 )
1336 self.assertContains(response, 'id="id_inner2_set-0-DELETE"')
1337
1338 def test_inline_change_fk_all_perms(self):
1339 permission = Permission.objects.get(
1340 codename="add_inner2", content_type=self.inner_ct
1341 )
1342 self.user.user_permissions.add(permission)
1343 permission = Permission.objects.get(
1344 codename="change_inner2", content_type=self.inner_ct
1345 )
1346 self.user.user_permissions.add(permission)
1347 permission = Permission.objects.get(
1348 codename="delete_inner2", content_type=self.inner_ct
1349 )
1350 self.user.user_permissions.add(permission)
1351 response = self.client.get(self.holder_change_url)
1352 # All perms on inner2s, so we can add/change/delete
1353 self.assertContains(
1354 response,
1355 '<h2 id="inner2_set-heading" class="inline-heading">Inner2s</h2>',
1356 html=True,
1357 )
1358 self.assertContains(
1359 response,
1360 '<h2 id="inner2_set-2-heading" class="inline-heading">Inner2s</h2>',
1361 html=True,
1362 )
1363 # One form for existing instance only, three for new
1364 self.assertContains(
1365 response,
1366 '<input type="hidden" id="id_inner2_set-TOTAL_FORMS" value="4" '
1367 'name="inner2_set-TOTAL_FORMS">',
1368 html=True,
1369 )
1370 self.assertContains(
1371 response,
1372 '<input type="hidden" id="id_inner2_set-0-id" value="%i" '
1373 'name="inner2_set-0-id">' % self.inner2.id,
1374 html=True,
1375 )
1376 self.assertContains(response, 'id="id_inner2_set-0-DELETE"')
1377 # TabularInline
1378 self.assertContains(
1379 response, '<th class="column-dummy required">Dummy</th>', html=True
1380 )
1381 self.assertContains(
1382 response,
1383 '<input type="number" name="inner2_set-2-0-dummy" value="%s" '
1384 'class="vIntegerField" id="id_inner2_set-2-0-dummy">' % self.inner2.dummy,
1385 html=True,
1386 )
1387
1388
1389 @override_settings(ROOT_URLCONF="admin_inlines.urls")
1390 class TestReadOnlyChangeViewInlinePermissions(TestCase):
1391 @classmethod
1392 def setUpTestData(cls):
1393 cls.user = User.objects.create_user(
1394 "testing", password="password", is_staff=True
1395 )
1396 cls.user.user_permissions.add(
1397 Permission.objects.get(
1398 codename="view_poll",
1399 content_type=ContentType.objects.get_for_model(Poll),
1400 )
1401 )
1402 cls.user.user_permissions.add(
1403 *Permission.objects.filter(
1404 codename__endswith="question",
1405 content_type=ContentType.objects.get_for_model(Question),
1406 ).values_list("pk", flat=True)
1407 )
1408
1409 cls.poll = Poll.objects.create(name="Survey")
1410 cls.add_url = reverse("admin:admin_inlines_poll_add")
1411 cls.change_url = reverse("admin:admin_inlines_poll_change", args=(cls.poll.id,))
1412
1413 def setUp(self):
1414 self.client.force_login(self.user)
1415
1416 def test_add_url_not_allowed(self):
1417 response = self.client.get(self.add_url)
1418 self.assertEqual(response.status_code, 403)
1419
1420 response = self.client.post(self.add_url, {})
1421 self.assertEqual(response.status_code, 403)
1422
1423 def test_post_to_change_url_not_allowed(self):
1424 response = self.client.post(self.change_url, {})
1425 self.assertEqual(response.status_code, 403)
1426
1427 def test_get_to_change_url_is_allowed(self):
1428 response = self.client.get(self.change_url)
1429 self.assertEqual(response.status_code, 200)
1430
1431 def test_main_model_is_rendered_as_read_only(self):
1432 response = self.client.get(self.change_url)
1433 self.assertContains(
1434 response, '<div class="readonly">%s</div>' % self.poll.name, html=True
1435 )
1436 input = (
1437 '<input type="text" name="name" value="%s" class="vTextField" '
1438 'maxlength="40" required id="id_name">'
1439 )
1440 self.assertNotContains(response, input % self.poll.name, html=True)
1441
1442 def test_inlines_are_rendered_as_read_only(self):
1443 question = Question.objects.create(
1444 text="How will this be rendered?", poll=self.poll
1445 )
1446 response = self.client.get(self.change_url)
1447 self.assertContains(
1448 response, '<td class="field-text"><p>%s</p></td>' % question.text, html=True
1449 )
1450 self.assertNotContains(response, 'id="id_question_set-0-text"')
1451 self.assertNotContains(response, 'id="id_related_objs-0-DELETE"')
1452
1453 def test_submit_line_shows_only_close_button(self):
1454 response = self.client.get(self.change_url)
1455 self.assertContains(
1456 response,
1457 '<a href="/admin/admin_inlines/poll/" class="closelink">Close</a>',
1458 html=True,
1459 )
1460 delete_link = (
1461 '<a href="/admin/admin_inlines/poll/%s/delete/" class="deletelink">Delete'
1462 "</a>"
1463 )
1464 self.assertNotContains(response, delete_link % self.poll.id, html=True)
1465 self.assertNotContains(
1466 response,
1467 '<input type="submit" value="Save and add another" name="_addanother">',
1468 )
1469 self.assertNotContains(
1470 response,
1471 '<input type="submit" value="Save and continue editing" name="_continue">',
1472 )
1473
1474 def test_inline_delete_buttons_are_not_shown(self):
1475 Question.objects.create(text="How will this be rendered?", poll=self.poll)
1476 response = self.client.get(self.change_url)
1477 self.assertNotContains(
1478 response,
1479 '<input type="checkbox" name="question_set-0-DELETE" '
1480 'id="id_question_set-0-DELETE">',
1481 html=True,
1482 )
1483
1484 def test_extra_inlines_are_not_shown(self):
1485 response = self.client.get(self.change_url)
1486 self.assertNotContains(response, 'id="id_question_set-0-text"')
1487
1488
1489 @override_settings(ROOT_URLCONF="admin_inlines.urls")
1490 class TestVerboseNameInlineForms(TestDataMixin, TestCase):
1491 factory = RequestFactory()
1492
1493 def test_verbose_name_inline(self):
1494 class NonVerboseProfileInline(TabularInline):
1495 model = Profile
1496 verbose_name = "Non-verbose childs"
1497
1498 class VerboseNameProfileInline(TabularInline):
1499 model = VerboseNameProfile
1500 verbose_name = "Childs with verbose name"
1501
1502 class VerboseNamePluralProfileInline(TabularInline):
1503 model = VerboseNamePluralProfile
1504 verbose_name = "Childs with verbose name plural"
1505
1506 class BothVerboseNameProfileInline(TabularInline):
1507 model = BothVerboseNameProfile
1508 verbose_name = "Childs with both verbose names"
1509
1510 modeladmin = ModelAdmin(ProfileCollection, admin_site)
1511 modeladmin.inlines = [
1512 NonVerboseProfileInline,
1513 VerboseNameProfileInline,
1514 VerboseNamePluralProfileInline,
1515 BothVerboseNameProfileInline,
1516 ]
1517 obj = ProfileCollection.objects.create()
1518 url = reverse("admin:admin_inlines_profilecollection_change", args=(obj.pk,))
1519 request = self.factory.get(url)
1520 request.user = self.superuser
1521 response = modeladmin.changeform_view(request)
1522 self.assertNotContains(response, "Add another Profile")
1523 # Non-verbose model.
1524 self.assertContains(
1525 response,
1526 (
1527 '<h2 id="profile_set-heading" class="inline-heading">'
1528 "Non-verbose childss</h2>"
1529 ),
1530 html=True,
1531 )
1532 self.assertContains(response, "Add another Non-verbose child")
1533 self.assertNotContains(
1534 response,
1535 '<h2 id="profile_set-heading" class="inline-heading">Profiles</h2>',
1536 html=True,
1537 )
1538 # Model with verbose name.
1539 self.assertContains(
1540 response,
1541 (
1542 '<h2 id="verbosenameprofile_set-heading" class="inline-heading">'
1543 "Childs with verbose names</h2>"
1544 ),
1545 html=True,
1546 )
1547 self.assertContains(response, "Add another Childs with verbose name")
1548 self.assertNotContains(
1549 response,
1550 '<h2 id="verbosenameprofile_set-heading" class="inline-heading">'
1551 "Model with verbose name onlys</h2>",
1552 html=True,
1553 )
1554 self.assertNotContains(response, "Add another Model with verbose name only")
1555 # Model with verbose name plural.
1556 self.assertContains(
1557 response,
1558 (
1559 '<h2 id="verbosenamepluralprofile_set-heading" class="inline-heading">'
1560 "Childs with verbose name plurals</h2>"
1561 ),
1562 html=True,
1563 )
1564 self.assertContains(response, "Add another Childs with verbose name plural")
1565 self.assertNotContains(
1566 response,
1567 '<h2 id="verbosenamepluralprofile_set-heading" class="inline-heading">'
1568 "Model with verbose name plural only</h2>",
1569 html=True,
1570 )
1571 # Model with both verbose names.
1572 self.assertContains(
1573 response,
1574 (
1575 '<h2 id="bothverbosenameprofile_set-heading" class="inline-heading">'
1576 "Childs with both verbose namess</h2>"
1577 ),
1578 html=True,
1579 )
1580 self.assertContains(response, "Add another Childs with both verbose names")
1581 self.assertNotContains(
1582 response,
1583 '<h2 id="bothverbosenameprofile_set-heading" class="inline-heading">'
1584 "Model with both - plural name</h2>",
1585 html=True,
1586 )
1587 self.assertNotContains(response, "Add another Model with both - name")
1588
1589 def test_verbose_name_plural_inline(self):
1590 class NonVerboseProfileInline(TabularInline):
1591 model = Profile
1592 verbose_name_plural = "Non-verbose childs"
1593
1594 class VerboseNameProfileInline(TabularInline):
1595 model = VerboseNameProfile
1596 verbose_name_plural = "Childs with verbose name"
1597
1598 class VerboseNamePluralProfileInline(TabularInline):
1599 model = VerboseNamePluralProfile
1600 verbose_name_plural = "Childs with verbose name plural"
1601
1602 class BothVerboseNameProfileInline(TabularInline):
1603 model = BothVerboseNameProfile
1604 verbose_name_plural = "Childs with both verbose names"
1605
1606 modeladmin = ModelAdmin(ProfileCollection, admin_site)
1607 modeladmin.inlines = [
1608 NonVerboseProfileInline,
1609 VerboseNameProfileInline,
1610 VerboseNamePluralProfileInline,
1611 BothVerboseNameProfileInline,
1612 ]
1613 obj = ProfileCollection.objects.create()
1614 url = reverse("admin:admin_inlines_profilecollection_change", args=(obj.pk,))
1615 request = self.factory.get(url)
1616 request.user = self.superuser
1617 response = modeladmin.changeform_view(request)
1618 # Non-verbose model.
1619 self.assertContains(
1620 response,
1621 (
1622 '<h2 id="profile_set-heading" class="inline-heading">'
1623 "Non-verbose childs</h2>"
1624 ),
1625 html=True,
1626 )
1627 self.assertContains(response, "Add another Profile")
1628 self.assertNotContains(
1629 response,
1630 '<h2 id="profile_set-heading" class="inline-heading">Profiles</h2>',
1631 html=True,
1632 )
1633 # Model with verbose name.
1634 self.assertContains(
1635 response,
1636 (
1637 '<h2 id="verbosenameprofile_set-heading" class="inline-heading">'
1638 "Childs with verbose name</h2>"
1639 ),
1640 html=True,
1641 )
1642 self.assertContains(response, "Add another Model with verbose name only")
1643 self.assertNotContains(
1644 response,
1645 '<h2 id="verbosenameprofile_set-heading" class="inline-heading">'
1646 "Model with verbose name onlys</h2>",
1647 html=True,
1648 )
1649 # Model with verbose name plural.
1650 self.assertContains(
1651 response,
1652 (
1653 '<h2 id="verbosenamepluralprofile_set-heading" class="inline-heading">'
1654 "Childs with verbose name plural</h2>"
1655 ),
1656 html=True,
1657 )
1658 self.assertContains(response, "Add another Profile")
1659 self.assertNotContains(
1660 response,
1661 '<h2 id="verbosenamepluralprofile_set-heading" class="inline-heading">'
1662 "Model with verbose name plural only</h2>",
1663 html=True,
1664 )
1665 # Model with both verbose names.
1666 self.assertContains(
1667 response,
1668 (
1669 '<h2 id="bothverbosenameprofile_set-heading" class="inline-heading">'
1670 "Childs with both verbose names</h2>"
1671 ),
1672 html=True,
1673 )
1674 self.assertContains(response, "Add another Model with both - name")
1675 self.assertNotContains(
1676 response,
1677 '<h2 id="bothverbosenameprofile_set-heading" class="inline-heading">'
1678 "Model with both - plural name</h2>",
1679 html=True,
1680 )
1681
1682 def test_both_verbose_names_inline(self):
1683 class NonVerboseProfileInline(TabularInline):
1684 model = Profile
1685 verbose_name = "Non-verbose childs - name"
1686 verbose_name_plural = "Non-verbose childs - plural name"
1687
1688 class VerboseNameProfileInline(TabularInline):
1689 model = VerboseNameProfile
1690 verbose_name = "Childs with verbose name - name"
1691 verbose_name_plural = "Childs with verbose name - plural name"
1692
1693 class VerboseNamePluralProfileInline(TabularInline):
1694 model = VerboseNamePluralProfile
1695 verbose_name = "Childs with verbose name plural - name"
1696 verbose_name_plural = "Childs with verbose name plural - plural name"
1697
1698 class BothVerboseNameProfileInline(TabularInline):
1699 model = BothVerboseNameProfile
1700 verbose_name = "Childs with both - name"
1701 verbose_name_plural = "Childs with both - plural name"
1702
1703 modeladmin = ModelAdmin(ProfileCollection, admin_site)
1704 modeladmin.inlines = [
1705 NonVerboseProfileInline,
1706 VerboseNameProfileInline,
1707 VerboseNamePluralProfileInline,
1708 BothVerboseNameProfileInline,
1709 ]
1710 obj = ProfileCollection.objects.create()
1711 url = reverse("admin:admin_inlines_profilecollection_change", args=(obj.pk,))
1712 request = self.factory.get(url)
1713 request.user = self.superuser
1714 response = modeladmin.changeform_view(request)
1715 self.assertNotContains(response, "Add another Profile")
1716 # Non-verbose model.
1717 self.assertContains(
1718 response,
1719 (
1720 '<h2 id="profile_set-heading" class="inline-heading">'
1721 "Non-verbose childs - plural name</h2>"
1722 ),
1723 html=True,
1724 )
1725 self.assertContains(response, "Add another Non-verbose childs - name")
1726 self.assertNotContains(
1727 response,
1728 '<h2 id="profile_set-heading" class="inline-heading">Profiles</h2>',
1729 html=True,
1730 )
1731 # Model with verbose name.
1732 self.assertContains(
1733 response,
1734 (
1735 '<h2 id="verbosenameprofile_set-heading" class="inline-heading">'
1736 "Childs with verbose name - plural name</h2>"
1737 ),
1738 html=True,
1739 )
1740 self.assertContains(response, "Add another Childs with verbose name - name")
1741 self.assertNotContains(
1742 response,
1743 '<h2 id="verbosenameprofile_set-heading" class="inline-heading">'
1744 "Model with verbose name onlys</h2>",
1745 html=True,
1746 )
1747 # Model with verbose name plural.
1748 self.assertContains(
1749 response,
1750 (
1751 '<h2 id="verbosenamepluralprofile_set-heading" class="inline-heading">'
1752 "Childs with verbose name plural - plural name</h2>"
1753 ),
1754 html=True,
1755 )
1756 self.assertContains(
1757 response,
1758 "Add another Childs with verbose name plural - name",
1759 )
1760 self.assertNotContains(
1761 response,
1762 '<h2 id="verbosenamepluralprofile_set-heading" class="inline-heading">'
1763 "Model with verbose name plural only</h2>",
1764 html=True,
1765 )
1766 # Model with both verbose names.
1767 self.assertContains(
1768 response,
1769 '<h2 id="bothverbosenameprofile_set-heading" class="inline-heading">'
1770 "Childs with both - plural name</h2>",
1771 html=True,
1772 )
1773 self.assertContains(response, "Add another Childs with both - name")
1774 self.assertNotContains(
1775 response,
1776 '<h2 id="bothverbosenameprofile_set-heading" class="inline-heading">'
1777 "Model with both - plural name</h2>",
1778 html=True,
1779 )
1780 self.assertNotContains(response, "Add another Model with both - name")
1781
1782
1783 @override_settings(ROOT_URLCONF="admin_inlines.urls")
1784 class TestInlineWithFieldsets(TestDataMixin, TestCase):
1785 def setUp(self):
1786 self.client.force_login(self.superuser)
1787
1788 @override_settings(DEBUG=True)
1789 def test_fieldset_context_fully_set(self):
1790 url = reverse("admin:admin_inlines_photographer_add")
1791 with self.assertRaisesMessage(AssertionError, "no logs"):
1792 with self.assertLogs("django.template", "DEBUG"):
1793 self.client.get(url)
1794
1795 def test_inline_headings(self):
1796 response = self.client.get(reverse("admin:admin_inlines_photographer_add"))
1797 # Page main title.
1798 self.assertContains(response, "<h1>Add photographer</h1>", html=True)
1799
1800 # Headings for the toplevel fieldsets. The first one has no name.
1801 self.assertContains(response, '<fieldset class="module aligned ">')
1802 # The second and third have the same "Advanced options" name, but the
1803 # second one has the "collapse" class.
1804 for x, classes in ((1, ""), (2, "collapse")):
1805 heading_id = f"fieldset-0-{x}-heading"
1806 with self.subTest(heading_id=heading_id):
1807 self.assertContains(
1808 response,
1809 f'<fieldset class="module aligned {classes}" '
1810 f'aria-labelledby="{heading_id}">',
1811 )
1812 self.assertContains(
1813 response,
1814 f'<h2 id="{heading_id}" class="fieldset-heading">'
1815 "Advanced options</h2>",
1816 )
1817 self.assertContains(response, f'id="{heading_id}"', count=1)
1818
1819 # Headings and subheadings for all the inlines.
1820 for inline_admin_formset in response.context["inline_admin_formsets"]:
1821 prefix = inline_admin_formset.formset.prefix
1822 heading_id = f"{prefix}-heading"
1823 formset_heading = (
1824 f'<h2 id="{heading_id}" class="inline-heading">Photos</h2>'
1825 )
1826 self.assertContains(response, formset_heading, html=True)
1827 self.assertContains(response, f'id="{heading_id}"', count=1)
1828
1829 # If this is a TabularInline, do not make further asserts since
1830 # fieldsets are not shown as such in this table layout.
1831 if "tabular" in inline_admin_formset.opts.template:
1832 continue
1833
1834 if "collapse" in inline_admin_formset.classes:
1835 formset_heading = f"<summary>{formset_heading}</summary>"
1836 self.assertContains(response, formset_heading, html=True, count=1)
1837
1838 # Headings for every formset (the amount depends on `extra`).
1839 for y, inline_admin_form in enumerate(inline_admin_formset):
1840 y_plus_one = y + 1
1841 form_heading = (
1842 f'<h3><b>Photo:</b> <span class="inline_label">#{y_plus_one}</span>'
1843 "</h3>"
1844 )
1845 self.assertContains(response, form_heading, html=True)
1846
1847 # Every fieldset defined for an inline's form.
1848 for z, fieldset in enumerate(inline_admin_form):
1849 if fieldset.name:
1850 heading_id = f"{prefix}-{y}-{z}-heading"
1851 self.assertContains(
1852 response,
1853 f'<fieldset class="module aligned {fieldset.classes}" '
1854 f'aria-labelledby="{heading_id}">',
1855 )
1856 fieldset_heading = (
1857 f'<h4 id="{heading_id}" class="fieldset-heading">'
1858 f"Details</h4>"
1859 )
1860 self.assertContains(response, fieldset_heading)
1861 if "collapse" in fieldset.classes:
1862 self.assertContains(
1863 response,
1864 f"<summary>{fieldset_heading}</summary>",
1865 html=True,
1866 )
1867 self.assertContains(response, f'id="{heading_id}"', count=1)
1868
1869 else:
1870 fieldset_html = (
1871 f'<fieldset class="module aligned {fieldset.classes}">'
1872 )
1873 self.assertContains(response, fieldset_html)
1874
1875
1876 @override_settings(ROOT_URLCONF="admin_inlines.urls")
1877 class SeleniumTests(AdminSeleniumTestCase):
1878 available_apps = ["admin_inlines"] + AdminSeleniumTestCase.available_apps
1879
1880 def setUp(self):
1881 User.objects.create_superuser(
1882 username="super", password="secret", email="[email protected]"
1883 )
1884
1885 @screenshot_cases(["desktop_size", "mobile_size", "dark", "high_contrast"])
1886 def test_add_stackeds(self):
1887 """
1888 The "Add another XXX" link correctly adds items to the stacked formset.
1889 """
1890 from selenium.webdriver.common.by import By
1891
1892 self.admin_login(username="super", password="secret")
1893 self.selenium.get(
1894 self.live_server_url + reverse("admin:admin_inlines_holder4_add")
1895 )
1896
1897 inline_id = "#inner4stacked_set-group"
1898 rows_selector = "%s .dynamic-inner4stacked_set" % inline_id
1899
1900 self.assertCountSeleniumElements(rows_selector, 3)
1901 add_button = self.selenium.find_element(
1902 By.LINK_TEXT, "Add another Inner4 stacked"
1903 )
1904 add_button.click()
1905 self.assertCountSeleniumElements(rows_selector, 4)
1906 self.take_screenshot("added")
1907
1908 def test_delete_stackeds(self):
1909 from selenium.webdriver.common.by import By
1910
1911 self.admin_login(username="super", password="secret")
1912 self.selenium.get(
1913 self.live_server_url + reverse("admin:admin_inlines_holder4_add")
1914 )
1915
1916 inline_id = "#inner4stacked_set-group"
1917 rows_selector = "%s .dynamic-inner4stacked_set" % inline_id
1918
1919 self.assertCountSeleniumElements(rows_selector, 3)
1920
1921 add_button = self.selenium.find_element(
1922 By.LINK_TEXT, "Add another Inner4 stacked"
1923 )
1924 add_button.click()
1925 add_button.click()
1926
1927 self.assertCountSeleniumElements(rows_selector, 5)
1928 for delete_link in self.selenium.find_elements(
1929 By.CSS_SELECTOR, "%s .inline-deletelink" % inline_id
1930 ):
1931 delete_link.click()
1932 with self.disable_implicit_wait():
1933 self.assertCountSeleniumElements(rows_selector, 0)
1934
1935 def test_delete_invalid_stacked_inlines(self):
1936 from selenium.common.exceptions import NoSuchElementException
1937 from selenium.webdriver.common.by import By
1938
1939 self.admin_login(username="super", password="secret")
1940 self.selenium.get(
1941 self.live_server_url + reverse("admin:admin_inlines_holder4_add")
1942 )
1943
1944 inline_id = "#inner4stacked_set-group"
1945 rows_selector = "%s .dynamic-inner4stacked_set" % inline_id
1946
1947 self.assertCountSeleniumElements(rows_selector, 3)
1948
1949 add_button = self.selenium.find_element(
1950 By.LINK_TEXT,
1951 "Add another Inner4 stacked",
1952 )
1953 add_button.click()
1954 add_button.click()
1955 self.assertCountSeleniumElements("#id_inner4stacked_set-4-dummy", 1)
1956
1957 # Enter some data and click 'Save'.
1958 self.selenium.find_element(By.NAME, "dummy").send_keys("1")
1959 self.selenium.find_element(By.NAME, "inner4stacked_set-0-dummy").send_keys(
1960 "100"
1961 )
1962 self.selenium.find_element(By.NAME, "inner4stacked_set-1-dummy").send_keys(
1963 "101"
1964 )
1965 self.selenium.find_element(By.NAME, "inner4stacked_set-2-dummy").send_keys(
1966 "222"
1967 )
1968 self.selenium.find_element(By.NAME, "inner4stacked_set-3-dummy").send_keys(
1969 "103"
1970 )
1971 self.selenium.find_element(By.NAME, "inner4stacked_set-4-dummy").send_keys(
1972 "222"
1973 )
1974 with self.wait_page_loaded():
1975 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
1976
1977 # Sanity check.
1978 self.assertCountSeleniumElements(rows_selector, 5)
1979 errorlist = self.selenium.find_element(
1980 By.CSS_SELECTOR,
1981 "%s .dynamic-inner4stacked_set .errorlist li" % inline_id,
1982 )
1983 self.assertEqual("Please correct the duplicate values below.", errorlist.text)
1984 delete_link = self.selenium.find_element(
1985 By.CSS_SELECTOR, "#inner4stacked_set-4 .inline-deletelink"
1986 )
1987 delete_link.click()
1988 self.assertCountSeleniumElements(rows_selector, 4)
1989 with self.disable_implicit_wait(), self.assertRaises(NoSuchElementException):
1990 self.selenium.find_element(
1991 By.CSS_SELECTOR,
1992 "%s .dynamic-inner4stacked_set .errorlist li" % inline_id,
1993 )
1994
1995 with self.wait_page_loaded():
1996 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
1997
1998 # The objects have been created in the database.
1999 self.assertEqual(Inner4Stacked.objects.count(), 4)
2000
2001 def test_delete_invalid_tabular_inlines(self):
2002 from selenium.common.exceptions import NoSuchElementException
2003 from selenium.webdriver.common.by import By
2004
2005 self.admin_login(username="super", password="secret")
2006 self.selenium.get(
2007 self.live_server_url + reverse("admin:admin_inlines_holder4_add")
2008 )
2009
2010 inline_id = "#inner4tabular_set-group"
2011 rows_selector = "%s .dynamic-inner4tabular_set" % inline_id
2012
2013 self.assertCountSeleniumElements(rows_selector, 3)
2014
2015 add_button = self.selenium.find_element(
2016 By.LINK_TEXT, "Add another Inner4 tabular"
2017 )
2018 add_button.click()
2019 add_button.click()
2020 self.assertCountSeleniumElements("#id_inner4tabular_set-4-dummy", 1)
2021
2022 # Enter some data and click 'Save'.
2023 self.selenium.find_element(By.NAME, "dummy").send_keys("1")
2024 self.selenium.find_element(By.NAME, "inner4tabular_set-0-dummy").send_keys(
2025 "100"
2026 )
2027 self.selenium.find_element(By.NAME, "inner4tabular_set-1-dummy").send_keys(
2028 "101"
2029 )
2030 self.selenium.find_element(By.NAME, "inner4tabular_set-2-dummy").send_keys(
2031 "222"
2032 )
2033 self.selenium.find_element(By.NAME, "inner4tabular_set-3-dummy").send_keys(
2034 "103"
2035 )
2036 self.selenium.find_element(By.NAME, "inner4tabular_set-4-dummy").send_keys(
2037 "222"
2038 )
2039 with self.wait_page_loaded():
2040 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
2041
2042 # Sanity Check.
2043 self.assertCountSeleniumElements(rows_selector, 5)
2044
2045 # Non-field errorlist is in its own <tr> just before
2046 # tr#inner4tabular_set-3:
2047 errorlist = self.selenium.find_element(
2048 By.CSS_SELECTOR,
2049 "%s #inner4tabular_set-3 + .row-form-errors .errorlist li" % inline_id,
2050 )
2051 self.assertEqual("Please correct the duplicate values below.", errorlist.text)
2052 delete_link = self.selenium.find_element(
2053 By.CSS_SELECTOR, "#inner4tabular_set-4 .inline-deletelink"
2054 )
2055 delete_link.click()
2056
2057 self.assertCountSeleniumElements(rows_selector, 4)
2058 with self.disable_implicit_wait(), self.assertRaises(NoSuchElementException):
2059 self.selenium.find_element(
2060 By.CSS_SELECTOR,
2061 "%s .dynamic-inner4tabular_set .errorlist li" % inline_id,
2062 )
2063
2064 with self.wait_page_loaded():
2065 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
2066
2067 # The objects have been created in the database.
2068 self.assertEqual(Inner4Tabular.objects.count(), 4)
2069
2070 def test_add_inlines(self):
2071 """
2072 The "Add another XXX" link correctly adds items to the inline form.
2073 """
2074 from selenium.webdriver.common.by import By
2075
2076 self.admin_login(username="super", password="secret")
2077 self.selenium.get(
2078 self.live_server_url + reverse("admin:admin_inlines_profilecollection_add")
2079 )
2080
2081 # There's only one inline to start with and it has the correct ID.
2082 self.assertCountSeleniumElements(".dynamic-profile_set", 1)
2083 self.assertEqual(
2084 self.selenium.find_elements(By.CSS_SELECTOR, ".dynamic-profile_set")[
2085 0
2086 ].get_attribute("id"),
2087 "profile_set-0",
2088 )
2089 self.assertCountSeleniumElements(
2090 ".dynamic-profile_set#profile_set-0 input[name=profile_set-0-first_name]", 1
2091 )
2092 self.assertCountSeleniumElements(
2093 ".dynamic-profile_set#profile_set-0 input[name=profile_set-0-last_name]", 1
2094 )
2095
2096 # Add an inline
2097 self.selenium.find_element(By.LINK_TEXT, "Add another Profile").click()
2098
2099 # The inline has been added, it has the right id, and it contains the
2100 # correct fields.
2101 self.assertCountSeleniumElements(".dynamic-profile_set", 2)
2102 self.assertEqual(
2103 self.selenium.find_elements(By.CSS_SELECTOR, ".dynamic-profile_set")[
2104 1
2105 ].get_attribute("id"),
2106 "profile_set-1",
2107 )
2108 self.assertCountSeleniumElements(
2109 ".dynamic-profile_set#profile_set-1 input[name=profile_set-1-first_name]", 1
2110 )
2111 self.assertCountSeleniumElements(
2112 ".dynamic-profile_set#profile_set-1 input[name=profile_set-1-last_name]", 1
2113 )
2114 # Let's add another one to be sure
2115 self.selenium.find_element(By.LINK_TEXT, "Add another Profile").click()
2116 self.assertCountSeleniumElements(".dynamic-profile_set", 3)
2117 self.assertEqual(
2118 self.selenium.find_elements(By.CSS_SELECTOR, ".dynamic-profile_set")[
2119 2
2120 ].get_attribute("id"),
2121 "profile_set-2",
2122 )
2123 self.assertCountSeleniumElements(
2124 ".dynamic-profile_set#profile_set-2 input[name=profile_set-2-first_name]", 1
2125 )
2126 self.assertCountSeleniumElements(
2127 ".dynamic-profile_set#profile_set-2 input[name=profile_set-2-last_name]", 1
2128 )
2129
2130 # Enter some data and click 'Save'
2131 self.selenium.find_element(By.NAME, "profile_set-0-first_name").send_keys(
2132 "0 first name 1"
2133 )
2134 self.selenium.find_element(By.NAME, "profile_set-0-last_name").send_keys(
2135 "0 last name 2"
2136 )
2137 self.selenium.find_element(By.NAME, "profile_set-1-first_name").send_keys(
2138 "1 first name 1"
2139 )
2140 self.selenium.find_element(By.NAME, "profile_set-1-last_name").send_keys(
2141 "1 last name 2"
2142 )
2143 self.selenium.find_element(By.NAME, "profile_set-2-first_name").send_keys(
2144 "2 first name 1"
2145 )
2146 self.selenium.find_element(By.NAME, "profile_set-2-last_name").send_keys(
2147 "2 last name 2"
2148 )
2149
2150 with self.wait_page_loaded():
2151 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
2152
2153 # The objects have been created in the database
2154 self.assertEqual(ProfileCollection.objects.count(), 1)
2155 self.assertEqual(Profile.objects.count(), 3)
2156
2157 def test_add_inline_link_absent_for_view_only_parent_model(self):
2158 from selenium.common.exceptions import NoSuchElementException
2159 from selenium.webdriver.common.by import By
2160
2161 user = User.objects.create_user("testing", password="password", is_staff=True)
2162 user.user_permissions.add(
2163 Permission.objects.get(
2164 codename="view_poll",
2165 content_type=ContentType.objects.get_for_model(Poll),
2166 )
2167 )
2168 user.user_permissions.add(
2169 *Permission.objects.filter(
2170 codename__endswith="question",
2171 content_type=ContentType.objects.get_for_model(Question),
2172 ).values_list("pk", flat=True)
2173 )
2174 self.admin_login(username="testing", password="password")
2175 poll = Poll.objects.create(name="Survey")
2176 change_url = reverse("admin:admin_inlines_poll_change", args=(poll.id,))
2177 self.selenium.get(self.live_server_url + change_url)
2178 with self.disable_implicit_wait():
2179 with self.assertRaises(NoSuchElementException):
2180 self.selenium.find_element(By.LINK_TEXT, "Add another Question")
2181
2182 def test_delete_inlines(self):
2183 from selenium.webdriver.common.by import By
2184
2185 self.admin_login(username="super", password="secret")
2186 self.selenium.get(
2187 self.live_server_url + reverse("admin:admin_inlines_profilecollection_add")
2188 )
2189
2190 # Add a few inlines
2191 self.selenium.find_element(By.LINK_TEXT, "Add another Profile").click()
2192 self.selenium.find_element(By.LINK_TEXT, "Add another Profile").click()
2193 self.selenium.find_element(By.LINK_TEXT, "Add another Profile").click()
2194 self.selenium.find_element(By.LINK_TEXT, "Add another Profile").click()
2195 self.assertCountSeleniumElements(
2196 "#profile_set-group table tr.dynamic-profile_set", 5
2197 )
2198 self.assertCountSeleniumElements(
2199 "form#profilecollection_form tr.dynamic-profile_set#profile_set-0", 1
2200 )
2201 self.assertCountSeleniumElements(
2202 "form#profilecollection_form tr.dynamic-profile_set#profile_set-1", 1
2203 )
2204 self.assertCountSeleniumElements(
2205 "form#profilecollection_form tr.dynamic-profile_set#profile_set-2", 1
2206 )
2207 self.assertCountSeleniumElements(
2208 "form#profilecollection_form tr.dynamic-profile_set#profile_set-3", 1
2209 )
2210 self.assertCountSeleniumElements(
2211 "form#profilecollection_form tr.dynamic-profile_set#profile_set-4", 1
2212 )
2213 # Click on a few delete buttons
2214 self.selenium.find_element(
2215 By.CSS_SELECTOR,
2216 "form#profilecollection_form tr.dynamic-profile_set#profile_set-1 "
2217 "td.delete a",
2218 ).click()
2219 self.selenium.find_element(
2220 By.CSS_SELECTOR,
2221 "form#profilecollection_form tr.dynamic-profile_set#profile_set-2 "
2222 "td.delete a",
2223 ).click()
2224 # The rows are gone and the IDs have been re-sequenced
2225 self.assertCountSeleniumElements(
2226 "#profile_set-group table tr.dynamic-profile_set", 3
2227 )
2228 self.assertCountSeleniumElements(
2229 "form#profilecollection_form tr.dynamic-profile_set#profile_set-0", 1
2230 )
2231 self.assertCountSeleniumElements(
2232 "form#profilecollection_form tr.dynamic-profile_set#profile_set-1", 1
2233 )
2234 self.assertCountSeleniumElements(
2235 "form#profilecollection_form tr.dynamic-profile_set#profile_set-2", 1
2236 )
2237
2238 @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
2239 def test_collapsed_inlines(self):
2240 from selenium.webdriver.common.by import By
2241
2242 # Collapsed inlines use details and summary elements.
2243 self.admin_login(username="super", password="secret")
2244 self.selenium.get(
2245 self.live_server_url + reverse("admin:admin_inlines_author_add")
2246 )
2247 # One field is in a stacked inline, other in a tabular one.
2248 test_fields = [
2249 "#id_nonautopkbook_set-0-title",
2250 "#id_nonautopkbook_set-2-0-title",
2251 ]
2252 summaries = self.selenium.find_elements(By.TAG_NAME, "summary")
2253 self.assertEqual(len(summaries), 3)
2254 self.take_screenshot("loaded")
2255 for show_index, field_name in enumerate(test_fields, 0):
2256 self.wait_until_invisible(field_name)
2257 summaries[show_index].click()
2258 self.wait_until_visible(field_name)
2259 self.take_screenshot("expanded")
2260 for hide_index, field_name in enumerate(test_fields, 0):
2261 self.wait_until_visible(field_name)
2262 summaries[hide_index].click()
2263 self.wait_until_invisible(field_name)
2264 self.take_screenshot("collapsed")
2265
2266 @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
2267 def test_added_stacked_inline_with_collapsed_fields(self):
2268 from selenium.webdriver.common.by import By
2269
2270 self.admin_login(username="super", password="secret")
2271 self.selenium.get(
2272 self.live_server_url + reverse("admin:admin_inlines_teacher_add")
2273 )
2274 add_text = gettext("Add another %(verbose_name)s") % {"verbose_name": "Child"}
2275 self.selenium.find_element(By.LINK_TEXT, add_text).click()
2276 test_fields = ["#id_child_set-0-name", "#id_child_set-1-name"]
2277 summaries = self.selenium.find_elements(By.TAG_NAME, "summary")
2278 self.assertEqual(len(summaries), 3)
2279 self.take_screenshot("loaded")
2280 for show_index, field_name in enumerate(test_fields, 0):
2281 self.wait_until_invisible(field_name)
2282 summaries[show_index].click()
2283 self.wait_until_visible(field_name)
2284 self.take_screenshot("expanded")
2285 for hide_index, field_name in enumerate(test_fields, 0):
2286 self.wait_until_visible(field_name)
2287 summaries[hide_index].click()
2288 self.wait_until_invisible(field_name)
2289 self.take_screenshot("collapsed")
2290
2291 def assertBorder(self, element, border):
2292 width, style, color = border.split(" ")
2293 border_properties = [
2294 "border-bottom-%s",
2295 "border-left-%s",
2296 "border-right-%s",
2297 "border-top-%s",
2298 ]
2299 for prop in border_properties:
2300 self.assertEqual(element.value_of_css_property(prop % "width"), width)
2301 for prop in border_properties:
2302 self.assertEqual(element.value_of_css_property(prop % "style"), style)
2303 # Convert hex color to rgb.
2304 self.assertRegex(color, "#[0-9a-f]{6}")
2305 r, g, b = int(color[1:3], 16), int(color[3:5], 16), int(color[5:], 16)
2306 # The value may be expressed as either rgb() or rgba() depending on the
2307 # browser.
2308 colors = [
2309 "rgb(%d, %d, %d)" % (r, g, b),
2310 "rgba(%d, %d, %d, 1)" % (r, g, b),
2311 ]
2312 for prop in border_properties:
2313 self.assertIn(element.value_of_css_property(prop % "color"), colors)
2314
2315 def test_inline_formset_error_input_border(self):
2316 from selenium.webdriver.common.by import By
2317
2318 self.admin_login(username="super", password="secret")
2319 self.selenium.get(
2320 self.live_server_url + reverse("admin:admin_inlines_holder5_add")
2321 )
2322 self.wait_until_visible("#id_dummy")
2323 self.selenium.find_element(By.ID, "id_dummy").send_keys(1)
2324 fields = ["id_inner5stacked_set-0-dummy", "id_inner5tabular_set-0-dummy"]
2325 summaries = self.selenium.find_elements(By.TAG_NAME, "summary")
2326 for show_index, field_name in enumerate(fields):
2327 summaries[show_index].click()
2328 self.wait_until_visible("#" + field_name)
2329 self.selenium.find_element(By.ID, field_name).send_keys(1)
2330
2331 # Before save all inputs have default border
2332 for inline in ("stacked", "tabular"):
2333 for field_name in ("name", "select", "text"):
2334 element_id = "id_inner5%s_set-0-%s" % (inline, field_name)
2335 self.assertBorder(
2336 self.selenium.find_element(By.ID, element_id),
2337 "1px solid #cccccc",
2338 )
2339 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
2340 # Test the red border around inputs by css selectors
2341 stacked_selectors = [".errors input", ".errors select", ".errors textarea"]
2342 for selector in stacked_selectors:
2343 self.assertBorder(
2344 self.selenium.find_element(By.CSS_SELECTOR, selector),
2345 "1px solid #ba2121",
2346 )
2347 tabular_selectors = [
2348 "td ul.errorlist + input",
2349 "td ul.errorlist + select",
2350 "td ul.errorlist + textarea",
2351 ]
2352 for selector in tabular_selectors:
2353 self.assertBorder(
2354 self.selenium.find_element(By.CSS_SELECTOR, selector),
2355 "1px solid #ba2121",
2356 )
2357
2358 def test_inline_formset_error(self):
2359 from selenium.webdriver.common.by import By
2360
2361 self.admin_login(username="super", password="secret")
2362 self.selenium.get(
2363 self.live_server_url + reverse("admin:admin_inlines_holder5_add")
2364 )
2365 stacked_inline_details_selector = (
2366 "div#inner5stacked_set-group fieldset.module.collapse details"
2367 )
2368 tabular_inline_details_selector = (
2369 "div#inner5tabular_set-group fieldset.module.collapse details"
2370 )
2371 # Inlines without errors, both inlines collapsed
2372 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
2373 self.assertCountSeleniumElements(
2374 stacked_inline_details_selector + ":not([open])", 1
2375 )
2376 self.assertCountSeleniumElements(
2377 tabular_inline_details_selector + ":not([open])", 1
2378 )
2379 summaries = self.selenium.find_elements(By.TAG_NAME, "summary")
2380 self.assertEqual(len(summaries), 2)
2381
2382 # Inlines with errors, both inlines expanded
2383 test_fields = ["#id_inner5stacked_set-0-dummy", "#id_inner5tabular_set-0-dummy"]
2384 for show_index, field_name in enumerate(test_fields):
2385 summaries[show_index].click()
2386 self.wait_until_visible(field_name)
2387 self.selenium.find_element(By.ID, field_name[1:]).send_keys(1)
2388 for hide_index, field_name in enumerate(test_fields):
2389 summary = summaries[hide_index]
2390 self.selenium.execute_script(
2391 "window.scrollTo(0, %s);" % summary.location["y"]
2392 )
2393 summary.click()
2394 self.wait_until_invisible(field_name)
2395 with self.wait_page_loaded():
2396 self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
2397 self.assertCountSeleniumElements(stacked_inline_details_selector, 0)
2398 self.assertCountSeleniumElements(tabular_inline_details_selector, 0)
2399
2400 def test_inlines_verbose_name(self):
2401 """
2402 The item added by the "Add another XXX" link must use the correct
2403 verbose_name in the inline form.
2404 """
2405 from selenium.webdriver.common.by import By
2406
2407 self.admin_login(username="super", password="secret")
2408 # Hide sidebar.
2409 self.selenium.get(
2410 self.live_server_url + reverse("admin:admin_inlines_course_add")
2411 )
2412 toggle_button = self.selenium.find_element(
2413 By.CSS_SELECTOR, "#toggle-nav-sidebar"
2414 )
2415 toggle_button.click()
2416 # Each combination of horizontal/vertical filter with stacked/tabular
2417 # inlines.
2418 tests = [
2419 "admin:admin_inlines_course_add",
2420 "admin:admin_inlines_courseproxy_add",
2421 "admin:admin_inlines_courseproxy1_add",
2422 "admin:admin_inlines_courseproxy2_add",
2423 ]
2424 css_available_selector = (
2425 ".dynamic-class_set#class_set-%s .selector-available-title"
2426 )
2427 css_chosen_selector = ".dynamic-class_set#class_set-%s .selector-chosen-title"
2428
2429 for url_name in tests:
2430 with self.subTest(url=url_name):
2431 self.selenium.get(self.live_server_url + reverse(url_name))
2432 # First inline shows the verbose_name.
2433 available = self.selenium.find_element(
2434 By.CSS_SELECTOR, css_available_selector % 0
2435 )
2436 chosen = self.selenium.find_element(
2437 By.CSS_SELECTOR, css_chosen_selector % 0
2438 )
2439 self.assertIn("Available attendant", available.text)
2440 self.assertIn("Chosen attendant", chosen.text)
2441 # Added inline should also have the correct verbose_name.
2442 self.selenium.find_element(By.LINK_TEXT, "Add another Class").click()
2443 available = self.selenium.find_element(
2444 By.CSS_SELECTOR, css_available_selector % 1
2445 )
2446 chosen = self.selenium.find_element(
2447 By.CSS_SELECTOR, css_chosen_selector % 1
2448 )
2449 self.assertIn("Available attendant", available.text)
2450 self.assertIn("Chosen attendant", chosen.text)
2451 # Third inline should also have the correct verbose_name.
2452 self.selenium.find_element(By.LINK_TEXT, "Add another Class").click()
2453 available = self.selenium.find_element(
2454 By.CSS_SELECTOR, css_available_selector % 2
2455 )
2456 chosen = self.selenium.find_element(
2457 By.CSS_SELECTOR, css_chosen_selector % 2
2458 )
2459 self.assertIn("Available attendant", available.text)
2460 self.assertIn("Chosen attendant", chosen.text)
2461
2462 def test_tabular_inline_layout(self):
2463 from selenium.webdriver.common.by import By
2464
2465 self.admin_login(username="super", password="secret")
2466 self.selenium.get(
2467 self.live_server_url + reverse("admin:admin_inlines_photographer_add")
2468 )
2469 tabular_inline = self.selenium.find_element(
2470 By.CSS_SELECTOR, "[data-inline-type='tabular']"
2471 )
2472 headers = tabular_inline.find_elements(By.TAG_NAME, "th")
2473 self.assertEqual(
2474 [h.get_attribute("innerText") for h in headers],
2475 [
2476 "",
2477 "IMAGE",
2478 "TITLE",
2479 "DESCRIPTION",
2480 "CREATION DATE",
2481 "UPDATE DATE",
2482 "UPDATED BY",
2483 "DELETE?",
2484 ],
2485 )
2486 # There are no fieldset section names rendered.
2487 self.assertNotIn("Details", tabular_inline.text)
2488 # There are no fieldset section descriptions rendered.
2489 self.assertNotIn("First group", tabular_inline.text)
2490 self.assertNotIn("Second group", tabular_inline.text)
2491 self.assertNotIn("Third group", tabular_inline.text)
2492 # There are no fieldset classes applied.
2493 self.assertEqual(
2494 tabular_inline.find_elements(By.CSS_SELECTOR, ".collapse"),
2495 [],
2496 )
2497
2498 @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
2499 def test_tabular_inline_delete_layout(self):
2500 from selenium.webdriver.common.by import By
2501
2502 user = User.objects.create_user("testing", password="password", is_staff=True)
2503 et_permission = Permission.objects.filter(
2504 content_type=ContentType.objects.get_for_model(ExtraTerrestrial),
2505 )
2506 s_permission = Permission.objects.filter(
2507 codename__in=["view_sighting", "add_sighting"],
2508 content_type=ContentType.objects.get_for_model(Sighting),
2509 )
2510 user.user_permissions.add(*et_permission, *s_permission)
2511 self.admin_login(username="testing", password="password")
2512 cf = ExtraTerrestrial.objects.create(name="test")
2513 url = reverse("admin:admin_inlines_extraterrestrial_change", args=(cf.pk,))
2514 self.selenium.get(self.live_server_url + url)
2515 headers = self.selenium.find_elements(
2516 By.CSS_SELECTOR, "fieldset.module thead tr th"
2517 )
2518 self.assertHTMLEqual(headers[-1].get_attribute("outerHTML"), "<th></th>")
2519 delete = self.selenium.find_element(
2520 By.CSS_SELECTOR,
2521 "fieldset.module tbody tr.dynamic-sighting_set:not(.original) td.delete",
2522 )
2523 self.assertIn(
2524 '<a role="button" class="inline-deletelink" href="#">',
2525 delete.get_attribute("innerHTML"),
2526 )
2527 self.take_screenshot("loaded")
2528
2529 @screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
2530 def test_tabular_inline_with_filter_horizontal(self):
2531 from selenium.webdriver.common.by import By
2532
2533 self.admin_login(username="super", password="secret")
2534 self.selenium.get(
2535 self.live_server_url + reverse("admin:admin_inlines_courseproxy2_add")
2536 )
2537 m2m_widget = self.selenium.find_element(By.CSS_SELECTOR, "div.selector")
2538 self.assertTrue(m2m_widget.is_displayed())
2539 self.take_screenshot("tabular")