Ticket #13223: 13223-1.diff

File 13223-1.diff, 6.1 KB (added by Ramiro Morales, 13 years ago)

Patch using the fix strategy proposed by contributor prestontimmons with enhanced tests

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    a b  
    970970            'inline_admin_formsets': inline_admin_formsets,
    971971            'errors': helpers.AdminErrorList(form, formsets),
    972972            'app_label': opts.app_label,
     973            'saveasnew': "_saveasnew" in request.POST,
    973974        }
    974975        context.update(extra_context or {})
    975976        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  
    6262{% block after_related_objects %}{% endblock %}
    6363
    6464{% submit_row %}
    65 
     65{% if saveasnew %}<input type="hidden" value="_saveasnew" name="_saveasnew">{% endif %}
    6666{% if adminform and add %}
    6767   <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
    6868{% endif %}
  • tests/regressiontests/admin_inlines/models.py

    diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
    a b  
    217217
    218218admin.site.register(Poll, PollAdmin)
    219219admin.site.register(Novel, NovelAdmin)
     220
     221# Models for testing bug #13223 fix
     222class Post(models.Model):
     223    title = models.CharField(max_length=200)
     224
     225class Block(models.Model):
     226    post = models.ForeignKey(Post)
     227    slug = models.CharField(max_length=200, unique=True)
     228
     229    def __unicode__(self):
     230        return self.slug
     231
     232class BlockInline(admin.TabularInline):
     233    model = Block
     234
     235class PostAdmin(admin.ModelAdmin):
     236    save_as = True
     237    inlines = [BlockInline]
     238
     239admin.site.register(Post,PostAdmin)
  • tests/regressiontests/admin_inlines/tests.py

    diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py
    a b  
    44
    55# local test models
    66from models import (Holder, Inner, InnerInline, Holder2, Inner2, Holder3,
    7     Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
     7    Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child, Post,
     8    Block)
    89
    910
    1011class TestInline(TestCase):
     
    113114        self.assertContains(response, '<p class="help">Awesome stacked help text is awesome.</p>', 4)
    114115        self.assertContains(response, '<img src="/static/admin/img/icon-unknown.gif" class="help help-tooltip" width="10" height="10" alt="(Awesome tabular help text is awesome.)" title="Awesome tabular help text is awesome." />', 1)
    115116
     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
    116197class TestInlineMedia(TestCase):
    117198    fixtures = ['admin-views-users.xml']
    118199
Back to Top