Ticket #13223: 13223-2.diff
File 13223-2.diff, 6.8 KB (added by , 13 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
a b 1003 1003 'inline_admin_formsets': inline_admin_formsets, 1004 1004 'errors': helpers.AdminErrorList(form, formsets), 1005 1005 'app_label': opts.app_label, 1006 'saveasnew': "_saveasnew" in request.POST, 1006 1007 } 1007 1008 context.update(extra_context or {}) 1008 1009 return self.render_change_form(request, context, form_url=form_url, add=True) -
django/contrib/admin/templates/admin/change_form.html
diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html
a b 66 66 67 67 {% block submit_buttons_bottom %}{% submit_row %}{% endblock %} 68 68 69 {% if saveasnew %}<input type="hidden" value="_saveasnew" name="_saveasnew">{% endif %} 69 70 {% if adminform and add %} 70 71 <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script> 71 72 {% endif %} -
tests/regressiontests/admin_inlines/admin.py
diff --git a/tests/regressiontests/admin_inlines/admin.py b/tests/regressiontests/admin_inlines/admin.py
a b 113 113 model = Profile 114 114 extra = 1 115 115 116 117 class BlockInline(admin.TabularInline): 118 model = Block 119 120 121 class PostAdmin(admin.ModelAdmin): 122 save_as = True 123 inlines = [BlockInline] 124 125 116 126 site.register(TitleCollection, inlines=[TitleInline]) 117 127 # Test bug #12561 and #12778 118 128 # only ModelAdmin media … … 128 138 site.register(Holder4, Holder4Admin) 129 139 site.register(Author, AuthorAdmin) 130 140 site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline]) 131 site.register(ProfileCollection, inlines=[ProfileInline]) 132 No newline at end of file 141 site.register(ProfileCollection, inlines=[ProfileInline]) 142 143 site.register(Post, PostAdmin) -
tests/regressiontests/admin_inlines/models.py
diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
a b 145 145 class Profile(models.Model): 146 146 collection = models.ForeignKey(ProfileCollection, blank=True, null=True) 147 147 first_name = models.CharField(max_length=100) 148 last_name = models.CharField(max_length=100) 149 No newline at end of file 148 last_name = models.CharField(max_length=100) 149 150 151 # Models for testing bug #13223 fix 152 class Post(models.Model): 153 title = models.CharField(max_length=200) 154 155 class Block(models.Model): 156 post = models.ForeignKey(Post) 157 slug = models.CharField(max_length=200, unique=True) 158 159 def __unicode__(self): 160 return self.slug -
tests/regressiontests/admin_inlines/tests.py
diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py
a b 10 10 from .admin import InnerInline 11 11 from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person, 12 12 OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile, 13 ProfileCollection )13 ProfileCollection, Post, Block) 14 14 15 15 16 16 class TestInline(TestCase): … … 145 145 '<input id="id_-2-0-name" type="text" class="vTextField" ' 146 146 'name="-2-0-name" maxlength="100" />') 147 147 148 def test_save_as_new_with_validation_error_in_inlines(self): 149 """ 150 #13223 - ValueError shouldn't happen after using 'Save as new' and 151 correcting a validation error in an inline. 152 """ 153 p = Post.objects.create(title='Post title') 154 b1 = p.block_set.create(slug='test1') 155 b2 = p.block_set.create(slug='test2') 156 157 initial_posts_count = Post.objects.count() 158 initial_blocks_count = Block.objects.count() 159 160 # Emulate edition of our Post object in the admin and then submittal 161 # with the 'Save as new' button but triggering a validation error in 162 # the Block inlines 163 data = { 164 'title': 'Second Post title', 165 166 'block_set-TOTAL_FORMS': 5, 167 'block_set-INITIAL_FORMS': 2, 168 'block_set-MAX_NUM_FORMS': 0, 169 'block_set-0-id': b1.id, 170 'block_set-0-post': p.id, 171 'block_set-0-slug': b1.slug, 172 'block_set-1-id': b2.id, 173 'block_set-1-post': p.id, 174 'block_set-1-slug': b2.slug, 175 'block_set-2-id': '', 176 'block_set-2-post': p.id, 177 'block_set-2-slug': '', 178 'block_set-3-id': '', 179 'block_set-3-post': p.id, 180 'block_set-3-slug': '', 181 'block_set-4-id': '', 182 'block_set-4-post': p.id, 183 'block_set-4-slug': '', 184 'block_set-__prefix__-id': '', 185 'block_set-__prefix__-post': p.id, 186 'block_set-__prefix__-slug': '', 187 188 '_saveasnew': u'Save as new', 189 } 190 response = self.client.post('/admin/admin_inlines/post/%s/' % p.id, data) 191 self.assertEqual(response.status_code, 200) 192 # Verify that an error has been reported 193 self.assertEqual(response.context['inline_admin_formset'].formset.errors[0]['slug'], 194 [u'Block with this Slug already exists.']) 195 self.assertContains(response, '<input type="hidden" value="_saveasnew" name="_saveasnew">') 196 197 # Correct the inline data so it validates. Also, add a new Block inline 198 # (for a total of three). This time we are presented with the 'Save' 199 # button, use it to submit the form. 200 data = { 201 'title': 'Second Post title', 202 203 'block_set-TOTAL_FORMS': 5, 204 'block_set-INITIAL_FORMS': 2, 205 'block_set-MAX_NUM_FORMS': 0, 206 'block_set-0-slug': 'test4', 207 'block_set-1-slug': 'test5', 208 'block_set-2-slug': 'test6', 209 'block_set-3-slug': '', 210 'block_set-4-slug': '', 211 'block_set-__prefix__-id': '', 212 'block_set-__prefix__-post': p.id, 213 'block_set-__prefix__-slug': '', 214 215 '_saveasnew': u'_saveasnew', 216 '_save': u'Save', 217 } 218 # Re-submit the form, take in account we will be redirected: 219 response = self.client.post( 220 '/admin/admin_inlines/post/%s/' % p.id, 221 data, follow=True) 222 self.assertEqual(response.status_code, 200) 223 # Verify the new objects were correctly created 224 self.assertEqual(Post.objects.count(), initial_posts_count + 1) 225 self.assertEqual(Block.objects.count(), initial_blocks_count + 3) 226 227 148 228 class TestInlineMedia(TestCase): 149 229 urls = "regressiontests.admin_inlines.urls" 150 230 fixtures = ['admin-views-users.xml']