Ticket #2070: 2070_revision7339_formhandling.diff
File 2070_revision7339_formhandling.diff, 11.4 KB (added by , 17 years ago) |
---|
-
django/oldforms/__init__.py
680 680 self.field_name, self.is_required = field_name, is_required 681 681 self.validator_list = [self.isNonEmptyFile] + validator_list 682 682 683 def isNonEmptyFile(self, field_data, all_data):684 try:685 content = field_data['content']686 except TypeError:687 raise validators.CriticalValidationError, ugettext("No file was submitted. Check the encoding type on the form.")688 if not content:683 def isNonEmptyFile(self, new_data, all_data): 684 if hasattr(new_data, 'upload_errors'): 685 upload_errors = new_data.upload_errors() 686 if upload_errors: 687 raise validators.CriticalValidationError, upload_errors 688 if not new_data.file_size: 689 689 raise validators.CriticalValidationError, ugettext("The submitted file is empty.") 690 690 691 691 def render(self, data): 692 692 return mark_safe(u'<input type="file" id="%s" class="v%s" name="%s" />' % \ 693 693 (self.get_id(), self.__class__.__name__, self.field_name)) 694 694 695 def prepare(self, new_data): 696 if hasattr(new_data, 'upload_errors'): 697 upload_errors = new_data.upload_errors() 698 new_data[self.field_name] = { '_file_upload_error': upload_errors } 699 695 700 def html2python(data): 696 701 if data is None: 697 702 raise EmptyValue -
django/db/models/base.py
13 13 from django.utils.datastructures import SortedDict 14 14 from django.utils.functional import curry 15 15 from django.utils.encoding import smart_str, force_unicode, smart_unicode 16 from django.core.files.filemove import file_move_safe 16 17 from django.conf import settings 17 18 from itertools import izip 18 19 import types … … 384 385 def _get_FIELD_size(self, field): 385 386 return os.path.getsize(self._get_FIELD_filename(field)) 386 387 387 def _save_FIELD_file(self, field, filename, raw_ contents, save=True):388 def _save_FIELD_file(self, field, filename, raw_field, save=True): 388 389 directory = field.get_directory_name() 389 390 try: # Create the date-based directory if it doesn't exist. 390 391 os.makedirs(os.path.join(settings.MEDIA_ROOT, directory)) 391 392 except OSError: # Directory probably already exists. 392 393 pass 394 395 if filename is None: 396 filename = raw_field.file_name 397 393 398 filename = field.get_filename(filename) 394 399 395 400 # If the filename already exists, keep adding an underscore to the name of … … 406 411 setattr(self, field.attname, filename) 407 412 408 413 full_filename = self._get_FIELD_filename(field) 409 fp = open(full_filename, 'wb') 410 fp.write(raw_contents) 411 fp.close() 414 if hasattr(raw_field, 'temporary_file_path'): 415 raw_field.close() 416 file_move_safe(raw_field.temporary_file_path(), full_filename) 417 else: 418 from django.utils import file_locks 419 fp = open(full_filename, 'wb') 420 # exclusive lock 421 file_locks.lock(fp, file_locks.LOCK_EX) 422 # Stream it into the file, from where it is. 423 for chunk in raw_field.chunk(65535): 424 fp.write(chunk) 425 fp.close() 412 426 413 427 # Save the width and/or height, if applicable. 414 428 if isinstance(field, ImageField) and (field.width_field or field.height_field): -
django/db/models/fields/__init__.py
785 785 setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self)) 786 786 setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self)) 787 787 setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, field=self)) 788 setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True: instance._save_FIELD_file(self, filename, raw_contents, save)) 788 setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_field, save=True: instance._save_FIELD_file(self, filename, raw_field, save)) 789 setattr(cls, 'move_%s_file' % self.name, lambda instance, raw_field, save=True: instance._save_FIELD_file(self, None, raw_field, save)) 789 790 dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls) 790 791 791 792 def delete_file(self, instance): … … 808 809 if new_data.get(upload_field_name, False): 809 810 func = getattr(new_object, 'save_%s_file' % self.name) 810 811 if rel: 811 func(new_data[upload_field_name][0]["filename"], new_data[upload_field_name][0] ["content"], save)812 func(new_data[upload_field_name][0]["filename"], new_data[upload_field_name][0], save) 812 813 else: 813 func(new_data[upload_field_name]["filename"], new_data[upload_field_name] ["content"], save)814 func(new_data[upload_field_name]["filename"], new_data[upload_field_name], save) 814 815 815 816 def get_directory_name(self): 816 817 return os.path.normpath(force_unicode(datetime.datetime.now().strftime(smart_str(self.upload_to)))) … … 823 824 def save_form_data(self, instance, data): 824 825 from django.newforms.fields import UploadedFile 825 826 if data and isinstance(data, UploadedFile): 826 getattr(instance, "save_%s_file" % self.name)(data.filename, data. content, save=False)827 getattr(instance, "save_%s_file" % self.name)(data.filename, data.data, save=False) 827 828 828 829 def formfield(self, **kwargs): 829 830 defaults = {'form_class': forms.FileField} -
django/core/files/filelocks.py
1 """ 2 Locking portability by Jonathan Feignberg <jdf@pobox.com> in python cookbook 3 4 Example Usage:: 5 6 from django.utils import file_locks 7 8 f = open('./file', 'wb') 9 10 file_locks.lock(f, file_locks.LOCK_EX) 11 f.write('Django') 12 f.close() 13 """ 14 15 16 import os 17 18 __all__ = ['LOCK_EX','LOCK_SH','LOCK_NB','lock','unlock'] 19 20 if os.name == 'nt': 21 import win32con 22 import win32file 23 import pywintypes 24 LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK 25 LOCK_SH = 0 26 LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY 27 __overlapped = pywintypes.OVERLAPPED() 28 elif os.name == 'posix': 29 import fcntl 30 LOCK_EX = fcntl.LOCK_EX 31 LOCK_SH = fcntl.LOCK_SH 32 LOCK_NB = fcntl.LOCK_NB 33 else: 34 raise RuntimeError("Locking only defined for nt and posix platforms") 35 36 if os.name == 'nt': 37 def lock(file, flags): 38 hfile = win32file._get_osfhandle(file.fileno()) 39 win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped) 40 41 def unlock(file): 42 hfile = win32file._get_osfhandle(file.fileno()) 43 win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) 44 45 elif os.name =='posix': 46 def lock(file, flags): 47 fcntl.flock(file.fileno(), flags) 48 49 def unlock(file): 50 fcntl.flock(file.fileno(), fcntl.LOCK_UN) -
django/core/files/filemove.py
1 import os 2 3 __all__ = ('file_move_safe',) 4 5 try: 6 import shutil 7 file_move = shutil.move 8 except ImportError: 9 file_move = os.rename 10 11 def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False): 12 """ 13 Moves a file from one location to another in the safest way possible. 14 15 First, it tries using shutils.move, which is OS-dependent but doesn't 16 break with change of filesystems. Then it tries os.rename, which will 17 break if it encounters a change in filesystems. Lastly, it streams 18 it manually from one file to another in python. 19 20 Without ``allow_overwrite``, if the destination file exists, the 21 file will raise an IOError. 22 """ 23 24 from django.core.files import filelocks 25 26 if old_file_name == new_file_name: 27 # No file moving takes place. 28 return 29 30 if not allow_overwrite and os.path.exists(new_file_name): 31 raise IOError, "Django does not allow overwriting files." 32 33 try: 34 file_move(old_file_name, new_file_name) 35 return 36 except OSError: # moving to another filesystem 37 pass 38 39 new_file = open(new_file_name, 'wb') 40 # exclusive lock 41 filelocks.lock(new_file, filelocks.LOCK_EX) 42 old_file = open(old_file_name, 'rb') 43 current_chunk = None 44 45 while current_chunk != '': 46 current_chunk = old_file.read(chunk_size) 47 new_file.write(current_chunk) 48 49 new_file.close() 50 old_file.close() 51 52 os.remove(old_file_name) 53 -
django/newforms/fields.py
416 416 417 417 class UploadedFile(StrAndUnicode): 418 418 "A wrapper for files uploaded in a FileField" 419 def __init__(self, filename, content):419 def __init__(self, filename, data): 420 420 self.filename = filename 421 self. content = content421 self.data = data 422 422 423 423 def __unicode__(self): 424 424 """ … … 445 445 elif not data and initial: 446 446 return initial 447 447 try: 448 f = UploadedFile(data['filename'], data ['content'])448 f = UploadedFile(data['filename'], data) 449 449 except TypeError: 450 450 raise ValidationError(self.error_messages['invalid']) 451 451 except KeyError: 452 452 raise ValidationError(self.error_messages['missing']) 453 if not f. content:453 if not f.data.file_size: 454 454 raise ValidationError(self.error_messages['empty']) 455 455 return f 456 456 … … 470 470 elif not data and initial: 471 471 return initial 472 472 from PIL import Image 473 from cStringIO import StringIO 473 474 # We need to get the file, it either has a path 475 # or we have to read it all into memory... 476 if hasattr(data, 'temporary_file_path'): 477 file = data.temporary_file_path() 478 else: 479 try: 480 from cStringIO import StringIO 481 except ImportError: 482 from StringIO import StringIO 483 file = StringIO(data.read()) 484 474 485 try: 475 486 # load() is the only method that can spot a truncated JPEG, 476 487 # but it cannot be called sanely after verify() 477 trial_image = Image.open( StringIO(f.content))488 trial_image = Image.open(file) 478 489 trial_image.load() 479 490 # verify() is the only method that can spot a corrupt PNG, 480 491 # but it must be called immediately after the constructor 481 trial_image = Image.open( StringIO(f.content))492 trial_image = Image.open(file) 482 493 trial_image.verify() 483 494 except Exception: # Python Imaging Library doesn't recognize it as an image 484 495 raise ValidationError(self.error_messages['invalid_image'])