@@ -595,10 +595,10 @@ def setUpTestData(cls):
595595 cls .user .user_permissions .add (permission )
596596
597597 author = Author .objects .create (pk = 1 , name = 'The Author' )
598- book = author .books .create (name = 'The inline Book' )
598+ cls . book = author .books .create (name = 'The inline Book' )
599599 cls .author_change_url = reverse ('admin:admin_inlines_author_change' , args = (author .id ,))
600600 # Get the ID of the automatically created intermediate model for the Author-Book m2m
601- author_book_auto_m2m_intermediate = Author .books .through .objects .get (author = author , book = book )
601+ author_book_auto_m2m_intermediate = Author .books .through .objects .get (author = author , book = cls . book )
602602 cls .author_book_auto_m2m_intermediate_id = author_book_auto_m2m_intermediate .pk
603603
604604 cls .holder = Holder2 .objects .create (dummy = 13 )
@@ -636,6 +636,25 @@ def test_inline_change_fk_noperm(self):
636636 self .assertNotContains (response , 'Add another Inner2' )
637637 self .assertNotContains (response , 'id="id_inner2_set-TOTAL_FORMS"' )
638638
639+ def test_inline_add_m2m_view_only_perm (self ):
640+ permission = Permission .objects .get (codename = 'view_book' , content_type = self .book_ct )
641+ self .user .user_permissions .add (permission )
642+ response = self .client .get (reverse ('admin:admin_inlines_author_add' ))
643+ # View-only inlines. (It could be nicer to hide the empty, non-editable
644+ # inlines on the add page.)
645+ self .assertIs (response .context ['inline_admin_formset' ].has_view_permission , True )
646+ self .assertIs (response .context ['inline_admin_formset' ].has_add_permission , False )
647+ self .assertIs (response .context ['inline_admin_formset' ].has_change_permission , False )
648+ self .assertIs (response .context ['inline_admin_formset' ].has_delete_permission , False )
649+ self .assertContains (response , '<h2>Author-book relationships</h2>' )
650+ self .assertContains (
651+ response ,
652+ '<input type="hidden" name="Author_books-TOTAL_FORMS" value="0" '
653+ 'id="id_Author_books-TOTAL_FORMS">' ,
654+ html = True ,
655+ )
656+ self .assertNotContains (response , 'Add another Author-Book Relationship' )
657+
639658 def test_inline_add_m2m_add_perm (self ):
640659 permission = Permission .objects .get (codename = 'add_book' , content_type = self .book_ct )
641660 self .user .user_permissions .add (permission )
@@ -665,11 +684,39 @@ def test_inline_change_m2m_add_perm(self):
665684 self .assertNotContains (response , 'id="id_Author_books-TOTAL_FORMS"' )
666685 self .assertNotContains (response , 'id="id_Author_books-0-DELETE"' )
667686
687+ def test_inline_change_m2m_view_only_perm (self ):
688+ permission = Permission .objects .get (codename = 'view_book' , content_type = self .book_ct )
689+ self .user .user_permissions .add (permission )
690+ response = self .client .get (self .author_change_url )
691+ # View-only inlines.
692+ self .assertIs (response .context ['inline_admin_formset' ].has_view_permission , True )
693+ self .assertIs (response .context ['inline_admin_formset' ].has_add_permission , False )
694+ self .assertIs (response .context ['inline_admin_formset' ].has_change_permission , False )
695+ self .assertIs (response .context ['inline_admin_formset' ].has_delete_permission , False )
696+ self .assertContains (response , '<h2>Author-book relationships</h2>' )
697+ self .assertContains (
698+ response ,
699+ '<input type="hidden" name="Author_books-TOTAL_FORMS" value="1" '
700+ 'id="id_Author_books-TOTAL_FORMS">' ,
701+ html = True ,
702+ )
703+ # The field in the inline is read-only.
704+ self .assertContains (response , '<p>%s</p>' % self .book )
705+ self .assertNotContains (
706+ response ,
707+ '<input type="checkbox" name="Author_books-0-DELETE" id="id_Author_books-0-DELETE">' ,
708+ html = True ,
709+ )
710+
668711 def test_inline_change_m2m_change_perm (self ):
669712 permission = Permission .objects .get (codename = 'change_book' , content_type = self .book_ct )
670713 self .user .user_permissions .add (permission )
671714 response = self .client .get (self .author_change_url )
672715 # We have change perm on books, so we can add/change/delete inlines
716+ self .assertIs (response .context ['inline_admin_formset' ].has_view_permission , True )
717+ self .assertIs (response .context ['inline_admin_formset' ].has_add_permission , True )
718+ self .assertIs (response .context ['inline_admin_formset' ].has_change_permission , True )
719+ self .assertIs (response .context ['inline_admin_formset' ].has_delete_permission , True )
673720 self .assertContains (response , '<h2>Author-book relationships</h2>' )
674721 self .assertContains (response , 'Add another Author-book relationship' )
675722 self .assertContains (response , '<input type="hidden" id="id_Author_books-TOTAL_FORMS" '
0 commit comments