This repository was archived by the owner on Mar 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·377 lines (304 loc) · 12.5 KB
/
main.py
File metadata and controls
executable file
·377 lines (304 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python
# Based on Google's official Python documentation
#import cgi
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app, login_required
import gdata.gauth
import gdata.docs.client
# From upload.py
import collection
import glims
glims = reload(glims)
# From http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html
# Constants included for ease of understanding. It is a more common
# and reliable practice to create a helper for reading a Consumer Key
# and Secret from a config file. You may have different consumer keys
# and secrets for different environments, and you also may not want to
# check these values into your source code repository.
SETTINGS = {
'APP_NAME': 'birgdata',
'CONSUMER_KEY': 'birgdata.appspot.com',
'CONSUMER_SECRET': 'VAeTG4YvQYlmcYS8xwK7V2d6',
'SCOPES': ['https://docs.google.com/feeds/']
}
#credfile = open(".credentials.info")
#credentials = credfile.readlines()
#credfile.close()
#if credentials.count() > 1:
# SETTINGS['CONSUMER_KEY'] = credentials[0]
# SETTINGS['CONSUMER_SECRET'] = credentials[1]
# From http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html
# Create an instance of the DocsService to make API calls
gdocs = gdata.docs.client.DocsClient(source = SETTINGS['APP_NAME'])
class NoCollectionWithIDException(Exception):
def __init__(self,value):
self.specific_msg = value
def __str__(self):
return repr(self.specific_msg)
class UserSpectraFile(db.Model):
owner = db.UserProperty(required=True)
#filename = db.StringProperty(required=True)
blob_key = db.BlobReferenceProperty(required=True)
#data = db.BlobProperty(required=True)
class CopyingHandler(webapp.RequestHandler):
def post(self):
current_user = users.get_current_user()
if current_user:
name = self.request.POST['birgun'];
password = self.request.POST['birgpass'];
cid = self.request.POST['birgcolid'];
study_name = self.request.POST['gdRootCol'];
c = collection.Collection()
r = c.get_collection(cid,name,password)
if r.status != 200:
raise NoCollectionWithIDException('Failed to get the collection from BiRG')
#email = 'glims.test@gmail.com' #raw_input('Google E-mail: ')
#password = 'birglab1' #raw_input("Password: ") #getpass.getpass('Password: ')
#study_name = 'ANIT' #raw_input("Study name: ")
# Assign the files to folders
#helper = glims.Helper(email,password)
helper = glims.Helper()
potential_study_collections = helper.get_collections(study_name)
if len(potential_study_collections) > 0:
# Assume it is the one and only return (later this will have to be dynamic)
study = glims.Study(helper,potential_study_collections[0]['entry'])
else:
study = glims.Study(helper,study_name)
study.upload_files(c)
# Report success.
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<!-- Username: %s -->
<p>A copy of collection #%d has been transferred from BiRG to Google Docs.</p>
</body>
</html>
""" % (name,cid))
else:
self.redirect('https://%s/' % self.request.host)
class DownloadHandler(webapp.RequestHandler):
@login_required
def post(self):
current_user = users.get_current_user()
if current_user:
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<a href="">Your file is ready.</a>
</body>
</html>
""")
else:
self.redirect('https://%s/' % self.request.host)
class FileSendHandler(blobstore_handlers.BlobstoreUploadHandler):
@login_required
def post(self):
current_user = users.get_current_user()
if current_user:
# -- DCW: Assume we only want the first file.
uploaded = self.get_uploads()[0]
collectionFile = UserSpectraFile(owner=users.get_current_user(),
blob_key=uploaded.key())
# Assign the files to folders
#helper = glims.Helper()
#potential_studies = helper.get_collections(study_name)
#if len(potential_studies) > 0:
# study = glims.Study(helper,potential_studies[0]['entry']) # Assume it is the one and only return (later this will have to be dynamic)
#else:
# study = glims.Study(helper,study_name)
#
#study.upload_files(c)
db.put(collectionFile)
# Report success.
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<p>Your file is uploaded.</p>
</body>
</html>
""")
else:
self.redirect('https://%s/' % self.request.host)
class FrontendHandler(webapp.RequestHandler):
@login_required
def get(self):
current_user = users.get_current_user()
if current_user:
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<form action="/upload" method="post">
<div><input type="submit" value="Upload a Collection"></div>
</form>
<form action="/download" method="post">
<div><input type="submit" value="Download a Collection"></div>
</form>
<form action="/xfer" method="post">
<div><input type="submit" value="Transfer a Collection"></div>
</form>
</body>
</html>
""")
else:
self.redirect('https://%s/' % self.request.host)
# Based on Fetcher class from
# http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html
class MainHandler(webapp.RequestHandler):
@login_required
def get(self):
"""This handler is responsible for fetching an initial OAuth
request token and redirecting the user to the approval page."""
current_user = users.get_current_user()
# We need to first get a unique token for the user to
# promote.
#
# We provide the callback URL. This is where we want the
# user to be sent after they have granted us
# access. Sometimes, developers generate different URLs
# based on the environment. You want to set this value to
# "http://localhost:8080/staging" if you are running the
# development server locally.
#
# We also provide the data scope(s). In general, we want
# to limit the scope as much as possible. For this
# example, we just ask for access to all feeds.
scopes = SETTINGS['SCOPES']
oauth_callback = 'https://%s/staging' % self.request.host
consumer_key = SETTINGS['CONSUMER_KEY']
consumer_secret = SETTINGS['CONSUMER_SECRET']
request_token = gdocs.get_oauth_token(scopes, oauth_callback,
consumer_key, consumer_secret)
# Persist this token in the datastore.
request_token_key = 'request_token_%s' % current_user.user_id()
gdata.gauth.ae_save(request_token, request_token_key)
# Generate the authorization URL.
approval_page_url = request_token.generate_authorization_url()
message = """<a href="%s">
Request token for the Google Documents Scope</a>"""
self.response.out.write(message % approval_page_url)
# Based on RequestTokenCallback class from
# http://code.google.com/appengine/articles/python/retrieving_gdata_feeds.html
class OAuthStagingHandler(webapp.RequestHandler):
@login_required
def get(self):
"""When the user grants access, they are redirected back to this
handler where their authorized request token is exchanged for a
long-lived access token."""
current_user = users.get_current_user()
# Remember the token that we stashed? Let's get it back from
# datastore now and adds information to allow it to become an
# access token.
request_token_key = 'request_token_%s' % current_user.user_id()
request_token = gdata.gauth.ae_load(request_token_key)
gdata.gauth.authorize_request_token(request_token, self.request.uri)
# We can now upgrade our authorized token to a long-lived
# access token by associating it with gdocs client, and
# calling the get_access_token method.
gdocs.auth_token = gdocs.get_access_token(request_token)
# Note that we want to keep the access token around, as it
# will be valid for all API calls in the future until a user
# revokes our access. For example, it could be populated later
# from reading from the datastore or some other persistence
# mechanism.
access_token_key = 'access_token_%s' % current_user.user_id()
gdata.gauth.ae_save(request_token, access_token_key)
# -- DCW: Redirect the user to the main menu.
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title>Google Authentication Complete</title>
<meta http-equiv="refresh" content="5;url=/mainmenu" />
</head>
<body>
<p>Authentication with Google servers was successful!<br />
<a href="/mainmenu">Click this link if you are not redirected in 5 seconds.</a>
</p>
</body>
</html>
""")
class TransferHandler(webapp.RequestHandler):
@login_required
def post(self):
current_user = users.get_current_user()
if current_user:
helper = glims.Helper()
potential_studies = helper.get_studies()
option_string = ""
for one_study_name in potential_studies:
option_string = option_string + '\n <option>'
option_string = option_string + one_study_name + '</option>'
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<p>Please enter your BiRG Metabolomics Management credentials & target collection ID.</p>
<form action="/cpdata" method="post">
<p>BiRG user name:</p>
<div><input type="text" name="birgun"></div>
<p>BiRG password:</p>
<div><input type="text" name="birgpass"></div>
<p>BiRG collection ID:</p>
<div><input type="text" name="birgcolid"></div>
<p>Google Data Study Name:</p>
<div><select name="gdRootCol" size="5">""" + option_string + """
</select></div>
<div><input type="submit" value="Initiate Transfer"></div>
</form>
</body>
</html>
""")
else:
self.redirect('https://%s/' % self.request.host)
class UploadHandler(webapp.RequestHandler):
@login_required
def post(self):
current_user = users.get_current_user()
if current_user:
upload_url = blobstore.create_upload_url('/filesent')
self.response.out.write("""<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<p>Please select the collection file to be uploaded:</p>
<form action=\"""" + upload_url + """\" method="post">
<input type="file" name="collectionFile" required="required">
<div><input type="submit" value="Initiate Transfer"></div>
</form>
</body>
</html>
""")
else:
self.redirect('https://%s/' % self.request.host)
def main():
application = webapp.WSGIApplication([('/', MainHandler),
('/staging', OAuthStagingHandler),
('/mainmenu', FrontendHandler),
('/upload', UploadHandler),
('/download', DownloadHandler),
('/xfer', TransferHandler),
('/cpdata', CopyingHandler),
('/filesent', FileSendHandler)],
debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()