annotate versioned_data_form.py @ 1:5c5027485f7d draft

Uploaded correct file
author damion
date Sun, 09 Aug 2015 16:07:50 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
1 #!/usr/bin/python
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
2 #
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
3 import os
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
4 import sys
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
5
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
6 # Extra step enables this script to locate vdb_common and vdb_retrieval
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
7 # From http://code.activestate.com/recipes/66062-determining-current-function-name/
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
8 _self_dir = os.path.dirname(sys._getframe().f_code.co_filename)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
9 sys.path.append(_self_dir)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
10
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
11 import vdb_common
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
12 import vdb_retrieval
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
13
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
14 retrieval_obj = None # Global used here to manage user's currently selected retrieval info.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
15
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
16
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
17 def vdb_init_tool_user(trans):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
18 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
19 Retrieves a user's api key if they have one, otherwise lets them know they need one
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
20 This function is automatically called from versioned_data.xml form on presentation to user
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
21 Note that this is how self.api_url gets back into form, for passage back to 2nd call via versioned_data.py
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
22 self.api_key is passed via secure <configfile> construct.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
23 ALSO: squeezing history_id in this way since no other way to pass it.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
24 "trans" is provided only by tool form presentation via <code file="...">
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
25 BUT NOW SEE John Chilton's: https://gist.github.com/jmchilton/27c5bb05e155a611294d
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
26 See galaxy source code at https://galaxy-dist.readthedocs.org/en/latest/_modules/galaxy/web/framework.html,
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
27 See http://dev.list.galaxyproject.org/error-using-get-user-id-in-xml-file-in-new-Galaxy-td4665274.html
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
28 See http://dev.list.galaxyproject.org/hg-galaxy-2780-Real-Job-tm-support-for-the-library-upload-to-td4133384.html
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
29 master api key, set in galaxy config: #self.master_api_key = trans.app.config.master_api_key
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
30 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
31 global retrieval_obj
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
32
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
33 api_url = trans.request.application_url + '/api'
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
34 history_id = str(trans.security.encode_id(trans.history.id))
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
35 user_api_key = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
36 #master_api_key = trans.app.config.master_api_key
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
37
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
38 if trans.user:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
39
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
40 user_name = trans.user.username
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
41
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
42 if trans.user.api_keys and len(trans.user.api_keys) > 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
43 user_api_key = trans.user.api_keys[0].key #First key is always the active one?
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
44 items = [ { 'name': user_name, 'value': api_url + '-' + history_id, 'options':[], 'selected': True } ]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
45
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
46 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
47 items = [ { 'name': user_name + ' - Note: you need a key (see "User" menu)!', 'value': '0', 'options':[], 'selected': False } ]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
48
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
49 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
50 items = [ { 'name': 'You need to be logged in to use this tool!', 'value': '1', 'options':[], 'selected': False } ]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
51
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
52 retrieval_obj = vdb_retrieval.VDBRetrieval()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
53 retrieval_obj.set_trans(api_url, history_id, user_api_key) #, master_api_key
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
54
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
55 return items
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
56
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
57
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
58 def vdb_get_databases():
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
59 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
60 Called by Tool Form, retrieves list of versioned databases from galaxy library called "Versioned Data"
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
61
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
62 @return [name,value,selected] array
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
63 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
64 global retrieval_obj
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
65 items = retrieval_obj.get_library_data_store_list()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
66
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
67 if len(items) == 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
68 # Not great: Communicating library problem by text in form select pulldown input.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
69 items = [[vdb_retrieval.VDB_DATA_LIBRARY_FOLDER_ERROR, None, False]]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
70
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
71 return items
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
72
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
73
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
74 def vdb_get_versions(spec_file_id, global_retrieval_date):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
75 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
76 Retrieve applicable versions of given database.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
77 Unfortunately this is only refreshed when form screen is refreshed
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
78
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
79 @param dbKey [folder_id]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
80
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
81 @return [name,value,selected] array
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
82 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
83 global retrieval_obj
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
84
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
85 items = []
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
86 if spec_file_id:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
87
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
88 data_store_spec = retrieval_obj.user_api.libraries.show_folder(retrieval_obj.library_id, spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
89
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
90 if data_store_spec: #OTHERWISE DOES THIS MEAN USER DOESN'T HAVE PERMISSIONS? VALIDATE
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
91
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
92 file_name = data_store_spec['name'] # Short (no path), original file name, not galaxy-assigned file_name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
93 data_store_type = retrieval_obj.test_data_store_type(file_name)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
94 library_label_path = retrieval_obj.get_library_label_path(spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
95
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
96 if not data_store_type or not library_label_path:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
97 # Cludgy method of sending message to user
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
98 items.append([vdb_retrieval.VDB_DATA_LIBRARY_FOLDER_ERROR, None, False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
99 items.append([vdb_retrieval.VDB_DATA_LIBRARY_CONFIG_ERROR + '"' + vdb_retrieval.VDB_DATA_LIBRARY + '/' + str(library_label_path) + '/' + file_name + '"', None, False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
100 return items
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
101
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
102 _retrieval_date = vdb_common.parse_date(global_retrieval_date)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
103
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
104 # Loads interface for appropriate data source retrieval
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
105 ds_obj = retrieval_obj.get_data_store_gateway(data_store_type, spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
106 items = ds_obj.get_version_options(global_retrieval_date=_retrieval_date)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
107
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
108 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
109 items.append(['Unable to find' + spec_file_id + ':Check permissions?','',False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
110
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
111 return items
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
112
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
113
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
114 def vdb_get_workflows(dbKey):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
115 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
116 List appropriate workflows for database. These are indicated by prefix "Versioning:" in name.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
117 Currently can see ALL workflows that are published; admin_api() receives this in all galaxy versions.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
118 Seems like only some galaxy versions allow user_api() to also see published workflows.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
119 Only alternative is to list only individual workflows that current user can see - ones they created, and published workflows; but versioneddata user needs to have permissions on these too.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
120
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
121 Future: Sensitivity: Some workflows apply only to some kinds of database
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
122
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
123 @param dbKey [data_spec_id]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
124 @return [name,value,selected] array
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
125 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
126 global retrieval_obj
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
127
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
128 data = []
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
129 try:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
130 workflows = retrieval_obj.admin_api.workflows.get_workflows(published=True)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
131
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
132 except Exception as err:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
133 if err.message[-21:] == 'HTTP status code: 403':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
134 data.append(['Error: User does not have permissions to see workflows (or they need to be published).' , 0, False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
135 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
136 data.append(['Error: In getting workflows: %s' % err.message , 0, False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
137 return data
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
138
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
139 oldName = ""
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
140 for workflow in workflows:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
141 name = workflow['name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
142 if name[0:11].lower() == "versioning:" and name != oldName:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
143 # Interface Bug: If an item is published and is also shared personally with a user, it is shown twice.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
144 data.append([name, workflow['id'], False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
145 oldName = name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
146
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
147 return data
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
148
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
149