-
Notifications
You must be signed in to change notification settings - Fork 3
/
editor.py
executable file
·62 lines (45 loc) · 1.63 KB
/
editor.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
#!/usr/bin/env python
"""
A simple text editing app that saves files to a directory in cloud storage.
"""
from flask import (Flask, request, render_template, jsonify)
import kloudless
import os
usage_text = (
"\nUsage:\n"
" $ KLOUDLESS_APP_ID=app_id python editor.py\n"
"substituting 'app_id' with your Kloudless App ID.\n\n"
"You can get these at https://developers.kloudless.com/.\n"
"Please reach out to us at [email protected] if you have any "
"questions.\n")
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html',
app_id=os.environ['KLOUDLESS_APP_ID'])
else:
return save_file(request.form['account'], request.form['file_data'],
request.form['token'])
def save_file(account_id, file_data, token):
kloudless.configure(token=token)
# Create the folder in case it doesn't exist.
account = kloudless.Account(id=account_id)
folder = account.folders.create(data={
"name": "Cloud File Editor Sample App",
"parent_id": "root"})
f = account.files.create(file_name='test.txt', parent_id=folder.id,
file_data=file_data)
return jsonify(file_id=f.id)
def main():
for k in ['KLOUDLESS_APP_ID']:
if not os.environ.get(k):
print usage_text
return
if os.environ.get('KLOUDLESS_BASE_URL'):
kloudless.configure(base_url=os.environ['KLOUDLESS_BASE_URL'])
app.run()
if __name__ == '__main__':
main()