-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner_migrate.py
268 lines (217 loc) · 9.04 KB
/
planner_migrate.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
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
from flask import Flask, redirect, url_for, session, request, jsonify, render_template
from flask_oauthlib.client import OAuth, OAuthException
import migrator
import json
import os
from werkzeug.utils import secure_filename
# from flask_sslify import SSLify
from logging import Logger
import uuid
app = Flask(__name__)
# sslify = SSLify(app)
app.debug = True
app.secret_key = 'development'
oauth = OAuth(app)
UPLOAD_FOLDER = '/tmp/flask_uploads'
ALLOWED_EXTENSIONS = set(['json'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
config = json.load(open('config.json'))
# Put your consumer key and consumer secret into a config file
# and don't check it into github!!
export_permissions = [
'User.Read',
# 'User.Read.All',
'Directory.Read.All',
'Group.Read.All',
'Group.ReadWrite.All',
]
microsoft = oauth.remote_app(
'PlannerExporter',
consumer_key= config['export']['consumer_key'],
consumer_secret=config['export']['consumer_secret'],
request_token_params={'scope': ' '.join(export_permissions)},
base_url='https://graph.microsoft.com/v1.0/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://login.microsoftonline.com/common/oauth2/v2.0/token',
authorize_url='https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
)
import_permissions = [
'User.Read',
# 'Directory.Read.All',
# 'Group.Read.All',
# 'Group.ReadWrite.All'
# 'User.Read.All',
]
new_planner = oauth.remote_app(
'PlannerImporter',
consumer_key=config['import']['consumer_key'],
consumer_secret=config['import']['consumer_secret'],
request_token_params={'scope': ' '.join(import_permissions)},
base_url='https://graph.microsoft.com/v1.0/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://login.microsoftonline.com/common/oauth2/v2.0/token',
authorize_url='https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
)
#new_planner = oauth.remote_app(
# 'PlannerImporter',
# consumer_key='9b2d4bfe-b163-4f40-a7f2-1f9dbaa65b0f',
# consumer_secret='zjyqxPQD661~-{qpFZOF07=',
# request_token_params={'scope': ' '.join(import_permissions)},
# base_url='https://graph.microsoft.com/v1.0/',
# request_token_url=None,
# access_token_method='POST',
# access_token_url='https://login.microsoftonline.com/common/oauth2/v2.0/token',
# authorize_url='https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
#)
#exporter = migrator.Migrator(microsoft)
#PLANNER_API_URL = 'https://graph.microsoft.com/v1.0/planner'
@app.route('/')
def index():
return render_template('hello.html')
@app.route('/login', methods = ['POST', 'GET'])
def login():
if 'microsoft_token' in session:
return redirect(url_for('me'))
# Generate the guid to only accept initiated logins
guid = uuid.uuid4()
session['state'] = guid
return microsoft.authorize(callback=url_for('authorized', _external=True), state=guid)
@app.route('/logout', methods = ['POST', 'GET'])
def logout():
session.pop('microsoft_token', None)
session.pop('microsoft_token2', None)
session.pop('state', None)
session.pop('state2', None)
return redirect(url_for('index'))
@app.route('/login/authorized')
def authorized():
response = microsoft.authorized_response()
if response is None:
return "Access Denied: Reason=%s\nError=%s" % (
response.get('error'),
request.get('error_description')
)
# Check response for state
print("Response: " + str(response))
if str(session['state']) != str(request.args['state']):
raise Exception('State has been messed with, end authentication')
# Okay to store this in a local variable, encrypt if it's going to client
# machine or database. Treat as a password.
session['microsoft_token'] = (response['access_token'], '')
return redirect(url_for('me'))
@app.route('/me')
def me():
me = microsoft.get('me')
exporter = migrator.Migrator(microsoft)
groups = exporter.get("users/%s/memberOf" % (me.data.get('id')), session.get('microsoft_token')[0])
return render_template('me.html', me=me, groups=groups)
@app.route('/export')
def export():
gId = request.args.get('gId', '')
# me aka. owner
me = microsoft.get('me')
output_data = {}
# plan
exporter = migrator.Migrator(microsoft)
exporter.plans = exporter.get("groups/%s/planner/plans" % (gId), session.get('microsoft_token')[0])['value']
plan = exporter.plans[0]
output_data['plan'] = plan
plan['details'] = exporter.get("planner/plans/%s/details" % (exporter.getPlanId(0)), session.get('microsoft_token')[0])
# buckets /planner/plans/<id>/buckets
plan['buckets'] = exporter.get("planner/plans/%s/buckets" % (exporter.getPlanId(0)), session.get('microsoft_token')[0])
# tasks /planner/plans/{id}/tasks
tasks = exporter.get("planner/plans/%s/tasks" % (exporter.getPlanId(0)), session.get('microsoft_token')[0])['value']
plan['tasks'] = []
for task in tasks:
task['details'] = exporter.get("planner/tasks/%s/details" % (task['id']), session.get('microsoft_token')[0])
plan['tasks'].append(task)
with open('planner_export.json', 'w') as outfile:
json.dump(output_data, outfile)
#print "Logging in to new planner"
#return new_planner.authorize(callback=url_for('import_login', _external=True), state=guid2)
return redirect(url_for('logout'))
@app.route('/import')
def import_index():
return render_template('import.html')
@app.route('/import/do')
def import_data():
# load file
input_file = json.load(open(session['filename']))
print "Loaded file %s" % (session['filename'])
me = new_planner.get('me')
importer = migrator.Migrator(new_planner)
# create new plan
#groups = migrator.get("users/%s/memberOf" % (me.data.get('id')), session.get('microsoft_token')[0])
#groups = migrator.get("me/memberOf" % (me.data.get('id')), session.get('microsoft_token')[0])
groups = importer.get("me/memberOf", session.get('microsoft_token2')[0])
# find correct group
for g in groups:
print g
#new_plan = migrator.create_plan(groups['value'][0].get('id'), 'ENW migration test', session.get('microsoft_token2')[0])
# create buckets
# create tasks
# update task details
# add comments
# add attachments
return render_template('import_done.html', me=me.data, groups=groups)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/import/upload', methods=['GET', 'POST'])
def import_upload():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
session['filename'] = os.path.join(app.config['UPLOAD_FOLDER'], filename)
return render_template('import_upload.html', file=file)
return redirect(url_for('import_index'))
@app.route('/import/start', methods=['GET', 'POST'])
def import_start():
session.pop('microsoft_token2', None)
session.pop('state2', None)
# Generate the guid to only accept initiated logins
guid2 = uuid.uuid4()
session['state2'] = guid2
return new_planner.authorize(callback=url_for('import_login', _external=True), state=guid2)
@app.route('/login/import')
def import_login():
response = new_planner.authorized_response()
if response is None:
return "Access Denied: Reason=%s\nError=%s" % (
response.get('error'),
request.get('error_description')
)
# Check response for state
print("Response: " + str(response))
if str(session['state2']) != str(request.args['state']):
raise Exception('State has been messed with, end authentication')
# Okay to store this in a local variable, encrypt if it's going to client
# machine or database. Treat as a password.
session['microsoft_token2'] = (response['access_token'], '')
return redirect(url_for('import_data'))
# If library is having trouble with refresh, uncomment below and implement refresh handler
# see https://github.com/lepture/flask-oauthlib/issues/160 for instructions on how to do this
# Implements refresh token logic
# @app.route('/refresh', methods=['POST'])
# def refresh():
@microsoft.tokengetter
def get_microsoft_oauth_token():
return session.get('microsoft_token')
@new_planner.tokengetter
def get_microsoft_oauth_token2():
return session.get('microsoft_token2')
if __name__ == '__main__':
app.run()