File size: 7,828 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from flask import *
import db
import json
import sqlite3 as sql

# create and configure the app
app = Flask(__name__)
app.config.from_mapping(
#    SECRET_KEY='52acfd60b764849879dcea66fd0cecfac42547f742128204c1b5dcceffe1773a',
    DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
db.init_app(app)

@app.route('/')
def show():
    return render_template('testing_interface.html')

@app.route('/logs')
def show_logs():
    return render_template("log_interface.html")

@app.route('/log_db')
def getLogs():
    try:
        cur = db.get_db().cursor()
        cur.execute("SELECT * from logs")
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/tasklist', methods=['GET'])
def getTaskList():
    data = []
    try:
        cur = db.get_db().cursor()
        cur.execute("SELECT * from tasklist")
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/tasklist/<taskname>', methods=['GET'])
def getTask(taskname):
    return

@app.route('/', methods=['POST'])
def store_log():
    # store logs
    return

@app.route('/testset')
def show_testset():
    return render_template('testset_interface.html')

@app.route('/testset/<state>')
def show_url_param(state):
    return render_template('testset_interface.html', id=state)

@app.route('/testset/submit', methods=['POST', 'GET'])
def store_final_set():
    json_obj = request.json
    try:
        con = db.get_db()
        con.execute(
            "INSERT INTO testsets (user_id, test_id, testjson, approve, ratings, Description) VALUES (?, ?, ?, ?, ?, ?)", (json_obj.get('user_id'), json_obj.get('test_id'), json.dumps(json_obj), False, 0, json_obj.get('description'))
        )
        con.commit()
    except Exception as e:
        print(e)
    return render_template('testset_interface.html')

@app.route('/testset/submit_approval', methods=['POST', 'GET'])
def update_approval():
    print("submit_approval")
    json_obj = request.json
    try:
        con = db.get_db()
        _query = "UPDATE testsets SET approve=" + str(json_obj.get('approve')) + " WHERE test_id='" + json_obj.get('test_id') + "'"
        print(_query)
        con.execute(_query)
        con.commit()
    except Exception as e:
        print(e)
    return render_template('testset_list_admin.html')

@app.route('/testset/delete', methods=['POST', 'GET'])
def delete_set():
    print("submit_approval")
    json_obj = request.json
    try:
        con = db.get_db()
        _query = "DELETE FROM testsets WHERE test_id='" + json_obj.get('test_id') + "'"
        print(_query)
        con.execute(_query)
        con.commit()
    except Exception as e:
        print(e)
    return render_template('testset_list_admin.html')

@app.route('/testset/list')
def show_test_list():
    return render_template('testset_list.html')

@app.route('/testset/getlist', methods=['POST', 'GET'])
def get_test_list():
    try:
        cur = db.get_db().cursor()
        cur.execute("SELECT * from testsets")
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/testset/get_approved_list', methods=['POST', 'GET'])
def get_approved_test_list():
    try:
        cur = db.get_db().cursor()
        cur.execute("SELECT * from testsets WHERE approve=1")
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/testset/get_disapproved_list', methods=['POST', 'GET'])
def get_disapproved_test_list():
    try:
        cur = db.get_db().cursor()
        cur.execute("SELECT * from testsets WHERE approve=0")
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/testset/queryone', methods=['POST', 'GET'])
def get_test_one():
    json_idx = request.args.get('index')
    query_ = "SELECT * from testsets WHERE test_id='" + json_idx + "'"
    try:
        cur = db.get_db().cursor()
        cur.execute(query_)
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/testset/admin')
def show_test_list_admin():
    return render_template('testset_list_admin.html')

@app.route('/testset/search', methods=['POST', 'GET'])
def search_test():
    _user_id = request.args['user_id']
    _description = request.args['description']
    _approval = request.args['approval']
    print("search_testset", _user_id, _description, _approval)
    query_ = ""
    if(_user_id):
        if(_description):
            query_ = "SELECT * from testsets WHERE user_id='" + _user_id + "' AND description='"+_description+"'"
        else:
            query_ = "SELECT * from testsets WHERE user_id='" + _user_id + "'"
    else:
        if(_description):
            query_ = "SELECT * from testsets WHERE description='"+_description+"'"
        else:
            query_ = "SELECT * from testsets"
    if(int(_approval) > -1):
        if(_user_id or _description):
            query_ = query_ + " AND approve=" + _approval
        else:
            query_ = query_ + " WHERE approve=" + _approval
    print("search: ", query_)
    try:
        cur = db.get_db().cursor()
        cur.execute(query_)
        data = [dict((cur.description[i][0], value) \
               for i, value in enumerate(row)) for row in cur.fetchall()]
    except Exception as e:
        print(e)
    return jsonify(data)

@app.route('/testset/approved')
def show_approved_testset():
    return render_template('testset_approved_list.html')

@app.route('/testset/save', methods=['POST', 'GET'])
def save_json_test():
    _list = request.json['testsets']
    for d in _list:
        query_ = "SELECT * from testsets WHERE test_id='" + d['testid'] + "' AND user_id='"+ d['userid'] + "' AND approve=1"
        try:
            cur = db.get_db().cursor()
            cur.execute(query_)
            data = [dict((cur.description[i][0], value) \
                for i, value in enumerate(row)) for row in cur.fetchall()]
            json_obj = json.loads(data[0]['testjson'])
            testpairs = json.loads(json_obj['testArray'])
            final_set = dict()
            final_set['train'] = []
            final_set['test'] = []
            testpairs[-1]['input'] = testpairs[-1].pop('input_cells')
            testpairs[-1]['output'] = testpairs[-1].pop('output_cells')
            final_set['test'].append(testpairs[-1])
            for i in range(len(testpairs)-1):
                v = testpairs[i]
                v['input'] = v.pop('input_cells')
                v['output'] = v.pop('output_cells')
                final_set['train'].append(v)
            with open('../../data/generated/{}_{}_{}.json'.format(d.get('userid'), d.get('Description'), d.get('testid')), 'w') as f:
                json.dump(final_set, f)
        except Exception as e:
            print(e)
    return jsonify('data')

if __name__ == "__main__":
    app.run(host='0.0.0.0', port='80', debug=False)