Skip to content

Commit

Permalink
Merge branch 'master' into 3196-common-requests-code
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Dec 12, 2016
2 parents 21a2b18 + 31b8167 commit e5c2ad2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 36 deletions.
11 changes: 5 additions & 6 deletions ckan/lib/cli.py
Expand Up @@ -13,6 +13,7 @@
import logging
import urlparse
from optparse import OptionConflictError
import traceback

import sqlalchemy as sa
import routes
Expand Down Expand Up @@ -851,7 +852,7 @@ def add(self):
user_dict = logic.get_action('user_create')(context, data_dict)
pprint(user_dict)
except logic.ValidationError, e:
error(e)
error(traceback.format_exc())

def remove(self):
import ckan.model as model
Expand Down Expand Up @@ -1491,10 +1492,8 @@ def profile_url(url):
print 'App error: ', url.strip()
except KeyboardInterrupt:
raise
except:
import traceback
traceback.print_exc()
print 'Unknown error: ', url.strip()
except Exception:
error(traceback.format_exc())

output_filename = 'ckan%s.profile' % re.sub('[/?]', '.', url.replace('/', '.'))
profile_command = "profile_url('%s')" % url
Expand Down Expand Up @@ -2478,7 +2477,7 @@ def command(self):
config_filepath, options, self.options.section,
edit=self.options.edit)
except config_tool.ConfigToolError, e:
error(e)
error(traceback.format_exc())


class JobsCommand(CkanCommand):
Expand Down
56 changes: 33 additions & 23 deletions ckan/logic/action/get.py
Expand Up @@ -818,6 +818,9 @@ def user_list(context, data_dict):
:param order_by: which field to sort the list by (optional, default:
``'name'``). Can be any user field or ``edits`` (i.e. number_of_edits).
:type order_by: string
:param all_fields: return full user dictionaries instead of just names.
(optional, default: ``True``)
:type all_fields: boolean
:rtype: list of user dictionaries. User properties include:
``number_of_edits`` which counts the revisions by the user and
Expand All @@ -831,26 +834,30 @@ def user_list(context, data_dict):

q = data_dict.get('q', '')
order_by = data_dict.get('order_by', 'name')
all_fields = asbool(data_dict.get('all_fields', True))

query = model.Session.query(
model.User,
model.User.name.label('name'),
model.User.fullname.label('fullname'),
model.User.about.label('about'),
model.User.about.label('email'),
model.User.created.label('created'),
_select([_func.count(model.Revision.id)],
_or_(
model.Revision.author == model.User.name,
model.Revision.author == model.User.openid
)).label('number_of_edits'),
_select([_func.count(model.Package.id)],
_and_(
model.Package.creator_user_id == model.User.id,
model.Package.state == 'active',
model.Package.private == False,
)).label('number_created_packages')
)
if all_fields:
query = model.Session.query(
model.User,
model.User.name.label('name'),
model.User.fullname.label('fullname'),
model.User.about.label('about'),
model.User.about.label('email'),
model.User.created.label('created'),
_select([_func.count(model.Revision.id)],
_or_(
model.Revision.author == model.User.name,
model.Revision.author == model.User.openid
)).label('number_of_edits'),
_select([_func.count(model.Package.id)],
_and_(
model.Package.creator_user_id == model.User.id,
model.Package.state == 'active',
model.Package.private == False,
)).label('number_created_packages')
)
else:
query = model.Session.query(model.User.name)

if q:
query = model.User.search(q, query, user_name=context.get('user'))
Expand All @@ -861,7 +868,6 @@ def user_list(context, data_dict):
_or_(
model.Revision.author == model.User.name,
model.Revision.author == model.User.openid))))

else:
query = query.order_by(
_case([(
Expand All @@ -879,9 +885,13 @@ def user_list(context, data_dict):

users_list = []

for user in query.all():
result_dict = model_dictize.user_dictize(user[0], context)
users_list.append(result_dict)
if all_fields:
for user in query.all():
result_dict = model_dictize.user_dictize(user[0], context)
users_list.append(result_dict)
else:
for user in query.all():
users_list.append(user[0])

return users_list

Expand Down
2 changes: 2 additions & 0 deletions ckan/model/__init__.py
Expand Up @@ -255,6 +255,8 @@ def delete_all(self):
else:
tables = reversed(self.metadata.sorted_tables)
for table in tables:
if table.name == 'migrate_version':
continue
connection.execute('delete from "%s"' % table.name)
self.session.commit()
log.info('Database table data deleted')
Expand Down
3 changes: 1 addition & 2 deletions ckan/public/base/javascript/main.js
Expand Up @@ -20,7 +20,6 @@ this.ckan = this.ckan || {};
ckan.initialize = function () {
var body = jQuery('body');
var locale = jQuery('html').attr('lang');
var browserLocale = window.navigator.userLanguage || window.navigator.language;
var location = window.location;
var root = location.protocol + '//' + location.host;

Expand All @@ -33,7 +32,7 @@ this.ckan = this.ckan || {};

// Convert all datetimes to the users timezone
jQuery('.automatic-local-datetime').each(function() {
moment.locale(browserLocale);
moment.locale(locale);
var date = moment(jQuery(this).data('datetime'));
if (date.isValid()) {
jQuery(this).html(date.format("LL, LT ([UTC]Z)"));
Expand Down
10 changes: 10 additions & 0 deletions ckan/tests/logic/action/test_get.py
Expand Up @@ -570,6 +570,16 @@ def test_user_list_excludes_deleted_users(self):
assert len(got_users) == 1
assert got_users[0]['name'] == user['name']

def test_user_list_not_all_fields(self):

user = factories.User()

got_users = helpers.call_action('user_list', all_fields=False)

assert len(got_users) == 1
got_user = got_users[0]
assert got_user == user['name']


class TestUserShow(helpers.FunctionalTestBase):

Expand Down
49 changes: 44 additions & 5 deletions ckanext/datastore/logic/action.py
@@ -1,9 +1,10 @@
# encoding: utf-8

import logging

import json
import sqlalchemy

import ckan.lib.search as search
import ckan.lib.navl.dictization_functions
import ckan.logic as logic
import ckan.plugins as p
Expand Down Expand Up @@ -141,16 +142,54 @@ def datastore_create(context, data_dict):
try:
result = db.create(context, data_dict)
except db.InvalidDataError as err:
raise p.toolkit.ValidationError(str(err))
raise p.toolkit.ValidationError(unicode(err))

# Set the datastore_active flag on the resource if necessary
if resource.extras.get('datastore_active') is not True:
log.debug(
'Setting datastore_active=True on resource {0}'.format(resource.id)
)
p.toolkit.get_action('resource_patch')(
context,
{'id': data_dict['resource_id'], 'datastore_active': True})
# issue #3245: race condition
update_dict = {'datastore_active': True}

# get extras(for entity update) and package_id(for search index update)
res_query = model.Session.query(
model.resource_table.c.extras,
model.resource_table.c.package_id
).filter(
model.Resource.id == data_dict['resource_id']
)
extras, package_id = res_query.one()

# update extras in database for record and its revision
extras.update(update_dict)
res_query.update({'extras': extras}, synchronize_session=False)

model.Session.query(model.resource_revision_table).filter(
model.ResourceRevision.id == data_dict['resource_id'],
model.ResourceRevision.current is True
).update({'extras': extras}, synchronize_session=False)

model.Session.commit()

# get package with updated resource from solr
# find changed resource, patch it and reindex package
psi = search.PackageSearchIndex()
solr_query = search.PackageSearchQuery()
q = {
'q': 'id:"{0}"'.format(package_id),
'fl': 'data_dict',
'wt': 'json',
'fq': 'site_id:"%s"' % config.get('ckan.site_id'),
'rows': 1
}
for record in solr_query.run(q)['results']:
solr_data_dict = json.loads(record['data_dict'])
for resource in solr_data_dict['resources']:
if resource['id'] == data_dict['resource_id']:
resource.update(update_dict)
psi.index_package(solr_data_dict)
break

result.pop('id', None)
result.pop('private', None)
Expand Down

0 comments on commit e5c2ad2

Please sign in to comment.