Skip to content

Commit

Permalink
Move sharing status api endpoint to mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed May 3, 2018
1 parent 17772d1 commit 3507636
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
40 changes: 40 additions & 0 deletions lib/galaxy/web/base/controller.py
Expand Up @@ -1413,6 +1413,46 @@ def create_item_slug(self, sa_session, item):
item.slug = new_slug
return item.slug == cur_slug

def set_sharing_status(self, trans, item, payload):
payload = payload or {}
action = payload.get("action")
class_name = item.__class__.__name__
if not action:
raise exceptions.MessageException("Specify a sharing action.")
if action == "make_accessible_via_link":
self._make_item_accessible(trans.sa_session, item)
elif action == "make_accessible_and_publish":
self._make_item_accessible(trans.sa_session, item)
item.published = True
elif action == "publish":
if item.importable:
item.published = True
else:
raise exceptions.MessageException("%s not importable." % class_name)
elif action == "disable_link_access":
item.importable = False
elif action == "unpublish":
item.published = False
elif action == "disable_link_access_and_unpublish":
item.importable = item.published = False
elif action == "unshare_user":
user = trans.sa_session.query(trans.app.model.User).get(self.decode_id(payload.get("user_id")))
class_name_lc = class_name.lower()
ShareAssociation = getattr(trans.app.model, "%sUserShareAssociation" % class_name)
usas = trans.sa_session.query(ShareAssociation).filter_by(**{"user": user, class_name_lc: item}).all()
if not usas:
raise exceptions.MessageException("%s was not shared with user." % class_name)
for usa in usas:
trans.sa_session.delete(usa)
if item.importable and not item.slug:
self._make_item_accessible(trans.sa_session, item)
trans.sa_session.add(item)
trans.sa_session.flush()
return {"message": "Sharing status updated.", "item": {
"published": item.published,
"importable": item.importable,
"users_shared_with": [{"id": trans.app.security.encode_id(a.user.id), "email": a.user.email} for a in item.users_shared_with]
}}
# -- Abstract methods. --

@web.expose
Expand Down
37 changes: 1 addition & 36 deletions lib/galaxy/webapps/galaxy/api/histories.py
Expand Up @@ -537,42 +537,7 @@ def sharing(self, trans, history_id, payload=None, **kwd):
Set sharing details of a given history.
"""
history = self._get_history(trans, history_id)
payload = payload or {}
action = payload.get("action")
if not action:
raise exceptions.MessageException("Specify a sharing action.")
if action == "make_accessible_via_link":
self._make_item_accessible(trans.sa_session, history)
elif action == "make_accessible_and_publish":
self._make_item_accessible(trans.sa_session, history)
history.published = True
elif action == "publish":
if history.importable:
history.published = True
else:
raise exceptions.MessageException("Item not importable.")
elif action == "disable_link_access":
history.importable = False
elif action == "unpublish":
history.published = False
elif action == "disable_link_access_and_unpublish":
history.importable = history.published = False
elif action == "unshare_user":
user = trans.sa_session.query(trans.app.model.User).get(self.decode_id(payload.get("user_id")))
husas = trans.sa_session.query(trans.app.model.HistoryUserShareAssociation).filter_by(user=user, history=history).all()
if not husas:
raise exceptions.MessageException("History was not shared with user.")
for husa in husas:
trans.sa_session.delete(husa)
if history.importable and not history.slug:
self._make_item_accessible(trans.sa_session, history)
trans.sa_session.add(history)
trans.sa_session.flush()
return {"message": "Sharing status updated.", "item": {
"published": history.published,
"importable": history.importable,
"users_shared_with": [{"id": trans.app.security.encode_id(a.user.id), "email": a.user.email} for a in history.users_shared_with]
}}
return self.set_sharing_status(trans, history, payload)

def _get_history(self, trans, id):
history = self.history_manager.get_accessible(self.decode_id(id), trans.user, current_history=trans.history)
Expand Down

0 comments on commit 3507636

Please sign in to comment.