-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
157 lines (111 loc) · 3.83 KB
/
app.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
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
import datetime
import os
import pprint
from flask import Flask, jsonify, request, render_template, url_for
from flask_caching import Cache
from werkzeug.exceptions import Forbidden
import json
from flask.wrappers import Response
import Config
import RestAuthController, RestUserController
import string
import secrets
from multiprocessing import Process
from flask_wtf import form
class ReverseProxied(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
scheme = environ.get('HTTP_X_FORWARDED_PROTO')
if scheme:
environ['wsgi.url_scheme'] = scheme
return self.app(environ, start_response)
app = Flask('bbdn-biab-registration', template_folder='templates', static_folder='static')
app.wsgi_app = ReverseProxied(app.wsgi_app)
cache = Cache(app)
PAGE_TITLE = 'Michigan BUG Registration'
basic_oauth_service = RestAuthController.RestAuthController()
class Password():
def generate(self):
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(10))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
return password
def init_service():
global basic_oauth_service
try:
basic_oauth_service = RestAuthController.RestAuthController()
basic_oauth_service.setBasicToken()
except Exception as e:
print(str(e))
print("call init_service")
init_service()
print("init_service completed, start app")
@app.route('/', methods=['GET'])
def registration():
tpl_kwargs = {
'page_title': PAGE_TITLE
}
return render_template('registration.html')
@app.route('/register/', methods=['POST'])
def register():
form = request.form
print(str(form))
given_name = form['fname']
family_name = form['lname']
email = form['email']
company = form['institution']
job_title = form['title']
password = Password().generate()
print(password)
heavy_process = Process( # Create a daemonic process with heavy "my_func"
target=executeRegistration, args=(given_name,family_name,email,company,job_title,password),
daemon=True
)
heavy_process.start()
print("heavy process started")
tp_kwargs = {
'page_title' : "Registration Confirmed!",
'url': 'https://' + Config.config['learn_rest_url'],
'username' : email,
'password' : password
}
print(str(tp_kwargs))
return render_template('confirmation.html', **tp_kwargs)
def executeRegistration(given_name,family_name,email,company,job_title,password):
global basic_oauth_service
token = basic_oauth_service.getBasicToken()
userCtrl = RestUserController.RestUserController(Config.config['learn_rest_url'], token)
user = {
"userName": email,
"password": password,
"institutionRoleIds" : [
"kbug"
],
"availability": {
"available": "Yes"
},
"name": {
"given": given_name,
"family": family_name
},
"job": {
"title": job_title,
"company": company
},
"contact": {
"email": email
}
}
devres = userCtrl.createLearnUser(user)
enrres = userCtrl.enrollUserInCourse("userName:" + email)
if __name__ == '__main__':
print("call init_service")
init_service()
print("init_service completed, start app")
port = int(os.environ.get('PORT', 9001))
app.run(host='0.0.0.0', port=port)