Skip to content

Commit

Permalink
[#1262] make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Oct 17, 2013
1 parent 7d15d59 commit 6cc143f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
3 changes: 2 additions & 1 deletion ckan/config/middleware.py
Expand Up @@ -23,6 +23,7 @@
from ckan.plugins import PluginImplementations
from ckan.plugins.interfaces import IMiddleware
from ckan.lib.i18n import get_locales_from_config
import ckan.lib.uploader as uploader

from ckan.config.environment import load_environment
import ckan.lib.app_globals as app_globals
Expand Down Expand Up @@ -148,7 +149,7 @@ def make_app(conf, full_stack=True, static_files=True, **app_conf):
cache_max_age=static_max_age)
static_parsers = [static_app, app]

storage_directory = config.get('ckan.storage_path')
storage_directory = uploader.get_storage_path()
if storage_directory:
path = os.path.join(storage_directory, 'storage')
try:
Expand Down
41 changes: 24 additions & 17 deletions ckan/lib/uploader.py
Expand Up @@ -10,6 +10,7 @@

_storage_path = None


def get_storage_path():
'''Function to cache storage path'''
global _storage_path
Expand All @@ -27,21 +28,24 @@ def get_storage_path():
_storage_path = ofs_storage_dir
return _storage_path
elif ofs_impl:
log.critical('''We only support local file storage form version 2.2 of ckan
please specify ckan.storage_path in your config for your uploads''')
log.critical('''We only support local file storage form version 2.2
of ckan please specify ckan.storage_path in your
config for your uploads''')
_storage_path = False
else:
log.critical('''Please specify a ckan.storage_path in your config for your uploads''')
log.critical('''Please specify a ckan.storage_path in your config
for your uploads''')
_storage_path = False

return _storage_path



class Upload(object):
def __init__(self, object_type, old_filename=None):
''' Setup upload by creating a subdirectory of the storage directory of name
object_type. old_filename is the name of the file in the url field last time'''
''' Setup upload by creating a subdirectory of the storage directory
of name object_type. old_filename is the name of the file in the url
field last time'''

self.storage_path = None
self.filename = None
self.filepath = None
Expand All @@ -60,20 +64,21 @@ def __init__(self, object_type, old_filename=None):
self.old_filepath = os.path.join(self.storage_path, old_filename)

def update_data_dict(self, data_dict, url_field, file_field, clear_field):
''' Manipulate data from the data_dict. url_field is the name of the field
where the upload is going to be. file_field is name of the key where the
FieldStorage is kept (i.e the field where the file data actually is). clear_field
is the name of a boolean field which requests the upload to be deleted. This needs
to be called before it reaches any validators'''

if not self.storage_path:
return
''' Manipulate data from the data_dict. url_field is the name of the
field where the upload is going to be. file_field is name of the key
where the FieldStorage is kept (i.e the field where the file data
actually is). clear_field is the name of a boolean field which
requests the upload to be deleted. This needs to be called before
it reaches any validators'''

self.url = data_dict.get(url_field, '')
self.clear = data_dict.pop(clear_field, None)
self.file_field = file_field
self.upload_field_storage = data_dict.pop(file_field, None)

if not self.storage_path:
return

if isinstance(self.upload_field_storage, cgi.FieldStorage):
self.filename = self.upload_field_storage.filename
self.filename = str(datetime.datetime.utcnow()) + self.filename
Expand All @@ -90,9 +95,11 @@ def update_data_dict(self, data_dict, url_field, file_field, clear_field):
data_dict[url_field] = ''

def upload(self, max_size=2):
''' Actually upload the file. This should happen just before a commit but after
the data has been validated and flushed to the db. This is so we do not store anything
unless the request is actually good. Max_size is size in MB maximum of the file'''
''' Actually upload the file.
This should happen just before a commit but after the data has
been validated and flushed to the db. This is so we do not store
anything unless the request is actually good.
max_size is size in MB maximum of the file'''

if self.filename:
output_file = open(self.tmp_filepath, 'wb')
Expand Down

0 comments on commit 6cc143f

Please sign in to comment.