Ticket #10448: patch10448against10203withtest.diff

File patch10448against10203withtest.diff, 3.4 KB (added by bastih, 6 years ago)
  • django/contrib/admin/options.py

     
    782782            'app_label': opts.app_label,
    783783        }
    784784        context.update(extra_context or {})
    785         return self.render_change_form(request, context, add=True)
     785        return self.render_change_form(request, context, form_url=form_url, add=True)
    786786    add_view = transaction.commit_on_success(add_view)
    787787
    788788    def change_view(self, request, object_id, extra_context=None):
     
    805805            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})
    806806
    807807        if request.method == 'POST' and request.POST.has_key("_saveasnew"):
    808             return self.add_view(request, form_url='../../add/')
     808            return self.add_view(request, form_url='../add/')
    809809
    810810        ModelForm = self.get_form(request, obj)
    811811        formsets = []
  • tests/regressiontests/admin_views/tests.py

     
    182182        response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit, {'color__id__exact': 'StringNotInteger!'})
    183183        self.assertRedirects(response, '/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)
    184184
     185class SaveAsTest(TestCase):
     186    fixtures = ['admin-views-users.xml','admin-views-person.xml']
     187    urlbit = 'admin'
     188   
     189    def setUp(self):
     190        self.client.login(username='super', password='secret')
     191
     192    def tearDown(self):
     193        self.client.logout()
     194   
     195    def testSaveAsDuplication(self):
     196        """Ensure save as actually creates a new person"""
     197        post_data = {'_saveasnew':'', 'name':'John M', 'gender':1}
     198        response = self.client.post('/test_admin/%s/admin_views/person/1/' % self.urlbit, post_data)
     199        self.assertEqual(len(Person.objects.filter(name='John M')), 1)
     200        self.assertEqual(len(Person.objects.filter(id=1)),1)
     201   
     202    def testSaveAsDisplay(self):
     203        """Ensure that 'save as' is displayed when activated and after submitting invalid data aside
     204        save_as_new will not show us a form to overwrite the initial model"""
     205        response = self.client.get('/test_admin/%s/admin_views/person/1/' % self.urlbit)
     206        self.assert_('_saveasnew' in response.content)
     207        post_data = {'_saveasnew':'', 'name':'John M', 'gender':3, 'alive':'checked'}
     208        response = self.client.post('/test_admin/%s/admin_views/person/1/' % self.urlbit, post_data)
     209        self.assert_('_saveasnew' not in response.content)
     210        self.assert_('../add/' in response.content)
     211        self.assert_('Add person' in response.content)
     212
    185213class CustomModelAdminTest(AdminViewBasicTest):
    186214    urlbit = "admin2"
    187215
  • tests/regressiontests/admin_views/models.py

     
    156156    list_filter = ('gender',)
    157157    search_fields = ('name',)
    158158    ordering = ["id"]
     159    save_as = True
    159160
    160161class Persona(models.Model):
    161162    """
Back to Top