Skip to content

Commit

Permalink
[1262] use logic instead of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Oct 9, 2013
1 parent 66105d3 commit 1b62423
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
11 changes: 0 additions & 11 deletions ckan/controllers/group.py
Expand Up @@ -12,7 +12,6 @@
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.maintain as maintain
import ckan.lib.uploader as uploader
import ckan.lib.navl.dictization_functions as dict_fns
import ckan.logic as logic
import ckan.lib.search as search
Expand Down Expand Up @@ -491,17 +490,12 @@ def _get_group_type(self, id):

def _save_new(self, context, group_type=None):
try:
upload = uploader.Upload('group')
upload.register_request(request, 'image_url',
'image_upload', 'clear_upload')

data_dict = clean_dict(dict_fns.unflatten(
tuplize_dict(parse_params(request.params))))
data_dict['type'] = group_type or 'group'
context['message'] = data_dict.get('log_message', '')
data_dict['users'] = [{'name': c.user, 'capacity': 'admin'}]
group = self._action('group_create')(context, data_dict)
upload.upload()

# Redirect to the appropriate _read route for the type of group
h.redirect_to(group['type'] + '_read', id=group['name'])
Expand Down Expand Up @@ -529,17 +523,12 @@ def _save_edit(self, id, context):
try:
old_group = self._action('group_show')(context, {"id": id})
old_image_url = old_group.get('image_url')
upload = uploader.Upload('group', old_image_url)
upload.register_request(request, 'image_url',
'image_upload', 'clear_upload')

data_dict = clean_dict(dict_fns.unflatten(
tuplize_dict(parse_params(request.params))))
context['message'] = data_dict.get('log_message', '')
data_dict['id'] = id
context['allow_partial_update'] = True
group = self._action('group_update')(context, data_dict)
upload.upload()
if id != group['name']:
self._force_reindex(group)

Expand Down
14 changes: 7 additions & 7 deletions ckan/lib/uploader.py
Expand Up @@ -19,25 +19,25 @@ def __init__(self, object_type, old_filename=None):
self.filename = None
self.filepath = None

def register_request(self, request, url_field, file_field, clear_field):
self.url = request.POST.get(url_field, '')
self.clear = request.POST.get(clear_field)
self.upload_field_storage = request.POST.pop(file_field, None)
def update_data_dict(self, data_dict, url_field, file_field, clear_field):
self.url = data_dict.get(url_field, '')
self.clear = data_dict.pop(clear_field, None)
self.upload_field_storage = data_dict.pop(file_field, None)

if isinstance(self.upload_field_storage, cgi.FieldStorage):
self.filename = self.upload_field_storage.filename
self.filename = str(datetime.datetime.utcnow()) + self.filename
self.filename = munge.munge_filename(self.filename)
self.filepath = os.path.join(self.storage_path, self.filename)
request.POST[url_field] = self.filename
data_dict[url_field] = self.filename
self.upload_file = self.upload_field_storage.file
self.tmp_filepath = self.filepath + '~'
### keep the file if there has been no change
elif self.old_filename and not self.old_filename.startswith('http'):
if not self.clear:
request.POST[url_field] = self.old_filename
data_dict[url_field] = self.old_filename
if self.clear and self.url == self.old_filename:
request.POST[url_field] = ''
data_dict[url_field] = ''


def upload(self):
Expand Down
7 changes: 6 additions & 1 deletion ckan/logic/action/create.py
Expand Up @@ -15,6 +15,7 @@
import ckan.lib.dictization.model_dictize as model_dictize
import ckan.lib.dictization.model_save as model_save
import ckan.lib.navl.dictization_functions
import ckan.lib.uploader as uploader

from ckan.common import _

Expand Down Expand Up @@ -446,6 +447,7 @@ def member_create(context, data_dict=None):
if not obj:
raise NotFound('%s was not found.' % obj_type.title())


# User must be able to update the group to add a member to it
_check_access('group_update', context, data_dict)

Expand Down Expand Up @@ -475,7 +477,9 @@ def _group_or_org_create(context, data_dict, is_org=False):
parent = context.get('parent', None)
data_dict['is_organization'] = is_org


upload = uploader.Upload('group')
upload.update_data_dict(data_dict, 'image_url',
'image_upload', 'clear_upload')
# get the schema
group_plugin = lib_plugins.lookup_group_plugin(
group_type=data_dict.get('type'))
Expand Down Expand Up @@ -561,6 +565,7 @@ def _group_or_org_create(context, data_dict, is_org=False):
logic.get_action('activity_create')(activity_create_context,
activity_dict)

upload.upload()
if not context.get('defer_commit'):
model.repo.commit()
context["group"] = group
Expand Down
7 changes: 7 additions & 0 deletions ckan/logic/action/update.py
Expand Up @@ -19,6 +19,7 @@
import ckan.lib.plugins as lib_plugins
import ckan.lib.email_notifications as email_notifications
import ckan.lib.search as search
import ckan.lib.uploader as uploader

from ckan.common import _, request

Expand Down Expand Up @@ -424,6 +425,10 @@ def _group_or_org_update(context, data_dict, is_org=False):
except AttributeError:
schema = group_plugin.form_to_db_schema()

upload = uploader.Upload('group', group.image_url)
upload.update_data_dict(data_dict, 'image_url',
'image_upload', 'clear_upload')

if is_org:
_check_access('organization_update', context, data_dict)
else:
Expand Down Expand Up @@ -528,9 +533,11 @@ def _group_or_org_update(context, data_dict, is_org=False):
# TODO: Also create an activity detail recording what exactly changed
# in the group.

upload.upload()
if not context.get('defer_commit'):
model.repo.commit()


return model_dictize.group_dictize(group, context)

def group_update(context, data_dict):
Expand Down
7 changes: 3 additions & 4 deletions ckan/templates/organization/snippets/organization_form.html
Expand Up @@ -19,13 +19,12 @@

{{ form.markdown('description', label=_('Description'), id='field-description', placeholder=_('A little information about my organization...'), value=data.description, error=errors.description) }}

{{ form.input('image_url', label=_('Image URL'), id='field-image-url', type='url', placeholder=_('http://example.com/my-image.jpg'), value=data.image_url, error=errors.image_url, classes=['control-full']) }}
{{ form.input('image_url', label=_('Image URL'), id='field-image-url', placeholder=_('http://example.com/my-image.jpg'), value=data.image_url, error=errors.image_url, classes=['control-full']) }}

{% if h.uploads_enabled() %}
{{ form.input('image_upload', label=_('Image Upload'), id='field-image-upload', type='file', placeholder='', value=data.image_url, error='', classes=['control-full']) }}
{% set upload_file = data.get('upload_file', '') %}
{% if upload_file %}
{{ form.checkbox('clear_upload', label=_('Clear Upload') + ' ' + upload_file, id='field-clear-upload', value='true', error='', classes=['control-full']) }}
{% if data.image_url and not data.image_url.startswith('http') %}
{{ form.checkbox('clear_upload', label=_('Clear Upload'), id='field-clear-upload', value='true', error='', classes=['control-full']) }}
{% endif %}
{% endif %}

Expand Down

0 comments on commit 1b62423

Please sign in to comment.