-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (136 loc) · 5.5 KB
/
main.py
File metadata and controls
186 lines (136 loc) · 5.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
from flask import Flask, request, render_template
from base_class.base_func import list_tables_func, list_fields_func, update_data_func
import os, urllib.parse, base64, hashlib
import configparser
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
def writeToINI(section, key, value):
cfp = configparser.ConfigParser()
if not os.path.exists('config.ini'):
with open("config.ini", "w+") as f:
cfp.write(f)
cfp.read("config.ini")
if not cfp.has_section(section):
cfp.add_section(section)
cfp.set(section, key, value)
with open("config.ini", "w+") as f:
cfp.write(f)
def readFromINI(section, key):
cfp = configparser.ConfigParser()
cfp.read("config.ini")
value = cfp.get(section, key)
return value
@app.route('/setup', methods=['GET', 'POST'])
def setup():
if request.method == 'GET':
userid = request.args.get("userid")
baseid = request.args.get("baseid")
urllib.parse.quote(userid, safe="")
return render_template('setup.html', userid=userid, baseid=baseid)
else:
BASEID = request.form.get("baseid")
USERID = urllib.parse.unquote(request.form.get("userid"))
APP_TOKEN = request.form.get("app_token")
PERSONAL_BASE_TOKEN = request.form.get("personal_base_token")
if BASEID is None or BASEID == "":
return '用户身份验证失败,请刷新页面重试'
if USERID is None or USERID == "":
return '用户身份验证失败,请刷新页面重试'
if APP_TOKEN is None or APP_TOKEN == "":
return 'app_token参数错误'
if PERSONAL_BASE_TOKEN is None or PERSONAL_BASE_TOKEN == "":
return 'personal_base_token参数错误'
SECTION = BASEID + ":" + base64.b64encode(
(BASEID + ":" + USERID).encode('utf-8')).decode()
# print(SECTION, flush=True)
writeToINI(SECTION, 'APP_TOKEN', APP_TOKEN)
writeToINI(SECTION, 'PERSONAL_BASE_TOKEN', PERSONAL_BASE_TOKEN)
return {'code': 200, 'data': 'APP_TOKEN 和 PERSONAL_BASE_TOKEN 保存成功!'}
@app.route('/', methods=['GET'])
def entry():
return render_template('entry.html')
@app.route('/main', methods=['GET'])
def main():
try:
BASEID = request.args.get("baseid")
USERID = request.args.get("userid")
SECTION = BASEID + ":" + base64.b64encode(
(BASEID + ":" + USERID).encode('utf-8')).decode()
# print(SECTION, flush=True)
except Exception as e:
return '请在多维表格的扩展脚本中打开首页'
try:
APP_TOKEN = readFromINI(SECTION, 'APP_TOKEN')
PERSONAL_BASE_TOKEN = readFromINI(SECTION, 'PERSONAL_BASE_TOKEN')
except Exception as e:
USERID = urllib.parse.quote(request.args.get("userid"), safe="")
return render_template('setup.html', userid=USERID, baseid=BASEID)
try:
# data = list_tables_func(APP_TOKEN, PERSONAL_BASE_TOKEN)
# return render_template('main.html',
# tables_list=data.get("tables_list"),
# fields_list=data.get("fields_list"),
# section=SECTION,
# base_token=PERSONAL_BASE_TOKEN)
return render_template('main.html',
tables_list="",
fields_list="",
section=SECTION,
base_token=PERSONAL_BASE_TOKEN)
except Exception as e:
return '数据表列表获取错误!!!'
@app.route('/get_tables', methods=['POST'])
def get_tables():
SECTION = request.args.get("section")
try:
APP_TOKEN = readFromINI(SECTION, 'APP_TOKEN')
except Exception as e:
return 'APP_TOKEN获取失败,请先进行初始化设置'
try:
PERSONAL_BASE_TOKEN = readFromINI(SECTION, 'PERSONAL_BASE_TOKEN')
except Exception as e:
return 'PERSONAL_BASE_TOKEN获取失败,请先进行初始化设置'
try:
result = list_tables_func(APP_TOKEN, PERSONAL_BASE_TOKEN)
# print(result.get("tables_list"),flush=True)
return {"code": 200, "data": result.get("tables_list")}
except Exception as e:
return '数据表列表获取错误!!!'
@app.route('/get_fields', methods=['POST'])
def get_fields():
SECTION = request.args.get("section")
try:
APP_TOKEN = readFromINI(SECTION, 'APP_TOKEN')
except Exception as e:
return 'APP_TOKEN获取失败,请先进行初始化设置'
try:
PERSONAL_BASE_TOKEN = readFromINI(SECTION, 'PERSONAL_BASE_TOKEN')
except Exception as e:
return 'PERSONAL_BASE_TOKEN获取失败,请先进行初始化设置'
TABLE_ID = request.form.get("table_id")
try:
result = list_fields_func(APP_TOKEN, PERSONAL_BASE_TOKEN, TABLE_ID)
return {"code": 200, "data": result}
except Exception as e:
return '数据表字段获取错误!!!'
@app.route('/update_data', methods=['POST'])
def update_data():
SECTION = request.args.get("section")
try:
APP_TOKEN = readFromINI(SECTION, 'APP_TOKEN')
except Exception as e:
return 'APP_TOKEN获取失败,请先进行初始化设置'
try:
PERSONAL_BASE_TOKEN = readFromINI(SECTION, 'PERSONAL_BASE_TOKEN')
except Exception as e:
return 'PERSONAL_BASE_TOKEN获取失败,请先进行初始化设置'
TABLE_ID = request.form.get("table_id")
FIELD_NAME = request.form.get("field_name")
SEPARATOR = request.form.get("separator")
try:
result = update_data_func(APP_TOKEN, PERSONAL_BASE_TOKEN, TABLE_ID,
FIELD_NAME, SEPARATOR)
return {"code": 200, "data": result}
except Exception as e:
return '数据分列失败!!!'
app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=True)