from formencode import *
-from formencode import validators
+from formencode import validators
import pylons
_ = validators._ # dummy translation string
# Model-based validators
class Unique(validators.FancyValidator):
-
+
"""
Checks if given value is unique to the model.Will check the state: if state object
is the same as the instance, or the state contains a property with the same name
as the context name. For example:
-
+
validator = validators.Unique(model.NewsItem, "title", context_name="news_item")
-
+
This will check if there is an existing instance with the same "title". If there
is a matching instance, will check if the state passed into the validator is the
same instance, or if the state contains a property "news_item" which is the same
instance.
"""
-
+
__unpackargs__ = ('model', 'attr', "model_name", "context_name", "attribute_name")
messages = {
'notUnique' : _("%(modelName)s already exists with this %(attrName)s"),
}
-
+
model_name = "Item"
attribute_name = None
context_name = None
-
+
def validate_python(self, value, state):
instance = self.model.get_by(**{self.attr : value})
if instance:
if state != instance and \
getattr(state, context_name, None) != instance:
attr_name = self.attribute_name or self.attr
- raise Invalid(self.message('notUnique', state,
+ raise Invalid(self.message('notUnique', state,
modelName=self.model_name,
- attrName=attr_name),
+ attrName=attr_name),
value, state)
-
+
validators.Unique = Unique
def get_path(space, hash):
return "/" + os.path.join( space, hash[:2], hash[2:] )
-
-def save_file(space, temp):
-
+
+def save_file(space, temp):
+
dest_root = os.path.join( config['app_conf']['static_root'], space )
-
+
# we don't know where we're going to save this stuff yet,
# so I guess we'll write it to another tempfile. One we know the path of.
- # I'm assuming the tempfile we get from pylons is set to delete itself
+ # I'm assuming the tempfile we get from pylons is set to delete itself
# when it closes, and has no visible path. Maybe I'm wrong?
intermediate_file_descriptor, intermediate_path = tempfile.mkstemp()
-
+
# that function gives me an integer file descriptor for some reason.
intermediate_file = os.fdopen(intermediate_file_descriptor, "wb")
-
+
sha1 = hashlib.sha1()
while 1:
data = temp.file.read(chunk_size)
"""
This module can be used for loading data into your models, for example when setting up default application data,
-unit tests, JSON export/import and importing/exporting legacy data. Data is serialized to and from the JSON format.
+unit tests, JSON export/import and importing/exporting legacy data. Data is serialized to and from the JSON format.
"""
VALID_FIXTURE_FILE_EXTENSIONS = ['.json']
def load_data(model, filename=None, base_dir=None):
- """Installs provided fixture files into given model. Filename may be directory, file or list of dirs or files. If filename is
- None, assumes that source file is located in fixtures/model_module_name/model_tablename.yaml of your application directory,
- for example MyProject/fixtures/news/newsitems.yaml. The base_dir argument is the top package of the application unless
+ """Installs provided fixture files into given model. Filename may be directory, file or list of dirs or files. If filename is
+ None, assumes that source file is located in fixtures/model_module_name/model_tablename.yaml of your application directory,
+ for example MyProject/fixtures/news/newsitems.yaml. The base_dir argument is the top package of the application unless
specified. You can also pass the name of a table instead of a model class."""
if type(model) is types.StringType:
def load_data_to_table(table, filename=None, base_dir=None):
"""Installs data directly into a table. Useful if table does not have a corresponding model, for example a many-to-many join table.
"""
-
+
if filename is None:
filename = _default_fixture_path_for_table(table, base_dir)
_load_data_to_table(table, filename)
-
+
def dump_data(model, filename=None, **params):
- """Dumps data to given destination. Params are optional arguments for selecting data. If filename is None, assumes that destination
- file is located in fixtures/model_module_name/model_name_lowercase.yaml of your application directory, for example
+ """Dumps data to given destination. Params are optional arguments for selecting data. If filename is None, assumes that destination
+ file is located in fixtures/model_module_name/model_name_lowercase.yaml of your application directory, for example
MyProject/fixtures/news/newsitem.yaml.
"""
-
+
if filename is None:
filename = _default_fixture_path_for_model(model)
_dump_data_to_file(model, filename, **params)
module_dirs = model.__module__.split('.', 2)[-1].split('.')
for dir in module_dirs:
path = os.path.join(path, dir)
- return os.path.join(path, model.table.name + '.json')
+ return os.path.join(path, model.table.name + '.json')
def _default_fixture_path_for_table(table, base_dir=None):
if base_dir is None:
def _is_fixture_file(filename):
basename, ext = os.path.splitext(filename)
return (ext.lower() in VALID_FIXTURE_FILE_EXTENSIONS)
-
+
def _load_data_from_dir(model, dirname):
for dirpath, dirnames, filenames in os.walk(dirname):
for filename in filenames:
_load_data_from_file(model, filename)
-
+
def _load_data_from_file(model, filename):
- if not _is_fixture_file(filename):
+ if not _is_fixture_file(filename):
return
fp = file(filename, 'r')
data = simplejson.load(fp)
elif type(data) is types.DictType:
retval = {}
for key, item in data.iteritems():
- retval[key] = _load_instance_from_dict(model, item)
+ retval[key] = _load_instance_from_dict(model, item)
return retval
def _load_data_to_table(tablename, filename):
- if not _is_fixture_file(filename):
+ if not _is_fixture_file(filename):
return
fp = file(filename, 'r')
data = simplejson.load(fp)
for key, item in data.iteritems():
table.insert(item).execute()
return data
-
+
def _dump_data_to_file(model, filename, **params):
if params:
queryset = model.select_by(**params)
fp = file(filename, 'w')
simplejson.dump(data, fp)
fp.close()
-
+
def _load_instance_from_dict(model, dict):
- if not dict: return
+ if not dict: return
instance = model()
fields = model._descriptor.fields.keys()
for k, v in dict.iteritems():
for field in fields:
d[field] = getattr(instance, field)
return d
-
+
__all__ = ['load_data', 'dump_data']