Skip to content

Commit

Permalink
package_show: FOR UPDATE context parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Jun 4, 2019
1 parent 2b590f1 commit 0fa66b2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 3 additions & 1 deletion ckan/logic/action/get.py
Expand Up @@ -989,7 +989,9 @@ def package_show(context, data_dict):
context['session'] = model.Session
name_or_id = data_dict.get("id") or _get_or_bust(data_dict, 'name_or_id')

pkg = model.Package.get(name_or_id)
pkg = model.Package.get(
name_or_id,
for_update=context.get('for_update', False))

if pkg is None:
raise NotFound
Expand Down
8 changes: 5 additions & 3 deletions ckan/logic/action/update.py
Expand Up @@ -409,8 +409,11 @@ def package_sfu(context, data_dict):
if name_or_id is None:
raise ValidationError({'select__id': _('Missing value')})

package_show_context = dict(context, return_type='dict')
orig = _get_action('package_show')( # FIXME: for_update=True
package_show_context = dict(
context,
return_type='dict',
for_update=True)
orig = _get_action('package_show')(
package_show_context,
{'id': name_or_id})

Expand Down Expand Up @@ -443,7 +446,6 @@ def package_sfu(context, data_dict):
for k, v in sorted(data['update__'].items()):
dfunc.update_merge_string_key(orig, k, v)


# immutable fields
orig['id'] = pkg.id
orig['type'] = pkg.type
Expand Down
10 changes: 6 additions & 4 deletions ckan/model/domain_object.py
Expand Up @@ -41,10 +41,12 @@ def count(cls):
return cls.Session.query(cls).count()

@classmethod
def by_name(cls, name, autoflush=True):
obj = meta.Session.query(cls).autoflush(autoflush)\
.filter_by(name=name).first()
return obj
def by_name(cls, name, autoflush=True, for_update=False):
q = meta.Session.query(cls).autoflush(autoflush
).filter_by(name=name)
if for_update:
q = q.with_for_update()
return q.first()

@classmethod
def text_search(cls, query, term):
Expand Down
9 changes: 6 additions & 3 deletions ckan/model/package.py
Expand Up @@ -77,14 +77,17 @@ def search_by_name(cls, text_query):
return meta.Session.query(cls).filter(cls.name.contains(text_query.lower()))

@classmethod
def get(cls, reference):
def get(cls, reference, for_update=False):
'''Returns a package object referenced by its id or name.'''
if not reference:
return None

pkg = meta.Session.query(cls).get(reference)
q = meta.Session.query(cls)
if for_update:
q = q.with_for_update()
pkg = q.get(reference)
if pkg == None:
pkg = cls.by_name(reference)
pkg = cls.by_name(reference, for_update=for_update)
return pkg
# Todo: Make sure package names can't be changed to look like package IDs?

Expand Down

0 comments on commit 0fa66b2

Please sign in to comment.