Ticket #10002: 10002.3.diff

File 10002.3.diff, 4.8 KB (added by David Gouldin, 15 years ago)
  • django/forms/forms.py

     
    371371            if callable(data):
    372372                data = data()
    373373        else:
    374             data = self.data
     374            if isinstance(self.field, FileField) and self.data is None:
     375                data = self.form.initial.get(self.name, self.field.initial)
     376            else:
     377                data = self.data
    375378        if not only_initial:
    376379            name = self.html_name
    377380        else:
  • tests/regressiontests/admin_views/tests.py

     
    22
    33import re
    44import datetime
     5import os
    56
     7from django.core.files import temp as tempfile
    68from django.test import TestCase
    79from django.contrib.auth.models import User, Permission
    810from django.contrib.contenttypes.models import ContentType
     
    1315from django.utils.html import escape
    1416
    1517# local test models
    16 from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast
     18from models import Article, CustomArticle, Section, ModelWithStringPrimaryKey, Person, Persona, FooAccount, BarAccount, Subscriber, ExternalSubscriber, Podcast, Picture, Gallery
    1719
    1820try:
    1921    set
     
    946948        }
    947949        response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data)
    948950        self.failUnlessEqual(response.status_code, 302)
     951
     952class AdminInlineFileUploadTest(TestCase):
     953    fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
     954    urlbit = 'admin'
     955
     956    def setUp(self):
     957        self.client.login(username='super', password='secret')
     958       
     959        # Set up test Picture and Gallery.
     960        # These must be set up here instead of in fixtures in order to allow Picture
     961        # to use a NamedTemporaryFile.
     962        tdir = tempfile.gettempdir()
     963        file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir)
     964        file1.write('a' * (2 ** 21))
     965        filename = file1.name
     966        file1.close()
     967        g = Gallery(name="Test Gallery")
     968        g.save()
     969        p = Picture(name="Test Picture", image=filename, gallery=g)
     970        p.save()
     971
     972    def tearDown(self):
     973        self.client.logout()
     974
     975    def testInlineFileUploadEditValidationErrorPost(self):
     976        post_data = {
     977            "name": u"Test Gallery",
     978            "pictures-TOTAL_FORMS": u"2",
     979            "pictures-INITIAL_FORMS": u"1",
     980            "pictures-0-id": u"1",
     981            "pictures-0-gallery": u"1",
     982            "pictures-0-name": "Test Picture",
     983            "pictures-0-image": "",
     984            "pictures-1-id": "",
     985            "pictures-1-gallery": "1",
     986            "pictures-1-name": "Test Picture 2",
     987            "pictures-1-image": "",
     988        }
     989        response = self.client.post('/test_admin/%s/admin_views/gallery/1/' % self.urlbit, post_data)
     990        self.failUnless(response._container[0].find("Currently:") > -1)
     991
  • tests/regressiontests/admin_views/models.py

     
    11# -*- coding: utf-8 -*-
     2import tempfile
     3import os
     4from django.core.files.storage import FileSystemStorage
    25from django.db import models
    36from django.contrib import admin
    47from django.core.mail import EmailMessage
     
    248251
    249252    ordering = ('name',)
    250253
     254temp_storage = FileSystemStorage(tempfile.mkdtemp())
     255UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
     256
     257class Gallery(models.Model):
     258    name = models.CharField(max_length=100)
     259
     260class Picture(models.Model):
     261    name = models.CharField(max_length=100)
     262    image = models.FileField(storage=temp_storage, upload_to='test_upload')
     263    gallery = models.ForeignKey(Gallery, related_name="pictures")
     264
     265class PictureInline(admin.TabularInline):
     266    model = Picture
     267    extra = 1
     268
     269class GalleryAdmin(admin.ModelAdmin):
     270    inlines = [PictureInline]
     271
     272class PictureAdmin(admin.ModelAdmin):
     273    pass
     274
    251275admin.site.register(Article, ArticleAdmin)
    252276admin.site.register(CustomArticle, CustomArticleAdmin)
    253277admin.site.register(Section, inlines=[ArticleInline])
     
    259283admin.site.register(Subscriber, SubscriberAdmin)
    260284admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
    261285admin.site.register(Podcast, PodcastAdmin)
     286admin.site.register(Gallery, GalleryAdmin)
     287admin.site.register(Picture, PictureAdmin)
    262288
    263289# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    264290# That way we cover all four cases:
Back to Top