Skip to content

Commit

Permalink
[#1725] Rename all_field_ids -> column_names
Browse files Browse the repository at this point in the history
In my opinion, it's a better name.
  • Loading branch information
vitorbaptista committed Jun 24, 2014
1 parent 6428376 commit 06dcb7a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
12 changes: 6 additions & 6 deletions ckanext/datastore/db.py
Expand Up @@ -801,8 +801,8 @@ def delete_data(context, data_dict):

def validate(context, data_dict):
all_fields = _get_fields(context, data_dict)
all_field_ids = _pluck('id', all_fields)
all_field_ids.insert(0, '_id')
column_names = _pluck('id', all_fields)
column_names.insert(0, '_id')
data_dict_copy = copy.deepcopy(data_dict)

# TODO: Convert all attributes that can be a comma-separated string to
Expand All @@ -817,7 +817,7 @@ def validate(context, data_dict):
for plugin in p.PluginImplementations(interfaces.IDatastore):
data_dict_copy = plugin.datastore_validate(context,
data_dict_copy,
all_field_ids)
column_names)

# Remove default elements in data_dict
del data_dict_copy['connection_url']
Expand Down Expand Up @@ -846,8 +846,8 @@ def validate(context, data_dict):
def search_data(context, data_dict):
validate(context, data_dict)
all_fields = _get_fields(context, data_dict)
all_field_ids = _pluck('id', all_fields)
all_field_ids.insert(0, '_id')
column_names = _pluck('id', all_fields)
column_names.insert(0, '_id')

query_dict = {
'select': [],
Expand All @@ -857,7 +857,7 @@ def search_data(context, data_dict):

for plugin in p.PluginImplementations(interfaces.IDatastore):
query_dict = plugin.datastore_search(context, data_dict,
all_field_ids, query_dict)
column_names, query_dict)

where_clause, where_values = _where(query_dict['where'])

Expand Down
18 changes: 9 additions & 9 deletions ckanext/datastore/interfaces.py
Expand Up @@ -4,7 +4,7 @@
class IDatastore(interfaces.Interface):
'''Allow modifying Datastore queries'''

def datastore_validate(self, context, data_dict, all_field_ids):
def datastore_validate(self, context, data_dict, column_names):
'''Validates the ``data_dict`` sent by the user
This is the first method that's called. It's used to guarantee that
Expand All @@ -29,12 +29,12 @@ def datastore_validate(self, context, data_dict, all_field_ids):
:type context: dictionary
:param data_dict: the parameters received from the user
:type data_dict: dictionary
:param all_field_ids: the current resource's column names
:type all_field_ids: list
:param column_names: the current resource's column names
:type column_names: list
'''
return data_dict

def datastore_search(self, context, data_dict, all_field_ids, query_dict):
def datastore_search(self, context, data_dict, column_names, query_dict):
'''Modify queries made on datastore_search
The overall design is that every IDatastore extension will receive the
Expand Down Expand Up @@ -79,8 +79,8 @@ def datastore_search(self, context, data_dict, all_field_ids, query_dict):
:type context: dictionary
:param data_dict: the parameters received from the user
:type data_dict: dictionary
:param all_field_ids: the current resource's column names
:type all_field_ids: list
:param column_names: the current resource's column names
:type column_names: list
:param query_dict: the current query_dict, as changed by the IDatastore
extensions that ran before yours
:type query_dict: dictionary
Expand All @@ -90,7 +90,7 @@ def datastore_search(self, context, data_dict, all_field_ids, query_dict):
'''
return query_dict

def datastore_delete(self, context, data_dict, all_field_ids, query_dict):
def datastore_delete(self, context, data_dict, column_names, query_dict):
'''Modify queries made on datastore_delete
The overall design is that every IDatastore extension will receive the
Expand Down Expand Up @@ -130,8 +130,8 @@ def datastore_delete(self, context, data_dict, all_field_ids, query_dict):
:type context: dictionary
:param data_dict: the parameters received from the user
:type data_dict: dictionary
:param all_field_ids: the current resource's column names
:type all_field_ids: list
:param column_names: the current resource's column names
:type column_names: list
:param query_dict: the current query_dict, as changed by the IDatastore
extensions that ran before yours
:type query_dict: dictionary
Expand Down
24 changes: 12 additions & 12 deletions ckanext/datastore/plugin.py
Expand Up @@ -271,14 +271,14 @@ def before_show(self, resource_dict):
connection.close()
return resource_dict

def datastore_validate(self, context, data_dict, all_field_ids):
def datastore_validate(self, context, data_dict, column_names):
fields = data_dict.get('fields')
if fields:
data_dict['fields'] = list(set(fields) - set(all_field_ids))
data_dict['fields'] = list(set(fields) - set(column_names))

filters = data_dict.get('filters', {})
for key in filters.keys():
if key in all_field_ids:
if key in column_names:
del filters[key]

q = data_dict.get('q')
Expand All @@ -299,7 +299,7 @@ def datastore_validate(self, context, data_dict, all_field_ids):
sort_clauses = data_dict.get('sort')
if sort_clauses:
invalid_clauses = [c for c in sort_clauses
if not self._is_valid_sort(c, all_field_ids)]
if not self._is_valid_sort(c, column_names)]
data_dict['sort'] = invalid_clauses

limit = data_dict.get('limit')
Expand All @@ -319,7 +319,7 @@ def datastore_validate(self, context, data_dict, all_field_ids):

return data_dict

def _is_valid_sort(self, clause, all_field_ids):
def _is_valid_sort(self, clause, column_names):
clause = clause.encode('utf-8')
clause_parts = shlex.split(clause)

Expand All @@ -332,24 +332,24 @@ def _is_valid_sort(self, clause, all_field_ids):

field, sort = unicode(field, 'utf-8'), unicode(sort, 'utf-8')

if field not in all_field_ids:
if field not in column_names:
return False
if sort.lower() not in ('asc', 'desc'):
return False

return True

def datastore_delete(self, context, data_dict, all_field_ids, query_dict):
query_dict['where'] += self._where(data_dict, all_field_ids)
def datastore_delete(self, context, data_dict, column_names, query_dict):
query_dict['where'] += self._where(data_dict, column_names)
return query_dict

def datastore_search(self, context, data_dict, all_field_ids, query_dict):
def datastore_search(self, context, data_dict, column_names, query_dict):
fields = data_dict.get('fields')

if fields:
field_ids = datastore_helpers.get_list(fields)
else:
field_ids = all_field_ids
field_ids = column_names

ts_query, rank_column = self._textsearch_query(data_dict)
limit = data_dict.get('limit', 100)
Expand All @@ -370,11 +370,11 @@ def datastore_search(self, context, data_dict, all_field_ids, query_dict):

return query_dict

def _where(self, data_dict, all_field_ids):
def _where(self, data_dict, column_names):
filters = data_dict.get('filters', {})
clauses = []
for field, value in filters.iteritems():
if field not in all_field_ids:
if field not in column_names:
continue
clause = (u'"{0}" = %s'.format(field), value)
clauses.append(clause)
Expand Down
6 changes: 3 additions & 3 deletions ckanext/datastore/tests/sample_datastore_plugin.py
Expand Up @@ -6,7 +6,7 @@
class SampleDataStorePlugin(p.SingletonPlugin):
p.implements(interfaces.IDatastore, inherit=True)

def datastore_validate(self, context, data_dict, all_field_ids):
def datastore_validate(self, context, data_dict, column_names):
valid_filters = ('age_between', 'age_not_between', 'insecure_filter')
filters = data_dict.get('filters', {})
for key in filters.keys():
Expand All @@ -15,11 +15,11 @@ def datastore_validate(self, context, data_dict, all_field_ids):

return data_dict

def datastore_search(self, context, data_dict, all_field_ids, query_dict):
def datastore_search(self, context, data_dict, column_names, query_dict):
query_dict['where'] += self._where(data_dict)
return query_dict

def datastore_delete(self, context, data_dict, all_field_ids, query_dict):
def datastore_delete(self, context, data_dict, column_names, query_dict):
query_dict['where'] += self._where(data_dict)
return query_dict

Expand Down

0 comments on commit 06dcb7a

Please sign in to comment.