-
Notifications
You must be signed in to change notification settings - Fork 2k
/
action.py
119 lines (91 loc) · 3.67 KB
/
action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import logging
import pylons
import ckan.logic as logic
import ckan.plugins as p
import ckanext.datastore.db as db
log = logging.getLogger(__name__)
_get_or_bust = logic.get_or_bust
def datastore_create(context, data_dict):
'''Adds a new table to the datastore.
:param resource_id: resource id that the data is going to be stored under.
:type resource_id: string
:param fields: fields/columns and their extra metadata.
:type fields: list of dictionaries
:param records: the data, eg: [{"dob": "2005", "some_stuff": ['a', b']}]
:type records: list of dictionaries
:returns: the newly created data object.
:rtype: dictionary
'''
model = _get_or_bust(context, 'model')
id = _get_or_bust(data_dict, 'resource_id')
if not model.Resource.get(id):
raise p.toolkit.ObjectNotFound(p.toolkit._(
'Resource "{}" was not found.'.format(id)
))
p.toolkit.check_access('datastore_create', context, data_dict)
data_dict['connection_url'] = pylons.config['ckan.datastore_write_url']
result = db.create(context, data_dict)
result.pop('id')
result.pop('connection_url')
return result
def datastore_delete(context, data_dict):
'''Deletes a table from the datastore.
:param resource_id: resource id that the data will be deleted from.
:type resource_id: string
:param filter: filter to do deleting on over (eg {'name': 'fred'}).
If missing delete whole table.
:returns: original filters sent.
:rtype: dictionary
'''
model = _get_or_bust(context, 'model')
id = _get_or_bust(data_dict, 'resource_id')
if not model.Resource.get(id):
raise p.toolkit.ObjectNotFound(p.toolkit._(
'Resource "{}" was not found.'.format(id)
))
p.toolkit.check_access('datastore_delete', context, data_dict)
data_dict['connection_url'] = pylons.config['ckan.datastore_write_url']
result = db.delete(context, data_dict)
result.pop('id')
result.pop('connection_url')
return result
def datastore_search(context, data_dict):
'''Search a datastore table.
:param resource_id: id of the data that is going to be selected.
:type resource_id: string
:param filters: matching conditions to select.
:type filters: dictionary
:param q: full text query
:type q: string
:param limit: maximum number of rows to return (default: 100)
:type limit: int
:param offset: offset the number of rows
:type offset: int
:param fields: ordered list of fields to return
(default: all fields in original order)
:type fields: list of dictionaries
:param sort: comma separated field names with ordering
eg: "fieldname1, fieldname2 desc"
:type sort: string
:returns: a dictionary containing the search parameters and the
search results.
keys: fields: same as datastore_create accepts
offset: query offset value
limit: query limit value
filters: query filters
total: number of total matching records
records: list of matching results
:rtype: dictionary
'''
model = _get_or_bust(context, 'model')
id = _get_or_bust(data_dict, 'resource_id')
if not model.Resource.get(id):
raise p.toolkit.ObjectNotFound(p.toolkit._(
'Resource "{}" was not found.'.format(id)
))
p.toolkit.check_access('datastore_search', context, data_dict)
data_dict['connection_url'] = pylons.config['ckan.datastore_write_url']
result = db.search(context, data_dict)
result.pop('id', None)
result.pop('connection_url')
return result