Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'develop'

  • Loading branch information...
commit dba1a89b7299832d6fe0f6f88ecf27e9d58a6476 2 parents f13ed6f + a306f73
@flupke flupke authored
View
3  requirements.txt
@@ -0,0 +1,3 @@
+django>=1.3
+django-jenkins
+django-nose
View
4 setup.py
@@ -1,13 +1,13 @@
#!/usr/bin/env python
from setuptools import setup, find_packages
-import sys, os
+import os
here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()
NEWS = open(os.path.join(here, 'NEWS.txt')).read()
-version = '0.2'
+version = '0.3'
install_requires = [
# List your project dependencies here.
View
1  src/fsfield/__init__.py
@@ -1 +1,2 @@
from fsfield.fields import FileStorageField
+from fsfield.core import model_instance_field_path, default_storage
View
17 src/fsfield/core.py
@@ -5,8 +5,7 @@
from fsfield import settings
-# Storage instances
-_storage_cache = {}
+_default_storage = None
def path_hash(value):
@@ -33,6 +32,10 @@ def model_instance_field_path(instance, field_name):
"""
Get the path used to store the Django model *instance* field named
*field_name*.
+
+ Note that the returned path is a relative path, to get the absolute path,
+ you must pass the value returned by this function to your storage's
+ :meth:`~django.core.files.storage.Storage.path` method.
"""
return op.join(
instance._meta.app_label,
@@ -45,10 +48,10 @@ def default_storage():
"""
Get the default storage instance configured in settings.
"""
- args, kwargs = settings.DEFAULT_STORAGE_ARGS
- key = (settings.DEFAULT_STORAGE_CLASS, tuple(args), tuple(kwargs.items()))
- if key not in _storage_cache:
+ global _default_storage
+ if _default_storage is None:
module, cls_name = settings.DEFAULT_STORAGE_CLASS.rsplit(".", 1)
+ args, kwargs = settings.DEFAULT_STORAGE_ARGS
cls = getattr(__import__(module, {}, {}, cls_name), cls_name)
- _storage_cache[key] = cls(*args, **kwargs)
- return _storage_cache[key]
+ _default_storage = cls(*args, **kwargs)
+ return _default_storage
View
21 src/fsfield/fields.py
@@ -17,9 +17,9 @@ def path(self, obj):
def __get__(self, obj, type=None):
if obj is None:
- raise AttributeError("can only be accessed via instances")
+ return
if obj.pk is None:
- raise ValueError("you must save the object the database before "
+ raise ValueError("you must save the object to the database before "
"accessing this field")
path = self.path(obj)
if not self.storage.exists(path):
@@ -31,9 +31,9 @@ def __get__(self, obj, type=None):
def __set__(self, obj, value):
if obj is None:
- raise AttributeError("can only be accessed via instances")
+ return
if obj.pk is None:
- raise ValueError("you must save the object the database before "
+ raise ValueError("you must save the object to the database before "
"accessing this field")
path = self.path(obj)
directory = op.dirname(self.storage.path(path))
@@ -65,9 +65,13 @@ class FileStorageField(object):
*default* is the value returned when the file associated to the field
doesn't exist.
+
+ *filename* may be used to customize the name of the files corresponding to
+ this field. The field name is used by default.
"""
- def __init__(self, storage=None, load=None, dump=None, default=None):
+ def __init__(self, storage=None, load=None, dump=None, default=None,
+ filename=None):
self.load = load
self.dump = dump
if storage is None:
@@ -75,8 +79,13 @@ def __init__(self, storage=None, load=None, dump=None, default=None):
else:
self.storage = storage
self.default = default
+ self.filename = filename
def contribute_to_class(self, cls, name):
- descriptor = FileStorageFieldDescriptor(name, self.storage,
+ if self.filename is not None:
+ filename = self.filename
+ else:
+ filename = name
+ descriptor = FileStorageFieldDescriptor(filename, self.storage,
self.default, self.load, self.dump)
setattr(cls, name, descriptor)
View
1  test_project/settings.py
@@ -121,6 +121,7 @@
'django.contrib.staticfiles',
'fsfield',
'django_nose',
+ 'django_jenkins',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
Please sign in to comment.
Something went wrong with that request. Please try again.