-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
76 lines (64 loc) · 2.59 KB
/
upload.py
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
'''
Created on 6 Oct 2014
@author: podero
uploads a csv file to be processed by cron job.
Reads the text contained in the csv file and posts into the appengine datastore.
'''
import cgi
import ops
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from settings import TEMPLATES_PATH
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
import os
DIRECTORY = os.path.dirname(__file__)
_DEBUG = True
class MainHandler(webapp.RequestHandler):
def get(self):
values = defaultValues()
wireframe = 'upload_csv'
values['isAuthorised'] = True
values['isLogged'] = True
values['isSuperAdmin'] = ops.isSuperAdmin(users.get_current_user().email())
app_path = os.path.join(TEMPLATES_PATH,'%s.html' % wireframe)
values['app'] = template.render(app_path, values, debug=_DEBUG)
path = os.path.join(TEMPLATES_PATH,'index.html')
self.response.out.write(template.render(path, values, debug=_DEBUG))
class UploadHandler(webapp.RequestHandler):
def post(self):
user = users.get_current_user()
email = user.email()
form = cgi.FieldStorage()
filedata = form['hostupload']
data = filedata.value
ops.uploadFile(email, data)
values = defaultValues()
wireframe = 'upload_csv'
values['isAuthorised'] = True
values['isLogged'] = True
values['isSuperAdmin'] = ops.isSuperAdmin(users.get_current_user().email())
values['upload_message'] = 'File uploaded and will be processed in not more than 10 mins!'
app_path = os.path.join(TEMPLATES_PATH,'%s.html' % wireframe)
values['app'] = template.render(app_path, values, debug=_DEBUG)
path = os.path.join(TEMPLATES_PATH,'index.html')
self.response.out.write(template.render(path, values, debug=_DEBUG))
def defaultValues():
user = users.get_current_user()
nickname = user.nickname()
logout_url = users.create_logout_url('/')
institution = "Kenyatta University"
return {
'owner': nickname,
'email': user.email(),
'logout_url': logout_url,
'owner': user.email(),
'institution': institution,
'upload_message':''
}
def main():
application = webapp.WSGIApplication([('/upload_csv', MainHandler),
('/upload', UploadHandler)], debug=True)
run_wsgi_app(application)
if __name__ == "__main__":
main()