mirror of
https://github.com/django/django.git
synced 2025-08-01 17:42:56 +00:00
File storage refactoring, adding far more flexibility to Django's file handling. The new files.txt document has details of the new features.
This is a backwards-incompatible change; consult BackwardsIncompatibleChanges for details. Fixes #3567, #3621, #4345, #5361, #5655, #7415. Many thanks to Marty Alchin who did the vast majority of this work. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8244 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c49eac7d4f
commit
7899568e01
33 changed files with 1585 additions and 457 deletions
66
tests/regressiontests/file_storage/tests.py
Normal file
66
tests/regressiontests/file_storage/tests.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
"""
|
||||
Tests for the file storage mechanism
|
||||
|
||||
>>> import tempfile
|
||||
>>> from django.core.files.storage import FileSystemStorage
|
||||
>>> from django.core.files.base import ContentFile
|
||||
|
||||
>>> temp_storage = FileSystemStorage(location=tempfile.gettempdir())
|
||||
|
||||
# Standard file access options are available, and work as expected.
|
||||
|
||||
>>> temp_storage.exists('storage_test')
|
||||
False
|
||||
>>> file = temp_storage.open('storage_test', 'w')
|
||||
>>> file.write('storage contents')
|
||||
>>> file.close()
|
||||
|
||||
>>> temp_storage.exists('storage_test')
|
||||
True
|
||||
>>> file = temp_storage.open('storage_test', 'r')
|
||||
>>> file.read()
|
||||
'storage contents'
|
||||
>>> file.close()
|
||||
|
||||
>>> temp_storage.delete('storage_test')
|
||||
>>> temp_storage.exists('storage_test')
|
||||
False
|
||||
|
||||
# Files can only be accessed if they're below the specified location.
|
||||
|
||||
>>> temp_storage.exists('..')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SuspiciousOperation: Attempted access to '..' denied.
|
||||
>>> temp_storage.open('/etc/passwd')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SuspiciousOperation: Attempted access to '/etc/passwd' denied.
|
||||
|
||||
# Custom storage systems can be created to customize behavior
|
||||
|
||||
>>> class CustomStorage(FileSystemStorage):
|
||||
... def get_available_name(self, name):
|
||||
... # Append numbers to duplicate files rather than underscores, like Trac
|
||||
...
|
||||
... parts = name.split('.')
|
||||
... basename, ext = parts[0], parts[1:]
|
||||
... number = 2
|
||||
...
|
||||
... while self.exists(name):
|
||||
... name = '.'.join([basename, str(number)] + ext)
|
||||
... number += 1
|
||||
...
|
||||
... return name
|
||||
>>> custom_storage = CustomStorage(tempfile.gettempdir())
|
||||
|
||||
>>> first = custom_storage.save('custom_storage', ContentFile('custom contents'))
|
||||
>>> first
|
||||
u'custom_storage'
|
||||
>>> second = custom_storage.save('custom_storage', ContentFile('more contents'))
|
||||
>>> second
|
||||
u'custom_storage.2'
|
||||
|
||||
>>> custom_storage.delete(first)
|
||||
>>> custom_storage.delete(second)
|
||||
"""
|
Loading…
Add table
Add a link
Reference in a new issue