Skip to content

Commit

Permalink
internally api_version now int not str
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Mar 6, 2012
1 parent 5090625 commit f7b3669
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 40 deletions.
15 changes: 6 additions & 9 deletions ckan/controllers/api.py
Expand Up @@ -38,7 +38,7 @@ def __call__(self, environ, start_response):
api_version = routes_dict.get('ver')
if api_version:
api_version = api_version[1:]
routes_dict['ver'] = api_version
routes_dict['ver'] = int(api_version)

self._identify_user()
try:
Expand Down Expand Up @@ -131,20 +131,18 @@ def _set_response_header(self, name, value):

def get_api(self, ver=None):
response_data = {}
response_data['version'] = ver or '1'
response_data['version'] = ver
return self._finish_ok(response_data)

def action(self, logic_function):
# FIXME this is a hack till ver gets passed
api_version = 3
def action(self, logic_function, ver=None):
function = get_action(logic_function)
if not function:
log.error('Can\'t find logic function: %s' % logic_function)
return self._finish_bad_request(
gettext('Action name not known: %s') % str(logic_function))

context = {'model': model, 'session': model.Session, 'user': c.user,
'api_version':api_version}
'api_version':ver}
model.Session()._context = context
return_dict = {'help': function.__doc__}
try:
Expand Down Expand Up @@ -419,7 +417,6 @@ def delete(self, ver=None, register=None, subregister=None, id=None, id2=None):
def search(self, ver=None, register=None):

log.debug('search %s params: %r' % (register, request.params))
ver = ver or '1' # i.e. default to v1
if register == 'revision':
since_time = None
if request.params.has_key('since_id'):
Expand Down Expand Up @@ -453,7 +450,7 @@ def search(self, ver=None, register=None):
# if using API v2, default to returning the package ID if
# no field list is specified
if register in ['dataset', 'package'] and not params.get('fl'):
params['fl'] = 'id' if ver == '2' else 'name'
params['fl'] = 'id' if ver == 2 else 'name'

try:
if register == 'resource':
Expand Down Expand Up @@ -486,7 +483,7 @@ def search(self, ver=None, register=None):
else:
# For package searches in API v3 and higher, we can pass
# parameters straight to Solr.
if ver in u'12':
if ver in [1, 2]:
# Otherwise, put all unrecognised ones into the q parameter
params = convert_legacy_parameters_to_solr(params)
query = query_for(model.Package)
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/dictization/model_dictize.py
Expand Up @@ -465,5 +465,5 @@ def tag_to_api1(tag, context):

def tag_to_api2(tag, context):
# DEPRICIATED set api_version in context and use tag_to_api()
context['api_version'] = 1
context['api_version'] = 2
return tag_to_api(tag, context)
6 changes: 4 additions & 2 deletions ckan/lib/dictization/model_save.py
Expand Up @@ -392,7 +392,9 @@ def user_dict_save(user_dict, context):
def package_api_to_dict(api1_dict, context):

package = context.get("package")
api_version = context.get('api_version') or '1'
api_version = context.get('api_version')
assert api_version, 'No api_version supplied in context'

dictized = {}

for key, value in api1_dict.iteritems():
Expand All @@ -418,7 +420,7 @@ def package_api_to_dict(api1_dict, context):
new_value.append({"key": extras_key,
"value": None})
if key == 'groups' and len(value):
if api_version == '1':
if api_version == 1:
new_value = [{'name': item} for item in value]
else:
new_value = [{'id': item} for item in value]
Expand Down
6 changes: 3 additions & 3 deletions ckan/lib/search/__init__.py
Expand Up @@ -100,7 +100,7 @@ def notify(self, entity, operation):
dispatch_by_operation(
entity.__class__.__name__,
get_action('package_show_rest')(
{'model': model, 'ignore_auth': True},
{'model': model, 'ignore_auth': True, 'api_version':1},
{'id': entity.id}
),
operation
Expand All @@ -119,7 +119,7 @@ def rebuild(package=None):

if package:
pkg_dict = get_action('package_show_rest')(
{'model': model, 'ignore_auth': True},
{'model': model, 'ignore_auth': True, 'api_version':1},
{'id': package}
)
package_index.remove_dict(pkg_dict)
Expand All @@ -130,7 +130,7 @@ def rebuild(package=None):
for pkg in model.Session.query(model.Package).filter(model.Package.state == 'active').all():
package_index.insert_dict(
get_action('package_show_rest')(
{'model': model, 'ignore_auth': True},
{'model': model, 'ignore_auth': True, 'api_version':1},
{'id': pkg.id}
)
)
Expand Down
4 changes: 2 additions & 2 deletions ckan/logic/action/create.py
Expand Up @@ -116,8 +116,8 @@ def package_relationship_create(context, data_dict):
model = context['model']
user = context['user']
schema = context.get('schema') or ckan.logic.schema.default_create_relationship_schema()
api = context.get('api_version') or '1'
ref_package_by = 'id' if api == '2' else 'name'
api = context.get('api_version')
ref_package_by = 'id' if api == 2 else 'name'

id = data_dict['subject']
id2 = data_dict['object']
Expand Down
19 changes: 8 additions & 11 deletions ckan/logic/action/get.py
Expand Up @@ -46,8 +46,8 @@ def package_list(context, data_dict):

model = context["model"]
user = context["user"]
api = context.get("api_version", '1')
ref_package_by = 'id' if api == '2' else 'name'
api = context.get("api_version", 1)
ref_package_by = 'id' if api == 2 else 'name'

check_access('package_list', context, data_dict)

Expand Down Expand Up @@ -105,8 +105,8 @@ def group_list(context, data_dict):

model = context['model']
user = context['user']
api = context.get('api_version') or '1'
ref_group_by = 'id' if api == '2' else 'name';
api = context.get('api_version')
ref_group_by = 'id' if api == 2 else 'name';
order_by = data_dict.get('order_by', 'name')
if order_by not in set(('name', 'packages')):
raise logic.ParameterError('"order_by" value %r not implemented.' % order_by)
Expand Down Expand Up @@ -286,12 +286,12 @@ def package_relationships_list(context, data_dict):
##TODO needs to work with dictization layer
model = context['model']
user = context['user']
api = context.get('api_version') or '1'
api = context.get('api_version')

id = data_dict["id"]
id2 = data_dict.get("id2")
rel = data_dict.get("rel")
ref_package_by = 'id' if api == '2' else 'name';
ref_package_by = 'id' if api == 2 else 'name';
pkg1 = model.Package.get(id)
pkg2 = None
if not pkg1:
Expand Down Expand Up @@ -343,7 +343,6 @@ def package_show(context, data_dict):

def resource_show(context, data_dict):
model = context['model']
api = context.get('api_version') or '1'
id = data_dict['id']

resource = model.Resource.get(id)
Expand All @@ -358,9 +357,9 @@ def resource_show(context, data_dict):

def revision_show(context, data_dict):
model = context['model']
api = context.get('api_version') or '1'
api = context.get('api_version')
id = data_dict['id']
ref_package_by = 'id' if api == '2' else 'name'
ref_package_by = 'id' if api == 2 else 'name'

rev = model.Session.query(model.Revision).get(id)
if rev is None:
Expand All @@ -374,8 +373,6 @@ def group_show(context, data_dict):

model = context['model']
id = data_dict['id']
api = context.get('api_version') or '1'


group = model.Group.get(id)
context['group'] = group
Expand Down
6 changes: 2 additions & 4 deletions ckan/logic/action/update.py
Expand Up @@ -223,8 +223,8 @@ def package_update_validate(context, data_dict):

def _update_package_relationship(relationship, comment, context):
model = context['model']
api = context.get('api_version') or '1'
ref_package_by = 'id' if api == '2' else 'name'
api = context.get('api_version')
ref_package_by = 'id' if api == 2 else 'name'
is_changed = relationship.comment != comment
if is_changed:
rev = model.repo.new_revision()
Expand All @@ -243,12 +243,10 @@ def package_relationship_update(context, data_dict):
model = context['model']
user = context['user']
schema = context.get('schema') or ckan.logic.schema.default_update_relationship_schema()
api = context.get('api_version') or '1'

id = data_dict['subject']
id2 = data_dict['object']
rel = data_dict['type']
ref_package_by = 'id' if api == '2' else 'name'

pkg1 = model.Package.get(id)
pkg2 = model.Package.get(id2)
Expand Down
10 changes: 5 additions & 5 deletions ckan/tests/functional/api/base.py
Expand Up @@ -81,7 +81,7 @@ def offset(self, path):
assert self.api_version != None, "API version is missing."
base = '/api'
if self.api_version:
base += '/' + self.api_version
base += '/%s' % self.api_version
utf8_encoded = (u'%s%s' % (base, path)).encode('utf8')
url_encoded = urllib.quote(utf8_encoded)
return url_encoded
Expand Down Expand Up @@ -281,7 +281,7 @@ def _ref_group(cls, group):

class Api1TestCase(Api1and2TestCase):

api_version = '1'
api_version = 1
ref_package_by = 'name'
ref_group_by = 'name'
ref_tag_by = 'name'
Expand All @@ -293,7 +293,7 @@ def assert_msg_represents_anna(self, msg):

class Api2TestCase(Api1and2TestCase):

api_version = '2'
api_version = 2
ref_package_by = 'id'
ref_group_by = 'id'
ref_tag_by = 'id'
Expand All @@ -305,7 +305,7 @@ def assert_msg_represents_anna(self, msg):

class Api3TestCase(ApiTestCase):

api_version = '3'
api_version = 3
ref_package_by = 'name'
ref_group_by = 'name'
ref_tag_by = 'name'
Expand All @@ -317,7 +317,7 @@ def assert_msg_represents_anna(self, msg):
class ApiUnversionedTestCase(Api1TestCase):

api_version = ''
oldest_api_version = '1'
oldest_api_version = 1

def get_expected_api_version(self):
return self.oldest_api_version
Expand Down
6 changes: 3 additions & 3 deletions ckan/tests/functional/api/model/test_package.py
Expand Up @@ -34,7 +34,7 @@ def get_groups_identifiers(self, test_groups, users=[]):
groups = []
for grp in test_groups:
group = model.Group.get(grp)
if self.get_expected_api_version() == '1':
if self.get_expected_api_version() == 1:
groups.append(group.name)
else:
groups.append(group.id)
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_register_post_with_group(self):
pkg_groups = model.Session.query(model.Group).\
join(model.Member, model.Member.group_id == model.Group.id).\
filter(model.Member.table_id == package.id).all()
if self.get_expected_api_version() == '1':
if self.get_expected_api_version() == 1:
self.assert_equal([g.name for g in pkg_groups], groups)
else:
self.assert_equal([g.id for g in pkg_groups], groups)
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_register_post_with_group_sysadmin(self):
pkg_groups = model.Session.query(model.Group).\
join(model.Member, model.Member.group_id == model.Group.id).\
filter(model.Member.table_id == package.id).all()
if self.get_expected_api_version() == '1':
if self.get_expected_api_version() == 1:
self.assert_equal([g.name for g in pkg_groups], groups)
else:
self.assert_equal([g.id for g in pkg_groups], groups)
Expand Down
1 change: 1 addition & 0 deletions ckan/tests/lib/test_dictization.py
Expand Up @@ -755,6 +755,7 @@ def test_14_resource_no_id(self):
def test_15_api_to_dictize(self):

context = {"model": model,
'api_version': 1,
"session": model.Session}

api_data = {
Expand Down

0 comments on commit f7b3669

Please sign in to comment.