views.py 17.5 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
2
from app import app

Vladislav Rykov's avatar
Vladislav Rykov committed
3
from flask import render_template, request, redirect, url_for, session, send_from_directory, flash
4
import psycopg2
Vladislav Rykov's avatar
Vladislav Rykov committed
5

Vladislav Rykov's avatar
Vladislav Rykov committed
6
7
8
9
10
import app.dao.user.user as ud
import app.dao.application.application as ad
import app.dao.device.device as dd
import app.dao.pend.pend as pend
import app.dao.data.data as data
Vladislav Rykov's avatar
Vladislav Rykov committed
11

Vladislav Rykov's avatar
Vladislav Rykov committed
12
import app.helpers.misc as misc
Vladislav Rykov's avatar
Vladislav Rykov committed
13

14
import binascii
15
import os
Vladislav Rykov's avatar
Vladislav Rykov committed
16

17

18
19
MAX_PG = 5
MAX_PG_ENTRIES_USERS = 10
20
21
MAX_PG_ENTRIES_DATA = 10
MAX_PG_ENTRIES_GRAPH_HOURS = 24
22
23


Vladislav Rykov's avatar
Vladislav Rykov committed
24
@app.route('/')
25
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
26
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
27
28
        apps = ad.get_list(session['name'])
        
Vladislav Rykov's avatar
Vladislav Rykov committed
29
        session.pop('appkey', None)
Vladislav Rykov's avatar
Vladislav Rykov committed
30
        # print('apps: ', apps)
31
        if apps[0]:
32
            return render_template('public/index.html', apps=apps[1], users_signup=app.config['USERS_SIGNUP'])
33
        else:
34
            return render_template('public/index.html', feedback=apps[1], users_signup=app.config['USERS_SIGNUP'])
Vladislav Rykov's avatar
Vladislav Rykov committed
35
    else:
36
        return render_template('public/index.html', users_signup=app.config['USERS_SIGNUP'])
37
38
39



Vladislav Rykov's avatar
Vladislav Rykov committed
40
@app.route('/signup', methods=['GET', 'POST'])
41
42
def signup():
    if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
43
        if session['role'] and session['role'] == 'admin':
44
            return render_template('admin/signup.html', users_signup=app.config['USERS_SIGNUP'])
Vladislav Rykov's avatar
Vladislav Rykov committed
45
        else:
46
            if app.config['USERS_SIGNUP']:
47
                return render_template('public/signup.html', users_signup=app.config['USERS_SIGNUP'])
48
            else:
49
                return redirect(url_for('index', users_signup=app.config['USERS_SIGNUP']))
50
51
52
53
54
55
56
    else:
        if app.config['USERS_SIGNUP'] or session['role'] == 'admin':
            username = request.form['username']
            password = request.form['password'].encode('utf-8')
            
            if (username == '' or password == ''):
                feedback = 'Username or password fields cannot be empty'
57
                return render_template('public/signup.html', feedback=feedback, users_signup=app.config['USERS_SIGNUP'])
58
59
            elif (len(password) < 8):
                flash('Password length must be at least 8 characters.', 'danger')
60
                return redirect(request.url, users_signup=app.config['USERS_SIGNUP'])
Vladislav Rykov's avatar
Vladislav Rykov committed
61
            else:
62
63
64
65
66
67
68
69
                role = 'user'
                if request.form['role'] and request.form['role'] == 'administrator':
                    role = 'admin'

                res = ud.create(username, password, role)
                if (not res[0]):
                    flash('Error: {}'.format(res[1]), 'danger')
                    return redirect(request.url)
Vladislav Rykov's avatar
Vladislav Rykov committed
70
                else:
71
72
73
                    session['name'] = username
                    
                    flash('User successfully created.', 'success')
74

75
76
77
78
79
80
                    if session['role'] and session['role'] == 'admin':
                        return redirect(url_for('dashboard'))
                    else:
                        return redirect(url_for('index'))
        else:
            return redirect(url_for('index'))
81
82


Vladislav Rykov's avatar
Vladislav Rykov committed
83
@app.route('/login', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
84
def login():
Vladislav Rykov's avatar
Vladislav Rykov committed
85
    if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
86
        return render_template('public/login.html')
Vladislav Rykov's avatar
Vladislav Rykov committed
87
88
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
89
        password = request.form['password'].encode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
90
91

        if (username == '' or password == ''):
Vladislav Rykov's avatar
Vladislav Rykov committed
92
93
            flash('Username or password fields cannot be empty', 'danger')
            return redirect(request.url)
Vladislav Rykov's avatar
Vladislav Rykov committed
94
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
95
            res = ud.check(username, password)
Vladislav Rykov's avatar
Vladislav Rykov committed
96
            if (not res[0]):
Vladislav Rykov's avatar
Vladislav Rykov committed
97
98
                flash('Error: {}'.format(res[1]), 'danger')
                return redirect(request.url)
Vladislav Rykov's avatar
Vladislav Rykov committed
99
100
            else:
                session['name'] = username
101
                session['role'] = res[1][2]
Vladislav Rykov's avatar
Vladislav Rykov committed
102
        
Vladislav Rykov's avatar
Vladislav Rykov committed
103
104
105
                return redirect(url_for('index'))


106

Vladislav Rykov's avatar
Vladislav Rykov committed
107
@app.route('/logout')
Vladislav Rykov's avatar
Vladislav Rykov committed
108
109
110
def logout():
    session.clear()
    return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
111
112


113

Vladislav Rykov's avatar
Vladislav Rykov committed
114
@app.route('/new-app')
115
def new_application():
Vladislav Rykov's avatar
Vladislav Rykov committed
116
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
117
        return render_template('public/new-app.html')
Vladislav Rykov's avatar
Vladislav Rykov committed
118
119
    else:
        return redirect(url_for('index'))
120
121
122



Vladislav Rykov's avatar
Vladislav Rykov committed
123
124
@app.route('/app', methods=['GET', 'POST'])
def app_():
Vladislav Rykov's avatar
Vladislav Rykov committed
125
126
127
    if 'name' in session:
        if request.method == 'GET':
            session['appkey'] = request.args.get('appkey')
Vladislav Rykov's avatar
Vladislav Rykov committed
128

Vladislav Rykov's avatar
Vladislav Rykov committed
129
130
            ap = ad.get(session['appkey'])
            devs = dd.get_list(ap[1][1])
131
132
133
134
135
            
            if session['role'] == 'admin' or session['name'] == ap[1][2]:
                return render_template('public/app.html', app=ap[1], devs=devs[1])
            else:
                return redirect(url_for('index'))
136
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
137
138
            if request.form['appname'] == '':
                error = 'Application name cannot be empty.'
Vladislav Rykov's avatar
Vladislav Rykov committed
139
                return render_template('public/new-app.html', feedback=error)
Vladislav Rykov's avatar
Vladislav Rykov committed
140
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
141
                appkey = misc.rand_str(app.config['APPKEY_LENGTH']).decode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
142
                res = ad.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
143
            
Vladislav Rykov's avatar
Vladislav Rykov committed
144
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
145
                    return render_template('public/new-app.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
146
            
Vladislav Rykov's avatar
Vladislav Rykov committed
147
                res = dd.create_table(appkey)
148
            
Vladislav Rykov's avatar
Vladislav Rykov committed
149
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
150
                    ad.delete(appkey)
Vladislav Rykov's avatar
Vladislav Rykov committed
151
                    return render_template('public/new-app.html', feedback=res[1])
152
            
Vladislav Rykov's avatar
Vladislav Rykov committed
153
154
155
                return redirect(url_for('index'))
    else:
        return redirect(url_for('index'))
156

Vladislav Rykov's avatar
Vladislav Rykov committed
157
@app.route('/delete-app')
158
def delete_app():
Vladislav Rykov's avatar
Vladislav Rykov committed
159
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
160
        devs = dd.get_list(session['appkey'])
161
    
Vladislav Rykov's avatar
Vladislav Rykov committed
162
163
        for dev in devs[1]:
            data.delete_table(session['appkey'], dev[1])
164
    
Vladislav Rykov's avatar
Vladislav Rykov committed
165
        dd.delete_table(session['appkey'])
166
    
Vladislav Rykov's avatar
Vladislav Rykov committed
167
        res = ad.delete(session['appkey'])
168
    
Vladislav Rykov's avatar
Vladislav Rykov committed
169
        if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
170
            return redirect(url_for('app_'))
Vladislav Rykov's avatar
Vladislav Rykov committed
171
172
        else:
            return redirect(url_for('index'))
173
174
    else:
        return redirect(url_for('index'))
175

Vladislav Rykov's avatar
Vladislav Rykov committed
176

Vladislav Rykov's avatar
Vladislav Rykov committed
177
@app.route('/add-dev')
Vladislav Rykov's avatar
Vladislav Rykov committed
178
def new_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
179
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
180
        dev_list = dd.get_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
181
    
Vladislav Rykov's avatar
Vladislav Rykov committed
182
    #print('dev list : ', dev_list)
Vladislav Rykov's avatar
Vladislav Rykov committed
183

Vladislav Rykov's avatar
Vladislav Rykov committed
184
        if not dev_list[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
185
            return render_template('public/add-dev.html', feedback=dev_list[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
186
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
187
            return render_template('public/add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
188
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
189
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
190
191
192
 


Vladislav Rykov's avatar
Vladislav Rykov committed
193
@app.route('/dev', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
194
def dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
195
196
    if 'name' in session:
        if request.method == 'GET':
197
198
199
200
201
202
            ap = ad.get(session['appkey'])
            if session['role'] == 'admin' or session['name'] == ap[1][2]:
                dev = dd.get(session['appkey'], request.args.get('id'))

                session['devid'] = int(dev[1][1])
                session['devname'] = dev[1][0]
Vladislav Rykov's avatar
Vladislav Rykov committed
203
        
204
                last = data.get_last_n(session['appkey'], session['devid'], 1)
Vladislav Rykov's avatar
Vladislav Rykov committed
205
        
206
                ltup = 'Device have not sent data yet'
Vladislav Rykov's avatar
Vladislav Rykov committed
207

208
209
                if last[0]:
                    ltup = last[1][0][1]
210

211
212
213
                return render_template('public/dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
            else:
                return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
214
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
215
            res = dd.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
216

217
            if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
218
                return render_template('public/add-dev.html', feedback=res[1])
219
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
220
221
222
                res = data.create_table(session['appkey'], request.form['devid'])
            
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
223
                    dd.delete(session['appkey'], request.form['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
224
                    return render_template('public/add-dev.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
225
                else:
226
                    return redirect(url_for('app_', appkey=session['appkey']))
Vladislav Rykov's avatar
Vladislav Rykov committed
227
228
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
229
230


Vladislav Rykov's avatar
Vladislav Rykov committed
231
@app.route('/dev-conf', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
232
def dev_conf():
Vladislav Rykov's avatar
Vladislav Rykov committed
233
234
    if 'name' in session and 'devid' in session:
        if request.method == 'GET':
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            pend_msgs = pend.get_list(session['appkey'], session['devid'])
            
            if pend_msgs[0]:
                config_list = []

                for pm in pend_msgs[1]:
                    cntt = binascii.a2b_base64(pm[2])
                    config_id = int(cntt[0])
                    config_args = cntt[2:(len(cntt)-1)].decode('utf-8')
                    ack = pm[3]
                    config_list.append((config_id, config_args, ack, pm[2]))

                return render_template('public/dev-conf.html', devname=session['devname'], config_list=config_list)
            else:
                return render_template('public/dev-conf.html', devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
250
251
252
253
254
        else:
            argslen = len(request.form['arg']) + 1
            args = bytearray(argslen + 2)
            args[0] = int(request.form['confid'])
            args[1] = argslen
Vladislav Rykov's avatar
Vladislav Rykov committed
255
        
256
            bstr = bytes(request.form['arg'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
257
258
259
260
            i = 0
            while i < argslen - 1:
                args[2+i] = bstr[i]
                i += 1
Vladislav Rykov's avatar
Vladislav Rykov committed
261

Vladislav Rykov's avatar
Vladislav Rykov committed
262
            base64_args = binascii.b2a_base64(args).decode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
263

Vladislav Rykov's avatar
Vladislav Rykov committed
264
            pend.create(session['appkey'], session['devid'], base64_args)
Vladislav Rykov's avatar
Vladislav Rykov committed
265

Vladislav Rykov's avatar
Vladislav Rykov committed
266
267
268
            return redirect(url_for('dev', id=session['devid']))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
269

270
271
272
273
274
275
276
277
278
279
280
281
282
@app.route('/dev-conf-rm')
def dev_conf_rm():
    if 'name' in session and 'appkey' in session and 'devid' in session:
        res = pend.delete(session['appkey'], session['devid'], request.args.get('conf')+'_')

        if res[0]:
            flash('Configuration message successfully removed.','success')
            return redirect(url_for('dev_conf'))
        else:
            flash('Error removing configuration message: {}'.format(res[1]), 'danger')
            return redirect(url_for('dev_conf'))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
283

Vladislav Rykov's avatar
Vladislav Rykov committed
284
@app.route('/delete-dev')
Vladislav Rykov's avatar
Vladislav Rykov committed
285
def delete_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
286
287
    if 'name' in session and 'devid' in session:
        data.delete_table(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
288
        res = dd.delete(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
289

Vladislav Rykov's avatar
Vladislav Rykov committed
290
        return redirect(url_for('app_', appkey=session['appkey']))
Vladislav Rykov's avatar
Vladislav Rykov committed
291
292
    else:
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
293
294


Vladislav Rykov's avatar
Vladislav Rykov committed
295
@app.route('/dev-data')
296
297
298
299
300
301
302
303
304
305
def dev_data_pg():
    if 'name' in session and 'devid' in session:
        cur_pg = 1
        if request.args.get('p'):
            cur_pg = int(request.args.get('p'))
            if cur_pg < 1:
                cur_pg = 1
        
        last = data.get_last_range(session['appkey'], session['devid'], [MAX_PG_ENTRIES_DATA, (cur_pg-1)*MAX_PG_ENTRIES_DATA])
        
Vladislav Rykov's avatar
Vladislav Rykov committed
306
307
308
309
        ent_cnt = data.get_count(session['appkey'], session['devid'])
        if ent_cnt[0]:
            # range data
            rd = misc.paging(cur_pg, ent_cnt[1][0], MAX_PG_ENTRIES_DATA, MAX_PG)
310

Vladislav Rykov's avatar
Vladislav Rykov committed
311
            if ent_cnt[1][0] > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
312
                return render_template('public/dev-data-t.html', data=last[1], total=ent_cnt[1][0], cp=cur_pg, np=rd[2], pp=rd[0], pr=rd[1], devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
313
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
314
                return render_template('public/dev-data-t.html', devname=session['devname'])
315
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
316
            flash('Error: {}'.format(ent_cnt[1]), 'danger')
Vladislav Rykov's avatar
Vladislav Rykov committed
317
            return render_template('public/dev-data-t.html', devname=session['devname'])
318
319
320
321
322
    else:
        return redirect(utl_for('index'))



Vladislav Rykov's avatar
Vladislav Rykov committed
323
@app.route('/data-csv')
Vladislav Rykov's avatar
Vladislav Rykov committed
324
def data_csv():
Vladislav Rykov's avatar
Vladislav Rykov committed
325
326
    if 'name' in session and 'devid' in session:
        dumpd = data.get_all(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
327

Vladislav Rykov's avatar
Vladislav Rykov committed
328
        fn = session['appkey']+ '_' +str(session['devid'])+ '.csv'
Vladislav Rykov's avatar
Vladislav Rykov committed
329

330
        with open(app.config['DATA_DOWNLOAD_DIR_OS']+'/'+fn, 'w+') as f: 
Vladislav Rykov's avatar
Vladislav Rykov committed
331
            f.write('utc,timestamp,')
Vladislav Rykov's avatar
Vladislav Rykov committed
332
333
            for d in dumpd[1][0][2]:
                f.write(d)
Vladislav Rykov's avatar
Vladislav Rykov committed
334
335
                f.write(',')
            f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
336
337
        
            for row in dumpd[1]:
Vladislav Rykov's avatar
Vladislav Rykov committed
338
                f.write('{},{},'.format(row[0],row[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
339
340
341
342
                for v in row[2]:
                    f.write(str(row[2][v]))
                    f.write(',')
                f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
343
    
Vladislav Rykov's avatar
Vladislav Rykov committed
344
        return send_from_directory(app.config['DATA_DOWNLOAD_DIR'], fn, as_attachment=True)
Vladislav Rykov's avatar
Vladislav Rykov committed
345
346
    else:
        return redirect(utl_for('index'))
347
348


Vladislav Rykov's avatar
Vladislav Rykov committed
349
350
351
352
353
354
355
356
357
358
359
360
361
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
    if 'role' in session and session['role'] == 'admin':
        user_cnt = ud.get_count()
        apps_cnt = ad.get_count()
        devs_cnt = dd.get_count_all()

        cur_pg = 1
        if request.args.get('p'):
            cur_pg = int(request.args.get('p'))
            if cur_pg < 1:
                cur_pg = 1

Vladislav Rykov's avatar
Vladislav Rykov committed
362
        users = None
Vladislav Rykov's avatar
Vladislav Rykov committed
363
        
Vladislav Rykov's avatar
Vladislav Rykov committed
364
365
        if request.method == 'POST':
            session['users_filter'] = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
366
            
Vladislav Rykov's avatar
Vladislav Rykov committed
367
368
369
370
        if 'users_filter' in session:
            users = ud.get_range_name(session['users_filter'], [MAX_PG_ENTRIES_USERS, (cur_pg-1)*MAX_PG_ENTRIES_USERS])
            print(users)
            rd = misc.paging(cur_pg, len(users[1]), MAX_PG_ENTRIES_USERS, MAX_PG)
Vladislav Rykov's avatar
Vladislav Rykov committed
371
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
372
373
374
375
            users = ud.get_range([MAX_PG_ENTRIES_USERS, (cur_pg-1)*MAX_PG_ENTRIES_USERS])
            rd = misc.paging(cur_pg, user_cnt[1][0], MAX_PG_ENTRIES_USERS, MAX_PG)
        
        return render_template('admin/dashboard.html', users_cnt=user_cnt[1][0], apps_cnt=apps_cnt[1][0], dev_cnt=devs_cnt, users=users[1], pp=rd[0], pr=rd[1], np=rd[2], cp=cur_pg, usn=(cur_pg-1)*MAX_PG_ENTRIES_USERS+1)
Vladislav Rykov's avatar
Vladislav Rykov committed
376
377
378
    else:
        return redirect(url_for('index'))

Vladislav Rykov's avatar
Vladislav Rykov committed
379
380
381
382
383
384
385

@app.route('/dashboard-clean-search')
def dashboard_clean_search():
    if 'users_filter' in session:
        session.pop('users_filter', None)
    return redirect(url_for('dashboard'))

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

@app.route('/user')
def user():
    if 'role' in session and session['role'] == 'admin':
        name = request.args.get('name')
        apps = ad.get_list(name)
        
        session.pop('appkey', None)
        # print('apps: ', apps)
        if apps[0]:
            return render_template('admin/user.html', apps=apps[1], username=name)
        else:
            return render_template('admin/user.html', feedback=apps[1], username=name)
    else:
        return render_template('public/index.html')


@app.route('/user-delete')
def user_delete():
    user = ud.get(request.args.get('name'))
Vladislav Rykov's avatar
Vladislav Rykov committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    if user[0] and user[1][2] != 'admin' and session['role'] == 'admin':
        app_list = ad.get_list(user[1][0])

        res = (True,)
        if app_list[0]:
            for app in app_list[1]:
                devs = dd.get_list(app[1])
                print('devs: {}'.format(devs))
                for dev in devs[1]:
                    res = data.delete_table(app[1], dev[1])
                    print ('data del {}'.format(res))
                    if not res[0]:
                        break
    
                if res[0]:
                    res = dd.delete_table(app[1])
                    print ('devices del {}'.format(res))
                    
                if res[0]:
                    res = ad.delete(app[1])
                    print ('app del {}'.format(res))

                if not res[0]:
                    break

        if res[0]:
            res = ud.delete(user[1][0])
            print ('user del {}'.format(res))

        if not res[0]:
            flash('Error: {}'.format(res[1]), 'danger')
            return render_template('admin/user.html', username=user[1][0])
        else:
                return redirect(url_for('dashboard'))
    else:
        flash('Warning: the user is admin or does not exist.' ,'warning')
        return redirect(url_for('index'))
443

444
445
446
447
@app.route('/settings', methods=['GET', 'POST'])
def settings():
    if request.method == 'GET':
        if session['role'] == 'admin':
448
            return render_template('admin/settings.html', username=session['name'], users_signup=app.config['USERS_SIGNUP'])
449
450
451
452
453
454
455
456
457
458
459
460
461
462
        else:
            return render_template('public/settings.html', username=session['name'])
    else:
        if request.form['name'] != session['name']:
            res = ud.update_name(session['name'], request.form['name'])
            if not res[0]:
                flash('Error: {}'.format(res[1]), 'danger')
                return redirect(request.url);
            else:
                session['name'] = request.form['name']
        if request.form['password'] != '':
            res = ud.update_password(session['name'], request.form['password'].encode('utf-8'))
            if not res[0]:
                flash('Error: {}'.format(res[1]), 'danger')
463
464
465
466
467
468
                return redirect(request.url)
        if session['role'] == 'admin':
            if request.form.getlist('users_signup') and request.form.getlist('users_signup')[0] == 'us':
                app.config['USERS_SIGNUP'] = True
            else:
                app.config['USERS_SIGNUP'] = False
469
470


471
        return redirect(request.url)
472
473


474
475
@app.route('/dev-data/<var>/<dest>/<page>')
def dev_data(var, dest, page):
476
    if dest == 'graph':
477
        last = data.get_last_hours(session['appkey'], session['devid'], MAX_PG_ENTRIES_GRAPH_HOURS, int(page))
478
479
480
481
482
483
484
485
        arr = '[["Time", "{}"],'.format(var)
        if last[0]:
            for d in last[1]:
                arr += '[new Date('+str(d[0])+'*1000),'+str(d[2][var])+'],'
            arr += ']'
        return arr
    elif dest == 'table':
        # for table <cnt> is in items
486
        last = data.get_last_range(session['appkey'], session['devid'], [MAX_PG_ENTRIES_DATA, (int(page)-1)*MAX_PG_ENTRIES_DATA])
487
488
489
490
491
492
493
        #t = """ <thead>
        #            <th>Time</th>
        #            <th>{}</th>
        #        </thead>
        #        <tbody>
        #""".format(var)
        t = ''
494
495
496
        if last[0]:
            for d in last[1]:
                t += '<tr><th>'+d[1]+'</th><th>'+str(d[2][var])+'</th></tr>'
497
        #t += '</tbody>'
498
        return t
499
500


501
502
def pend_delete_all_ack():
    pend.delete_all_ack()