Skip to content

Commit

Permalink
[#3344] handle offset, limit; better toolkit imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Dec 16, 2016
1 parent e29e8cd commit cb020f8
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions ckanext/datastore/controller.py
Expand Up @@ -5,52 +5,65 @@

import pylons

import ckan.plugins as p
import ckan.lib.base as base
import ckan.model as model

from ckan.common import request
from ckan.plugins.toolkit import (
Invalid,
ObjectNotFound,
get_action,
get_validator,
_,
request,
response,
BaseController,
abort,
)

int_validator = get_validator('int_validator')

PAGINATE_BY = 10000


class DatastoreController(base.BaseController):
class DatastoreController(BaseController):
def dump(self, resource_id):
context = {
'model': model,
'session': model.Session,
'user': p.toolkit.c.user
}
try:
offset = int_validator(request.GET.get('offset', 0), {})
except Invalid as e:
abort(400, u'offset: ' + e.error)
try:
limit = int_validator(request.GET.get('limit'), {})
except Invalid as e:
abort(400, u'limit: ' + e.error)

offset = 0
wr = None
while True:
data_dict = {
'resource_id': resource_id,
'limit': request.GET.get('limit', PAGINATE_BY),
'offset': request.GET.get('offset', offset)
}
if limit is not None and limit <= 0:
break

action = p.toolkit.get_action('datastore_search')
try:
result = action(context, data_dict)
except p.toolkit.ObjectNotFound:
base.abort(404, p.toolkit._('DataStore resource not found'))
result = get_action('datastore_search')(None, {
'resource_id': resource_id,
'limit':
PAGINATE_BY if limit is None
else min(PAGINATE_BY, limit),
'offset': offset,
})
except ObjectNotFound:
abort(404, _('DataStore resource not found'))

if not wr:
pylons.response.headers['Content-Type'] = 'text/csv'
pylons.response.headers['Content-disposition'] = (
response.headers['Content-Type'] = 'text/csv; charset=utf-8'
response.headers['Content-disposition'] = (
'attachment; filename="{name}.csv"'.format(
name=resource_id))
wr = csv.writer(pylons.response, encoding='utf-8')
wr = csv.writer(response, encoding='utf-8')

header = [x['id'] for x in result['fields']]
wr.writerow(header)

for record in result['records']:
wr.writerow([record[column] for column in header])

offset += PAGINATE_BY
if len(result['records']) < PAGINATE_BY:
break
offset += PAGINATE_BY
if limit is not None:
limit -= PAGINATE_BY

0 comments on commit cb020f8

Please sign in to comment.