| | 117 | def test_save_as_new_with_validation_error_in_inlines(self): |
| | 118 | """ |
| | 119 | #13223 - ValueError shouldn't happen after using 'Save as new' and |
| | 120 | correcting a validation error in an inline. |
| | 121 | """ |
| | 122 | p = Post.objects.create(title='Post title') |
| | 123 | b1 = p.block_set.create(slug='test1') |
| | 124 | b2 = p.block_set.create(slug='test2') |
| | 125 | |
| | 126 | initial_posts_count = Post.objects.count() |
| | 127 | initial_blocks_count = Block.objects.count() |
| | 128 | |
| | 129 | # Emulate edition of our Post object in the admin and then submittal |
| | 130 | # with the 'Save as new' button but triggering a validation error in |
| | 131 | # the Block inlines |
| | 132 | data = { |
| | 133 | 'title': 'Second Post title', |
| | 134 | |
| | 135 | 'block_set-TOTAL_FORMS': 5, |
| | 136 | 'block_set-INITIAL_FORMS': 2, |
| | 137 | 'block_set-MAX_NUM_FORMS': 0, |
| | 138 | 'block_set-0-id': b1.id, |
| | 139 | 'block_set-0-post': p.id, |
| | 140 | 'block_set-0-slug': b1.slug, |
| | 141 | 'block_set-1-id': b2.id, |
| | 142 | 'block_set-1-post': p.id, |
| | 143 | 'block_set-1-slug': b2.slug, |
| | 144 | 'block_set-2-id': '', |
| | 145 | 'block_set-2-post': p.id, |
| | 146 | 'block_set-2-slug': '', |
| | 147 | 'block_set-3-id': '', |
| | 148 | 'block_set-3-post': p.id, |
| | 149 | 'block_set-3-slug': '', |
| | 150 | 'block_set-4-id': '', |
| | 151 | 'block_set-4-post': p.id, |
| | 152 | 'block_set-4-slug': '', |
| | 153 | 'block_set-__prefix__-id': '', |
| | 154 | 'block_set-__prefix__-post': p.id, |
| | 155 | 'block_set-__prefix__-slug': '', |
| | 156 | |
| | 157 | '_saveasnew': u'Save as new', |
| | 158 | } |
| | 159 | response = self.client.post('/test_admin/admin/admin_inlines/post/%s/' % p.id, data) |
| | 160 | self.assertEqual(response.status_code, 200) |
| | 161 | # Verify that an error has been reported |
| | 162 | self.assertEqual(response.context['inline_admin_formset'].formset.errors[0]['slug'], |
| | 163 | [u'Block with this Slug already exists.']) |
| | 164 | self.assertContains(response, '<input type="hidden" value="_saveasnew" name="_saveasnew">') |
| | 165 | |
| | 166 | # Correct the inline data so it validates. Also, add a new Block inline |
| | 167 | # (for a total of three). This time we are presented with the 'Save' |
| | 168 | # button, use it to submit the form. |
| | 169 | data = { |
| | 170 | 'title': 'Second Post title', |
| | 171 | |
| | 172 | 'block_set-TOTAL_FORMS': 5, |
| | 173 | 'block_set-INITIAL_FORMS': 2, |
| | 174 | 'block_set-MAX_NUM_FORMS': 0, |
| | 175 | 'block_set-0-slug': 'test4', |
| | 176 | 'block_set-1-slug': 'test5', |
| | 177 | 'block_set-2-slug': 'test6', |
| | 178 | 'block_set-3-slug': '', |
| | 179 | 'block_set-4-slug': '', |
| | 180 | 'block_set-__prefix__-id': '', |
| | 181 | 'block_set-__prefix__-post': p.id, |
| | 182 | 'block_set-__prefix__-slug': '', |
| | 183 | |
| | 184 | '_saveasnew': u'_saveasnew', |
| | 185 | '_save': u'Save', |
| | 186 | } |
| | 187 | # Re-submit the form, take in account we will be redirected: |
| | 188 | response = self.client.post( |
| | 189 | '/test_admin/admin/admin_inlines/post/%s/' % p.id, |
| | 190 | data, follow=True) |
| | 191 | self.assertEqual(response.status_code, 200) |
| | 192 | # Verify the new objects were correctly created |
| | 193 | self.assertEqual(Post.objects.count(), initial_posts_count + 1) |
| | 194 | self.assertEqual(Block.objects.count(), initial_blocks_count + 3) |
| | 195 | |
| | 196 | |