Ticket #10002: 10002.2.diff

File 10002.2.diff, 5.1 KB (added by David Gouldin, 16 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/fixtures/admin-views-users.xml

     
    8888        <field type="DateTimeField" name="date">2009-03-18 11:54:58</field>
    8989        <field to="admin_views.section" name="section" rel="ManyToOneRel">1</field>
    9090    </object>
     91    <object pk="1" model="admin_views.gallery">
     92        <field type="CharField" name="name">Test gallery</field>
     93    </object>
     94    <object pk="1" model="admin_views.picture">
     95        <field type="CharField" name="name">Test picture</field>
     96        <field type="FileField" name="image">test.jpg</field>
     97        <field to="admin_views.gallery" name="gallery" rel="ManyToOneRel">1</field>
     98    </object>
    9199
    92100
    93101</django-objects>
     102 No newline at end of file
  • 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
     
    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        # upload test file for Picture fixture
     960        tdir = tempfile.gettempdir()
     961        image = open(os.path.join(tdir, u'test.jpg'.encode('utf-8')), 'w+b')
     962        image.write('b' * (2 ** 10))
     963        image.close()
     964
     965    def tearDown(self):
     966        self.client.logout()
     967
     968    def testInlineFileUploadEditValidationErrorPost(self):
     969        post_data = {
     970            "name": u"Test Gallery",
     971            "pictures-TOTAL_FORMS": u"2",
     972            "pictures-INITIAL_FORMS": u"1",
     973            "pictures-0-id": u"1",
     974            "pictures-0-gallery": u"1",
     975            "pictures-0-name": "Test Picture",
     976            "pictures-0-image": "",
     977            "pictures-1-id": "",
     978            "pictures-1-gallery": "1",
     979            "pictures-1-name": "Test Picture 2",
     980            "pictures-1-image": "",
     981        }
     982        response = self.client.post('/test_admin/%s/admin_views/gallery/1/' % self.urlbit, post_data)
     983        self.failUnless(response._container[0].find("Currently:") > -1)
     984
  • 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