Ticket #13223: 13223-2.diff

File 13223-2.diff, 6.8 KB (added by Ramiro Morales, 12 years ago)

Patch updated to current trunk status, still needs work

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    a b  
    10031003            'inline_admin_formsets': inline_admin_formsets,
    10041004            'errors': helpers.AdminErrorList(form, formsets),
    10051005            'app_label': opts.app_label,
     1006            'saveasnew': "_saveasnew" in request.POST,
    10061007        }
    10071008        context.update(extra_context or {})
    10081009        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  
    6666
    6767{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
    6868
     69{% if saveasnew %}<input type="hidden" value="_saveasnew" name="_saveasnew">{% endif %}
    6970{% if adminform and add %}
    7071   <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
    7172{% 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  
    113113    model = Profile
    114114    extra = 1
    115115
     116
     117class BlockInline(admin.TabularInline):
     118    model = Block
     119
     120
     121class PostAdmin(admin.ModelAdmin):
     122    save_as = True
     123    inlines = [BlockInline]
     124
     125
    116126site.register(TitleCollection, inlines=[TitleInline])
    117127# Test bug #12561 and #12778
    118128# only ModelAdmin media
     
    128138site.register(Holder4, Holder4Admin)
    129139site.register(Author, AuthorAdmin)
    130140site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline])
    131 site.register(ProfileCollection, inlines=[ProfileInline])
    132  No newline at end of file
     141site.register(ProfileCollection, inlines=[ProfileInline])
     142
     143site.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  
    145145class Profile(models.Model):
    146146    collection = models.ForeignKey(ProfileCollection, blank=True, null=True)
    147147    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
     152class Post(models.Model):
     153    title = models.CharField(max_length=200)
     154
     155class 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  
    1010from .admin import InnerInline
    1111from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person,
    1212    OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile,
    13     ProfileCollection)
     13    ProfileCollection, Post, Block)
    1414
    1515
    1616class TestInline(TestCase):
     
    145145                '<input id="id_-2-0-name" type="text" class="vTextField" '
    146146                'name="-2-0-name" maxlength="100" />')
    147147
     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
    148228class TestInlineMedia(TestCase):
    149229    urls = "regressiontests.admin_inlines.urls"
    150230    fixtures = ['admin-views-users.xml']
Back to Top