views.py 35.8 KB
Newer Older
1
2
from app import app, mail
from flask_mail import Message
Vladislav Rykov's avatar
Vladislav Rykov committed
3

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

Vladislav Rykov's avatar
Vladislav Rykov committed
7
8
9
10
11
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
12
13
import app.dao.notification.notification as nfs
import app.dao.trigger.trigger as tr
Vladislav Rykov's avatar
Vladislav Rykov committed
14
import app.dao.notification_queue.notification_queue as nq
Vladislav Rykov's avatar
Vladislav Rykov committed
15
import app.dao.misc.misc as md
Vladislav Rykov's avatar
Vladislav Rykov committed
16

Vladislav Rykov's avatar
Vladislav Rykov committed
17
import app.helpers.misc as misc
18
import app.helpers.mailer as mailer
Vladislav Rykov's avatar
Vladislav Rykov committed
19

20
import os
Vladislav Rykov's avatar
Vladislav Rykov committed
21
import binascii
Vladislav Rykov's avatar
Vladislav Rykov committed
22

23

24
25
MAX_PG = 5
MAX_PG_ENTRIES_USERS = 10
26
27
MAX_PG_ENTRIES_DATA = 10
MAX_PG_ENTRIES_GRAPH_HOURS = 24
28

Vladislav Rykov's avatar
Vladislav Rykov committed
29
@app.route('/')
30
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
31
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
32
33
34
35
36
37
        created_apps = ad.get_count_by_user(session['name'])[1][0]
        active_devices = dd.get_count_by_user(session['name'])
        total_activity = md.get_user_data_count(session['name'])[1][0]
        last_activity = md.get_user_data_count_per_day(session['name'])[1][0]
        info = [created_apps, active_devices, total_activity, last_activity]

38
39
40
41
42
        return render_template('new/public/dashboard.html', info=info)
        
    else:
        return render_template('new/public/login.html', users_signup=app.config['USERS_SIGNUP'])

Vladislav Rykov's avatar
Vladislav Rykov committed
43
@app.route('/register', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
44
def register():
45
    if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
46
        if 'role' in session and session['role'] == 'admin':
Vladislav Rykov's avatar
Vladislav Rykov committed
47
            return render_template('old/admin/signup.html', users_signup=app.config['USERS_SIGNUP'])
Vladislav Rykov's avatar
Vladislav Rykov committed
48
        else:
49
            if app.config['USERS_SIGNUP']:
Vladislav Rykov's avatar
Vladislav Rykov committed
50
                return render_template('new/public/register.html', users_signup=app.config['USERS_SIGNUP'])
51
            else:
52
                return redirect(url_for('index', users_signup=app.config['USERS_SIGNUP']))
53
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
54
        if app.config['USERS_SIGNUP'] or ('role' in session and session['role'] == 'admin'):
55
56
57
58
            username = request.form['username']
            password = request.form['password'].encode('utf-8')
            
            if (username == '' or password == ''):
Vladislav Rykov's avatar
Vladislav Rykov committed
59
60
                flash('Username or password fields cannot be empty', 'danger')
                return redirect(url_for('register', users_signup=app.config['USERS_SIGNUP']))
61
62
            elif (len(password) < 8):
                flash('Password length must be at least 8 characters.', 'danger')
Vladislav Rykov's avatar
Vladislav Rykov committed
63
                return redirect(url_for('register', users_signup=app.config['USERS_SIGNUP']))
Vladislav Rykov's avatar
Vladislav Rykov committed
64
            else:
65
                role = 'user'
Vladislav Rykov's avatar
Vladislav Rykov committed
66
                if 'role' in request.form and request.form['role'] == 'administrator':
67
68
69
70
71
72
                    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
73
                else:
74
75
76
                    session['name'] = username
                    
                    flash('User successfully created.', 'success')
77

Vladislav Rykov's avatar
Vladislav Rykov committed
78
                    if 'role' in session and session['role'] == 'admin':
Vladislav Rykov's avatar
Vladislav Rykov committed
79
                        return redirect(url_for('administration'))
80
81
82
83
                    else:
                        return redirect(url_for('index'))
        else:
            return redirect(url_for('index'))
84
85


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

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


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


Vladislav Rykov's avatar
Vladislav Rykov committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@app.route('/applications')
def applications():
    if 'name' in session:
        apps = ad.get_list(session['name'])
       
        session.pop('appkey', None)
        
        return render_template('new/public/applications.html', apps=apps[1])
    else:
        return redirect(url_for('login', users_signup=app.config['USERS_SIGNUP']))


@app.route('/application/<appkey>')
def application(appkey):
    if 'name' in session:    
        ap = list(ad.get(appkey)[1])
        ap[5] = misc.skey_b64_to_hex(ap[5])
        devs = dd.get_list(ap[1])[1]

        return render_template('new/public/application.html', app=ap, devs=devs)
    else:
        return redirect(url_for('login', users_signup=app.config['USERS_SIGNUP']))


139
140
141
142
143
144
145
146
147
148
149
150
151
152
@app.route('/new-application', methods=['GET', 'POST'])
def new_application():
    if 'name' in session:
        if request.method == 'GET':
            return render_template('new/public/new-application.html')
        elif request.method == 'POST':
            if request.form['appname'] == '':
                flash('Application name cannot be empty.', 'danger')
                return render_template(request.url)
            else:
                appkey = misc.rand_str(app.config['APPKEY_LENGTH']).decode('utf-8')
                secure_key = misc.gen_skey_b64(16)
                secure = False

Vladislav Rykov's avatar
Vladislav Rykov committed
153
                if request.form.getlist('secure') and request.form.getlist('secure')[0] == 'on':
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                    secure = True

                res = ad.create(request.form['appname'], appkey, session['name'], request.form['appdesc'], secure, secure_key)
            
                if not res[0]:
                    flash('Error: {}'.format(res[1]), 'danger')
                    return render_template(request.url)
            
                res = dd.create_table(appkey)
            
                if not res[0]:
                    ad.delete(appkey)
                    flash('Error: {}'.format(res[1]), 'danger')
                    return render_template(request.url)
            
                return redirect(url_for('applications'))
    else:
        return redirect(url_for('login', users_signup=app.config['USERS_SIGNUP']))


Vladislav Rykov's avatar
Vladislav Rykov committed
174
@app.route('/application/<appkey>/device/<devid>')
Vladislav Rykov's avatar
Vladislav Rykov committed
175
def application_device(appkey, devid):
Vladislav Rykov's avatar
Vladislav Rykov committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    if 'name' in session:
        ap = ad.get(appkey)
        if session['role'] == 'admin' or session['name'] == ap[1][2]:
            dev = dd.get(appkey, devid)

            session['devid'] = int(dev[1][1])
            session['devname'] = dev[1][0]
    
            ld = data.get_last_range(appkey, devid, [MAX_PG_ENTRIES_DATA, 0])
            cnt = data.get_count(appkey, devid)
    
            print(ld)

            ltup = 'Device have not any sent data yet'

            if ld[0] and ld[1][0] != []:
                ltup = ld[1][0][1]

Vladislav Rykov's avatar
Vladislav Rykov committed
194
195
196
197
            if ld[0]: 
                return render_template('new/public/device.html', dev=dev[1], app=ap[1], ltup=ltup, data=ld[1], total=cnt[1][0])
            else:
                return render_template('new/public/device.html', dev=dev[1], app=ap[1], ltup=ltup, data=[], total=cnt[1][0])
Vladislav Rykov's avatar
Vladislav Rykov committed
198
199
200
    else:
        return redirect(url_for('login'))

Vladislav Rykov's avatar
Vladislav Rykov committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@app.route('/application/<appkey>/add-device', methods=['GET', 'POST'])
def application_add_device(appkey):
    if 'name' in session:
        if request.method == 'GET':
            ap = ad.get(appkey)
            dev_list = dd.get_list(appkey)
            return render_template('new/public/add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1]))
        elif request.method == 'POST':
            res = dd.create(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'])

            if not res[0]:
                flash('Error: {}'.format(res[1]), 'danger')
                return render_template(request.url)
            else:
                res = data.create_table(appkey, request.form['devid'])
            
                if not res[0]:
                    dd.delete(session['appkey'], request.form['devid'])
                    flash('Error: {}'.format(res[1]), 'danger')
                    return render_template(request.url)
                else:
                    return redirect(url_for('applications'))
    else:
        return redirect(url_for('login'))
225

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

@app.route('/application/<appkey>/device/<devid>/configure', methods=['GET', 'POST'])
def application_device_configuration(appkey, devid):
    if 'name' in session:
        if request.method == 'GET':
            pend_msgs = pend.get_list(appkey, devid)
            ap = ad.get(appkey)[1]
            dev = dd.get(appkey, devid)[1]
            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]))
243
            
244
            return render_template('new/public/device-configuration.html', dev=dev, app=ap, config_list=config_list)
245
        elif request.method == 'POST':
246
247
            base64_args = misc.pend_base64_encode(request.form['arg'], request.form['confid'])
            pend.create(appkey, devid, base64_args)
248
            
249
            flash('Message enqueued', 'success')
250
            return '', 201
251
252
253
254
    else:
        return redirect(url_for('login'))


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@app.route('/application/<appkey>/device/<devid>/download-csv')
def application_device_download_csv(appkey, devid):
    if 'name' in session:
        dumpd = data.get_all(appkey, devid)
        ap = ad.get(appkey)[1]
        dev = dd.get(appkey, devid)[1]

        fn = ap[0]+ '-' +dev[0]+ '-data.csv'

        with open(app.config['DATA_DOWNLOAD_DIR_OS']+'/'+fn, 'w+') as f: 
            f.write('utc,timestamp,')
            for d in dumpd[1][0][2]:
                f.write(d)
                f.write(',')
            f.write('\n')
        
            for row in dumpd[1]:
                f.write('{},{},'.format(row[0],row[1]))
                for v in row[2]:
                    f.write(str(row[2][v]))
                    f.write(',')
                f.write('\n')
    
        return send_from_directory(app.config['DATA_DOWNLOAD_DIR'], fn, as_attachment=True)
    else:
        return redirect(utl_for('login'))

282

Vladislav Rykov's avatar
Vladislav Rykov committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
@app.route('/chart-update')
def chart_update():
    if 'name' in session:
        day_chart_values = md.get_user_data_count_per_hour_period(session['name'], 11)[1]
        day_chart_values = [x[0] for x in day_chart_values]
        day_chart_labels = [misc.local_hour(x) for x in range(11,-1,-1)]
        day_chart = [day_chart_labels, day_chart_values]

        week_chart_values = md.get_user_data_count_per_day_period(session['name'], 6)[1]
        week_chart_values = [x[0] for x in week_chart_values]
        week_chart_labels = [misc.local_weekday(x) for x in range(6,-1,-1)]
        week_chart = [week_chart_labels, week_chart_values]
        
        return "[{}, {}]".format(day_chart, week_chart)
    else:
        return '', 401

@app.route('/recent-activity')
def recent_activity():
    if 'name' in session:
        recent_activity = md.get_recent_activity(session['name'])[1]
        ra = ''
        
        for r in recent_activity:
            ra += '<tr><th scope="row">'+r[1]+'</th><th>'+r[2]+'</th><th>'+r[0]+'</th><th>'+str(r[3])+'</th></tr>'

        return ra, 200
    else:
        return '', 401


Vladislav Rykov's avatar
Vladislav Rykov committed
314
@app.route('/new-app')
315
def new_app():
Vladislav Rykov's avatar
Vladislav Rykov committed
316
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
317
        return render_template('old/public/new-app.html')
Vladislav Rykov's avatar
Vladislav Rykov committed
318
319
    else:
        return redirect(url_for('index'))
320
321


Vladislav Rykov's avatar
Vladislav Rykov committed
322
323
@app.route('/app', methods=['GET', 'POST'])
def app_():
Vladislav Rykov's avatar
Vladislav Rykov committed
324
325
326
    if 'name' in session:
        if request.method == 'GET':
            session['appkey'] = request.args.get('appkey')
Vladislav Rykov's avatar
Vladislav Rykov committed
327

Vladislav Rykov's avatar
Vladislav Rykov committed
328
            ap = ad.get(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
329
            print(ap)
Vladislav Rykov's avatar
Vladislav Rykov committed
330
            devs = dd.get_list(ap[1][1])
Vladislav Rykov's avatar
Vladislav Rykov committed
331
            print(devs)
332
            session['appname'] = ap[1][0]
333
334
            
            if session['role'] == 'admin' or session['name'] == ap[1][2]:
Vladislav Rykov's avatar
Vladislav Rykov committed
335
                return render_template('old/public/app.html', app=ap[1], devs=devs[1])
336
337
            else:
                return redirect(url_for('index'))
338
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
339
340
            if request.form['appname'] == '':
                error = 'Application name cannot be empty.'
Vladislav Rykov's avatar
Vladislav Rykov committed
341
                return render_template('old/public/new-app.html', feedback=error)
Vladislav Rykov's avatar
Vladislav Rykov committed
342
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
343
                appkey = misc.rand_str(app.config['APPKEY_LENGTH']).decode('utf-8')
344
345
346
347
348
349
350
                secure_key = misc.gen_skey_b64(16)
                secure = False

                if request.form.getlist('secure') and request.form.getlist('secure')[0] == 'true':
                    secure = True

                res = ad.create(request.form['appname'], appkey, session['name'], request.form['appdesc'], secure, secure_key)
Vladislav Rykov's avatar
Vladislav Rykov committed
351
            
Vladislav Rykov's avatar
Vladislav Rykov committed
352
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
353
                    return render_template('old/public/new-app.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
354
            
Vladislav Rykov's avatar
Vladislav Rykov committed
355
                res = dd.create_table(appkey)
356
            
Vladislav Rykov's avatar
Vladislav Rykov committed
357
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
358
                    ad.delete(appkey)
Vladislav Rykov's avatar
Vladislav Rykov committed
359
                    return render_template('old/public/new-app.html', feedback=res[1])
360
            
Vladislav Rykov's avatar
Vladislav Rykov committed
361
362
363
                return redirect(url_for('index'))
    else:
        return redirect(url_for('index'))
364

Vladislav Rykov's avatar
Vladislav Rykov committed
365
@app.route('/delete-app')
366
def delete_app():
Vladislav Rykov's avatar
Vladislav Rykov committed
367
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
368
        devs = dd.get_list(session['appkey'])
369
    
Vladislav Rykov's avatar
Vladislav Rykov committed
370
371
        for dev in devs[1]:
            data.delete_table(session['appkey'], dev[1])
372
    
Vladislav Rykov's avatar
Vladislav Rykov committed
373
        dd.delete_table(session['appkey'])
374
    
Vladislav Rykov's avatar
Vladislav Rykov committed
375
        res = ad.delete(session['appkey'])
376
    
Vladislav Rykov's avatar
Vladislav Rykov committed
377
        if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
378
            return redirect(url_for('app_'))
Vladislav Rykov's avatar
Vladislav Rykov committed
379
380
        else:
            return redirect(url_for('index'))
381
382
    else:
        return redirect(url_for('index'))
383

Vladislav Rykov's avatar
Vladislav Rykov committed
384

Vladislav Rykov's avatar
Vladislav Rykov committed
385
@app.route('/add-dev')
Vladislav Rykov's avatar
Vladislav Rykov committed
386
def new_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
387
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
388
        dev_list = dd.get_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
389
    
Vladislav Rykov's avatar
Vladislav Rykov committed
390
        if not dev_list[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
391
            return render_template('old/public/add-dev.html', feedback=dev_list[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
392
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
393
            return render_template('old/public/add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
394
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
395
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
396
397
398
 


Vladislav Rykov's avatar
Vladislav Rykov committed
399
@app.route('/dev', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
400
def dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
401
402
    if 'name' in session:
        if request.method == 'GET':
403
404
405
406
407
408
            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
409
        
410
                last = data.get_last_n(session['appkey'], session['devid'], 1)
Vladislav Rykov's avatar
Vladislav Rykov committed
411
        
412
                ltup = 'Device have not sent data yet'
Vladislav Rykov's avatar
Vladislav Rykov committed
413

414
415
                if last[0]:
                    ltup = last[1][0][1]
416

Vladislav Rykov's avatar
Vladislav Rykov committed
417
                return render_template('old/public/dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
418
419
            else:
                return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
420
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
421
            res = dd.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
422

423
            if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
424
                return render_template('old/public/add-dev.html', feedback=res[1])
425
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
426
427
428
                res = data.create_table(session['appkey'], request.form['devid'])
            
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
429
                    dd.delete(session['appkey'], request.form['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
430
                    return render_template('old/public/add-dev.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
431
                else:
432
                    return redirect(url_for('app_', appkey=session['appkey']))
Vladislav Rykov's avatar
Vladislav Rykov committed
433
434
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
435
436


Vladislav Rykov's avatar
Vladislav Rykov committed
437
@app.route('/dev-conf', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
438
def dev_conf():
Vladislav Rykov's avatar
Vladislav Rykov committed
439
440
    if 'name' in session and 'devid' in session:
        if request.method == 'GET':
441
442
443
444
445
446
447
448
449
450
451
452
            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]))

Vladislav Rykov's avatar
Vladislav Rykov committed
453
                return render_template('old/public/dev-conf.html', devname=session['devname'], config_list=config_list)
454
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
455
                return render_template('old/public/dev-conf.html', devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
456
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
457
            base64_args = pend_base64_encode(request.form['arg'], request.form['confid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
458
            pend.create(session['appkey'], session['devid'], base64_args)
Vladislav Rykov's avatar
Vladislav Rykov committed
459

Vladislav Rykov's avatar
Vladislav Rykov committed
460
461
462
            return redirect(url_for('dev', id=session['devid']))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
463

Vladislav Rykov's avatar
Vladislav Rykov committed
464
465
466
467
@app.route('/application/<appkey>/device/<devid>/remove-configuration')
def dev_conf_rm(appkey, devid):
    if 'name' in session:
        res = pend.delete(appkey, devid, request.args.get('conf')+'_')
468
469
470
471
472

        if res[0]:
            flash('Configuration message successfully removed.','success')
        else:
            flash('Error removing configuration message: {}'.format(res[1]), 'danger')
473
474
        
        return '', 200
475
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
476
        return redirect(url_for('login'))
Vladislav Rykov's avatar
Vladislav Rykov committed
477

Vladislav Rykov's avatar
Vladislav Rykov committed
478

479
480
481
482
483
484
485
486
487
488
489
490
491
492
@app.route('/application/<appkey>/device/<devid>/variables')
def application_device_variables(appkey, devid):
    if 'name' in session:
        last = data.get_last_n(appkey, devid, 1)
        if last[0]:
            select = '<select class="form-control" id="varname" name="varname" onchange="validate_form();" required>'
            select += '<option value="-">Select Variable</option>'
            for k in last[1][0][2]:
                select += '<option>'+k+'</option>'
            select += '</select>'
            return select
    else:
        return redirect(url_for('login'))

Vladislav Rykov's avatar
Vladislav Rykov committed
493
@app.route('/delete-dev')
Vladislav Rykov's avatar
Vladislav Rykov committed
494
def delete_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
495
496
    if 'name' in session and 'devid' in session:
        data.delete_table(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
497
        res = dd.delete(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
498

Vladislav Rykov's avatar
Vladislav Rykov committed
499
        return redirect(url_for('app_', appkey=session['appkey']))
Vladislav Rykov's avatar
Vladislav Rykov committed
500
501
    else:
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
502
503


Vladislav Rykov's avatar
Vladislav Rykov committed
504
@app.route('/dev-data')
505
506
507
508
509
510
511
512
513
514
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
515
516
517
518
        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)
519

Vladislav Rykov's avatar
Vladislav Rykov committed
520
            if ent_cnt[1][0] > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
521
                return render_template('old/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
522
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
523
                return render_template('old/public/dev-data-t.html', devname=session['devname'])
524
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
525
            flash('Error: {}'.format(ent_cnt[1]), 'danger')
Vladislav Rykov's avatar
Vladislav Rykov committed
526
            return render_template('old/public/dev-data-t.html', devname=session['devname'])
527
528
529
530
    else:
        return redirect(utl_for('index'))


Vladislav Rykov's avatar
Vladislav Rykov committed
531
532
533
534
535
@app.route('/dev-vars')
def dev_vars():
    if 'name' in session:
        last = data.get_last_n(session['appkey'], request.args.get('id'), 1)
        if last[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
536
            select = '<select class="form-control notifelem" id="varname" name="varname" onchange="onvar(event)" required>'
Vladislav Rykov's avatar
Vladislav Rykov committed
537
538
539
540
541
542
543
544
            select += '<option value="-">Select Variable</option>'
            for k in last[1][0][2]:
                select += '<option>'+k+'</option>'
            select += '</select>'
            return select
    else:
        return redirect(url_for('index'))

545

Vladislav Rykov's avatar
Vladislav Rykov committed
546
@app.route('/data-csv')
Vladislav Rykov's avatar
Vladislav Rykov committed
547
def data_csv():
Vladislav Rykov's avatar
Vladislav Rykov committed
548
549
    if 'name' in session and 'devid' in session:
        dumpd = data.get_all(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
550

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

553
        with open(app.config['DATA_DOWNLOAD_DIR_OS']+'/'+fn, 'w+') as f: 
Vladislav Rykov's avatar
Vladislav Rykov committed
554
            f.write('utc,timestamp,')
Vladislav Rykov's avatar
Vladislav Rykov committed
555
556
            for d in dumpd[1][0][2]:
                f.write(d)
Vladislav Rykov's avatar
Vladislav Rykov committed
557
558
                f.write(',')
            f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
559
560
        
            for row in dumpd[1]:
Vladislav Rykov's avatar
Vladislav Rykov committed
561
                f.write('{},{},'.format(row[0],row[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
562
563
564
565
                for v in row[2]:
                    f.write(str(row[2][v]))
                    f.write(',')
                f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
566
    
Vladislav Rykov's avatar
Vladislav Rykov committed
567
        return send_from_directory(app.config['DATA_DOWNLOAD_DIR'], fn, as_attachment=True)
Vladislav Rykov's avatar
Vladislav Rykov committed
568
569
    else:
        return redirect(utl_for('index'))
570
571


Vladislav Rykov's avatar
Vladislav Rykov committed
572
573
@app.route('/administration', methods=['GET', 'POST'])
def administration():
Vladislav Rykov's avatar
Vladislav Rykov committed
574
575
576
577
578
579
580
581
582
583
584
    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
585
        users = None
Vladislav Rykov's avatar
Vladislav Rykov committed
586
        
Vladislav Rykov's avatar
Vladislav Rykov committed
587
588
        if request.method == 'POST':
            session['users_filter'] = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
589
            
Vladislav Rykov's avatar
Vladislav Rykov committed
590
591
592
        if 'users_filter' in session:
            users = ud.get_range_name(session['users_filter'], [MAX_PG_ENTRIES_USERS, (cur_pg-1)*MAX_PG_ENTRIES_USERS])
            rd = misc.paging(cur_pg, len(users[1]), MAX_PG_ENTRIES_USERS, MAX_PG)
Vladislav Rykov's avatar
Vladislav Rykov committed
593
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
594
595
596
            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)
        
Vladislav Rykov's avatar
Vladislav Rykov committed
597
        return render_template('old/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
598
599
600
    else:
        return redirect(url_for('index'))

Vladislav Rykov's avatar
Vladislav Rykov committed
601
602
603
604
605

@app.route('/dashboard-clean-search')
def dashboard_clean_search():
    if 'users_filter' in session:
        session.pop('users_filter', None)
Vladislav Rykov's avatar
Vladislav Rykov committed
606
    return redirect(url_for('administration'))
Vladislav Rykov's avatar
Vladislav Rykov committed
607

608
609
610
611
612
613
614
615
616

@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)
        if apps[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
617
            return render_template('old/admin/user.html', apps=apps[1], username=name)
618
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
619
            return render_template('old/admin/user.html', feedback=apps[1], username=name)
620
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
621
        return render_template('old/public/index.html')
622
623
624
625
626


@app.route('/user-delete')
def user_delete():
    user = ud.get(request.args.get('name'))
Vladislav Rykov's avatar
Vladislav Rykov committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    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])
                for dev in devs[1]:
                    res = data.delete_table(app[1], dev[1])
                    if not res[0]:
                        break
    
                if res[0]:
                    res = dd.delete_table(app[1])
                    
                if res[0]:
                    res = ad.delete(app[1])

                if not res[0]:
                    break

        if res[0]:
            res = ud.delete(user[1][0])

        if not res[0]:
            flash('Error: {}'.format(res[1]), 'danger')
Vladislav Rykov's avatar
Vladislav Rykov committed
653
            return render_template('old/admin/user.html', username=user[1][0])
Vladislav Rykov's avatar
Vladislav Rykov committed
654
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
655
                return redirect(url_for('administraion'))
Vladislav Rykov's avatar
Vladislav Rykov committed
656
657
658
    else:
        flash('Warning: the user is admin or does not exist.' ,'warning')
        return redirect(url_for('index'))
659

Vladislav Rykov's avatar
Vladislav Rykov committed
660

661
662
663
664
@app.route('/settings', methods=['GET', 'POST'])
def settings():
    if request.method == 'GET':
        if session['role'] == 'admin':
Vladislav Rykov's avatar
Vladislav Rykov committed
665
            return render_template('old/admin/settings.html', username=session['name'], users_signup=app.config['USERS_SIGNUP'])
666
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
667
            return render_template('old/public/settings.html', username=session['name'])
668
669
670
671
672
673
674
675
676
677
678
679
    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')
680
681
682
683
684
685
                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
686
687


688
        return redirect(request.url)
689
690


691
692
@app.route('/dev-data/<var>/<dest>/<page>')
def dev_data(var, dest, page):
693
    if dest == 'graph':
694
        last = data.get_last_hours(session['appkey'], session['devid'], MAX_PG_ENTRIES_GRAPH_HOURS, int(page))
695
696
697
698
699
700
701
702
        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
703
        last = data.get_last_range(session['appkey'], session['devid'], [MAX_PG_ENTRIES_DATA, (int(page)-1)*MAX_PG_ENTRIES_DATA])
704
705
706
707
708
709
710
        #t = """ <thead>
        #            <th>Time</th>
        #            <th>{}</th>
        #        </thead>
        #        <tbody>
        #""".format(var)
        t = ''
711
712
713
        if last[0]:
            for d in last[1]:
                t += '<tr><th>'+d[1]+'</th><th>'+str(d[2][var])+'</th></tr>'
714
        #t += '</tbody>'
715
        return t
716
717


Vladislav Rykov's avatar
Vladislav Rykov committed
718
@app.route('/application/<appkey>/device/<devid>/data/<var>/<dest>/<page>')
719
def application_device_data(appkey, devid, var, dest, page):
Vladislav Rykov's avatar
Vladislav Rykov committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
    if dest == 'graph':
        last = data.get_last_hours(appkey, devid, MAX_PG_ENTRIES_GRAPH_HOURS, int(page))
        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
        last = data.get_last_range(appkey, devid, [MAX_PG_ENTRIES_DATA, (int(page)-1)*MAX_PG_ENTRIES_DATA])
        t = ''
        if last[0]:
            for d in last[1]:
                t += '<tr><th>'+d[1]+'</th><th>'+str(d[2][var])+'</th></tr>'
        return t


738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
@app.route('/application/<appkey>/alerts')
def application_alerts(appkey):
    if 'name' in session:
        ap = ad.get(appkey)
        alerts = nfs.get_alerts_list(appkey)
        print(alerts)
        return render_template('new/public/alerts.html', alert_list=alerts[1], app=ap[1])
    else:
        return redirect(url_for('login'))


@app.route('/application/<appkey>/new-alert', methods=['GET', 'POST'])
def application_new_alert(appkey):
    if 'name' in session:
        if request.method == 'GET':
            ap = ad.get(appkey)
            devs = dd.get_list(appkey)
            
            return render_template('new/public/new-alert.html', devs=devs[1], app=ap[1])
        elif request.method == 'POST':
            # create new notification
            nid = misc.rand_str(app.config['NID_LENGTH']).decode('utf-8')
            dev = dd.get(appkey, request.form['devid'])
            
            try:
                desc = dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']
                res = nfs.create(nid, appkey, request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail'])
                if res[0]:
                    # create new function and trigger
                    tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
                    tr.create(appkey, request.form['devid'], nid)
                    flash('Alert created', 'success')
                    return redirect(url_for('application_alerts', appkey=appkey))
                else:
                    flash('Error creating new alert: {}'.format(res[1]), 'danger')
                    return redirect(request.url) 
            except Exception as e:
                flash('Error creating new alert: {}. Make sure you have filled all form fields.'.format(e), 'danger')
                return redirect(request.url) 
    else:
        return redirect(url_for('login'))


@app.route('/application/<appkey>/remove-alert')
def application_alert_remove(appkey):
    if 'name' in session:
        nq.delete(appkey, request.args.get('devid'), request.args.get('id'))
        tr.delete(appkey, request.args.get('devid'), request.args.get('id'))
        tr.delete_function(appkey, request.args.get('devid'), request.args.get('id'))
        res = nfs.delete(appkey, request.args.get('devid'), request.args.get('id'))

        if res[0]:
            flash('Alert removed', 'success')
            return '', 200
        else:
            flash('Alert cannot be removed : {}'.format(res[1]), 'danger')
            return '', 500
    else:
        return redirect(url_for('login'))


Vladislav Rykov's avatar
Vladislav Rykov committed
799
800
801
802
803
804
805
806
807
808
809
@app.route('/application/<appkey>/automation')
def application_automation(appkey):
    if 'name' in session:
        ap = ad.get(appkey)
        ats = nfs.get_automation_list(appkey)
        
        return render_template('new/public/automation.html', automations=ats[1], app=ap[1])
    else:
        return redirect(url_for('login'))


Vladislav Rykov's avatar
Vladislav Rykov committed
810
811
812
@app.route('/alerts')
def alerts():
    if 'name' in session:
813
        alerts = nfs.get_alerts_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
814
        return render_template('old/public/alerts.html', alert_list=alerts[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
815
816
817
818
819
820
821
    else:
        return redirect(url_for('index'))

@app.route('/new-alert')
def new_alert():
    if 'name' in session:
        devs = dd.get_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
822
        return render_template('old/public/new-alert.html', devs=devs[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
823
824
825
826
827
828
    else:
        return redirect(url_for('index'))


@app.route('/alert', methods=['POST'])
def alert():
829
830
831
832
833
    if 'name' in session:
        if request.method == 'POST':
            # create new notification
            nid = misc.rand_str(app.config['NID_LENGTH']).decode('utf-8')
            dev = dd.get(session['appkey'], request.form['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
            
            try:
                desc = dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']
                res = nfs.create(nid, session['appkey'], request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail'])
                if res[0]:
                    # create new function and trigger
                    tr.create_function(session['appkey'], request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
                    tr.create(session['appkey'], request.form['devid'], nid)
                    return redirect(url_for('alerts'))
                else:
                    flash('Error creating new alert: {}'.format(res[1]), 'danger')
                    return redirect(url_for('alerts'))
            except Exception as e:
                flash('Error creating new alert: {}. Make sure you have filled all form fields.'.format(e), 'danger')
                return redirect(url_for('new_alert'))
849
850
851
852
        else:
            return redirect(url_for('index'))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
853
854
855
856

@app.route('/alert-rm')
def alarm_rm():
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
857
        nq.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
Vladislav Rykov's avatar
Vladislav Rykov committed
858
859
        tr.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
        tr.delete_function(session['appkey'], request.args.get('devid'), request.args.get('id'))
860
        res = nfs.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
Vladislav Rykov's avatar
Vladislav Rykov committed
861
862

        if res[0]:
863
            flash('Alert removed', 'success')
Vladislav Rykov's avatar
Vladislav Rykov committed
864
865
            return redirect(url_for('alerts'))
        else:
866
            flash('Alert cannot be removed : {}'.format(res[1]), 'danger')
Vladislav Rykov's avatar
Vladislav Rykov committed
867
            return redirect(url_for('alerts'))
Vladislav Rykov's avatar
Vladislav Rykov committed
868
869
870
    else:
        return redirect(url_for('index'))

871
872
873
874
875
876

@app.route('/automation', methods=['GET','POST'])
def automation():
    if 'name' in session:
        if request.method == 'GET':
            auto = nfs.get_automation_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
877
            return render_template('old/public/automation.html', auto_list=auto[1])
878
879
        elif request.method == 'POST':
            # new automation
Vladislav Rykov's avatar
Vladislav Rykov committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
            nid = misc.rand_str(app.config['NID_LENGTH']).decode('utf-8')
            dev = dd.get(session['appkey'], request.form['devid'])
            adev = dd.get(session['appkey'], request.form['adevid'])
            try:
                desc = 'IF '+dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']+' THEN '+adev[1][0]+'.confID_'+request.form['confid']+' = '+request.form['arg']
                # action format: '<devid>#<confid>#<arg>'
                action = request.form['adevid']+'#'+request.form['confid']+'#'+request.form['arg']
                res = nfs.create(nid, session['appkey'], request.form['devid'], request.form['automationname'], desc, 'automation', action)
                if res[0]:
                    # create new function and trigger
                    tr.create_function(session['appkey'], request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
                    tr.create(session['appkey'], request.form['devid'], nid)
                    return redirect(url_for('automation'))
                else:
                    flash('Error creating new alert: {}'.format(res[1]), 'danger')
                    return redirect(url_for('automation'))
            
                return redirect(url_for('autmation'))
            except Exception as e:
                flash('Error creating new automation: {}. Make sure you have filled all form fields correctly.'.format(e), 'danger')
                return redirect(url_for('automation'))

902
903
904
905
906
907
908
909
    else:
        return redirect(url_for('index'))


@app.route('/new-automation')
def new_automation():
    if 'name' in session:
        devs = dd.get_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
910
        return render_template('old/public/new-automation.html', devs=devs[1])
911
912
913
914
    else:
        return redirect(url_for('index'))


Vladislav Rykov's avatar
Vladislav Rykov committed
915
@app.route('/automation-rm')
Vladislav Rykov's avatar
Vladislav Rykov committed
916
def automation_rm():
Vladislav Rykov's avatar
Vladislav Rykov committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
    if 'name' in session:
        nq.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
        tr.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))
        tr.delete_function(session['appkey'], request.args.get('devid'), request.args.get('id'))
        res = nfs.delete(session['appkey'], request.args.get('devid'), request.args.get('id'))

        if res[0]:
            flash('Automation removed', 'success')
            return redirect(url_for('alerts'))
        else:
            flash('Automation cannot be removed : {}'.format(res[1]), 'danger')
            return redirect(url_for('alerts'))
    else:
        return redirect(url_for('index'))




935
936
def pend_delete_all_ack():
    pend.delete_all_ack()
Vladislav Rykov's avatar
Vladislav Rykov committed
937

938
939
940
941
942
943
944
945
946
def fire_notifications(app):
    fnfs = nq.get_all()
    if fnfs[0]:
        for fnf in fnfs[1]:
            nf = nfs.get(fnf[1], fnf[2], fnf[0])
            if nf[1][5] == 'alert':
                # send mail
                mailer.send_mail(app, nf[1], fnf)
            elif nf[1][5] == 'automation':
Vladislav Rykov's avatar
Vladislav Rykov committed
947
948
949
950
951
952
                # enqueue confid
                # action format: '<devid>#<confid>#<arg>'
                action = nf[1][6].split('#')
                base64_args = misc.pend_base64_encode(action[2], action[1])
                pend.create(nf[1][1], action[0], base64_args)
            nq.delete(fnf[1], fnf[2], fnf[0])