views.py 9.21 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 os
Vladislav Rykov's avatar
Vladislav Rykov committed
15

16

Vladislav Rykov's avatar
Vladislav Rykov committed
17
@app.route('/')
18
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
19
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
20
21
        apps = ad.get_list(session['name'])
        
Vladislav Rykov's avatar
Vladislav Rykov committed
22
        session.pop('appkey', None)
Vladislav Rykov's avatar
Vladislav Rykov committed
23
        # print('apps: ', apps)
24
        if apps[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
25
            return render_template('public/index.html', apps=apps[1])
26
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
27
            return render_template('public/index.html', feedback=apps[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
28
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
29
        return render_template('public/index.html')
30
31
32



Vladislav Rykov's avatar
Vladislav Rykov committed
33
@app.route('/signup', methods=['GET', 'POST'])
34
35
def signup():
    if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
36
        return render_template('public/signup.html')
37
38
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
39
        password = request.form['password'].encode('utf-8')
40
41
42

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
Vladislav Rykov's avatar
Vladislav Rykov committed
43
            return render_template('public/signup.html', feedback=feedback)
Vladislav Rykov's avatar
Vladislav Rykov committed
44
45
46
        elif (len(password) < 8):
            flash('Password length must be at least 8 characters.', 'danger')
            return redirect(request.url)
47
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
48
            res = ud.create(username, password)
Vladislav Rykov's avatar
Vladislav Rykov committed
49
            if (not res[0]):
Vladislav Rykov's avatar
Vladislav Rykov committed
50
51
                flash('Error: {}'.format(res[1]), 'danger')
                return redirect(request.url)
Vladislav Rykov's avatar
Vladislav Rykov committed
52
53
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
54
55
                
                flash('User successfully created.', 'success')
Vladislav Rykov's avatar
Vladislav Rykov committed
56
                return redirect(url_for('index'))
57

58
59


Vladislav Rykov's avatar
Vladislav Rykov committed
60
@app.route('/login', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
61
def login():
Vladislav Rykov's avatar
Vladislav Rykov committed
62
    if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
63
        return render_template('public/login.html')
Vladislav Rykov's avatar
Vladislav Rykov committed
64
65
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
66
        password = request.form['password'].encode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
67
68

        if (username == '' or password == ''):
Vladislav Rykov's avatar
Vladislav Rykov committed
69
70
            flash('Username or password fields cannot be empty', 'danger')
            return redirect(request.url)
Vladislav Rykov's avatar
Vladislav Rykov committed
71
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
72
            res = ud.get(username, password)
Vladislav Rykov's avatar
Vladislav Rykov committed
73
            if (not res[0]):
Vladislav Rykov's avatar
Vladislav Rykov committed
74
75
                flash('Error: {}'.format(res[1]), 'danger')
                return redirect(request.url)
Vladislav Rykov's avatar
Vladislav Rykov committed
76
77
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
78
        
Vladislav Rykov's avatar
Vladislav Rykov committed
79
80
81
                return redirect(url_for('index'))


82

Vladislav Rykov's avatar
Vladislav Rykov committed
83
@app.route('/logout')
Vladislav Rykov's avatar
Vladislav Rykov committed
84
85
86
def logout():
    session.clear()
    return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
87
88


89

Vladislav Rykov's avatar
Vladislav Rykov committed
90
@app.route('/new-app')
91
def new_application():
Vladislav Rykov's avatar
Vladislav Rykov committed
92
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
93
        return render_template('public/new-app.html')
Vladislav Rykov's avatar
Vladislav Rykov committed
94
95
    else:
        return redirect(url_for('index'))
96
97
98



Vladislav Rykov's avatar
Vladislav Rykov committed
99
100
@app.route('/app', methods=['GET', 'POST'])
def app_():
Vladislav Rykov's avatar
Vladislav Rykov committed
101
102
    if 'name' in session:
        if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
103
            
Vladislav Rykov's avatar
Vladislav Rykov committed
104
            session['appkey'] = request.args.get('appkey')
Vladislav Rykov's avatar
Vladislav Rykov committed
105

Vladislav Rykov's avatar
Vladislav Rykov committed
106
107
            ap = ad.get(session['appkey'])
            devs = dd.get_list(ap[1][1])
Vladislav Rykov's avatar
Vladislav Rykov committed
108
        
Vladislav Rykov's avatar
Vladislav Rykov committed
109
            try:
Vladislav Rykov's avatar
Vladislav Rykov committed
110
                filelist = [f for f in os.listdir(app.config['DATA_DOWNLOAD_DIR']) if f.startswith(session['appkey'])]
Vladislav Rykov's avatar
Vladislav Rykov committed
111
                for f in filelist:
Vladislav Rykov's avatar
Vladislav Rykov committed
112
                    os.remove(app.config['DATA_DOWNLOAD_DIR']+'/'+f)
Vladislav Rykov's avatar
Vladislav Rykov committed
113
114
115
116
            except OSError:
                pass

           # print('devs : ', devs)
Vladislav Rykov's avatar
Vladislav Rykov committed
117
            return render_template('public/app.html', app=ap[1], devs=devs[1])
118
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
119
120
            if request.form['appname'] == '':
                error = 'Application name cannot be empty.'
Vladislav Rykov's avatar
Vladislav Rykov committed
121
                return render_template('public/new-app.html', feedback=error)
Vladislav Rykov's avatar
Vladislav Rykov committed
122
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
123
                appkey = misc.rand_str(app.config['APPKEY_LENGTH'])
Vladislav Rykov's avatar
Vladislav Rykov committed
124
                res = ad.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
125
            
Vladislav Rykov's avatar
Vladislav Rykov committed
126
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
127
                    return render_template('public/new-app.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
128
            
Vladislav Rykov's avatar
Vladislav Rykov committed
129
                res = dd.create_table(appkey)
130
            
Vladislav Rykov's avatar
Vladislav Rykov committed
131
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
132
                    ad.delete(appkey)
Vladislav Rykov's avatar
Vladislav Rykov committed
133
                    return render_template('public/new-app.html', feedback=res[1])
134
            
Vladislav Rykov's avatar
Vladislav Rykov committed
135
136
137
                return redirect(url_for('index'))
    else:
        return redirect(url_for('index'))
138

Vladislav Rykov's avatar
Vladislav Rykov committed
139
@app.route('/delete-app')
140
def delete_app():
Vladislav Rykov's avatar
Vladislav Rykov committed
141
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
142
        devs = dd.get_list(session['appkey'])
143
    
Vladislav Rykov's avatar
Vladislav Rykov committed
144
145
        for dev in devs[1]:
            data.delete_table(session['appkey'], dev[1])
146
    
Vladislav Rykov's avatar
Vladislav Rykov committed
147
        dd.delete_table(session['appkey'])
148
    
Vladislav Rykov's avatar
Vladislav Rykov committed
149
        res = ad.delete(session['appkey'])
150
    
Vladislav Rykov's avatar
Vladislav Rykov committed
151
        if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
152
            return redirect(url_for('app_'))
Vladislav Rykov's avatar
Vladislav Rykov committed
153
154
        else:
            return redirect(url_for('index'))
155
156
    else:
        return redirect(url_for('index'))
157

Vladislav Rykov's avatar
Vladislav Rykov committed
158

Vladislav Rykov's avatar
Vladislav Rykov committed
159
@app.route('/add-dev')
Vladislav Rykov's avatar
Vladislav Rykov committed
160
def new_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
161
    if 'name' in session:
Vladislav Rykov's avatar
Vladislav Rykov committed
162
        dev_list = dd.get_list(session['appkey'])
Vladislav Rykov's avatar
Vladislav Rykov committed
163
    
Vladislav Rykov's avatar
Vladislav Rykov committed
164
    #print('dev list : ', dev_list)
Vladislav Rykov's avatar
Vladislav Rykov committed
165

Vladislav Rykov's avatar
Vladislav Rykov committed
166
        if not dev_list[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
167
            return render_template('public/add-dev.html', feedback=dev_list[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
168
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
169
            return render_template('public/add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
Vladislav Rykov's avatar
Vladislav Rykov committed
170
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
171
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
172
173
174
 


Vladislav Rykov's avatar
Vladislav Rykov committed
175
@app.route('/dev', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
176
def dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
177
178
    if 'name' in session:
        if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
179
            dev = dd.get(session['appkey'], request.args.get('id'))
Vladislav Rykov's avatar
Vladislav Rykov committed
180

Vladislav Rykov's avatar
Vladislav Rykov committed
181
            session['devid'] = int(dev[1][1])
Vladislav Rykov's avatar
Vladislav Rykov committed
182
            session['devname'] = dev[1][0]
Vladislav Rykov's avatar
Vladislav Rykov committed
183
        
Vladislav Rykov's avatar
Vladislav Rykov committed
184
            last = data.get_last_n(session['appkey'], session['devid'], 1)
Vladislav Rykov's avatar
Vladislav Rykov committed
185
        
Vladislav Rykov's avatar
Vladislav Rykov committed
186
            ltup = 'Device have not sent data yet'
Vladislav Rykov's avatar
Vladislav Rykov committed
187

Vladislav Rykov's avatar
Vladislav Rykov committed
188
189
            if last[0]:
                ltup = last[1][0][1]
190

Vladislav Rykov's avatar
Vladislav Rykov committed
191
            return render_template('public/dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
Vladislav Rykov's avatar
Vladislav Rykov committed
192
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
193
            res = dd.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
Vladislav Rykov's avatar
Vladislav Rykov committed
194

195
            if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
196
                return render_template('public/add-dev.html', feedback=res[1])
197
            else:
Vladislav Rykov's avatar
Vladislav Rykov committed
198
199
200
                res = data.create_table(session['appkey'], request.form['devid'])
            
                if not res[0]:
Vladislav Rykov's avatar
Vladislav Rykov committed
201
                    dd.delete(session['appkey'], request.form['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
202
                    return render_template('public/add-dev.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
203
204
205
206
                else:
                    return redirect(url_for('app', appkey=session['appkey']))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
207
208


Vladislav Rykov's avatar
Vladislav Rykov committed
209
@app.route('/dev-conf', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
210
def dev_conf():
Vladislav Rykov's avatar
Vladislav Rykov committed
211
212
    if 'name' in session and 'devid' in session:
        if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
213
            return render_template('public/dev-conf.html', devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
214
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
215
        
Vladislav Rykov's avatar
Vladislav Rykov committed
216
217
218
219
            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
220
        
Vladislav Rykov's avatar
Vladislav Rykov committed
221
222
223
224
225
            bstr = bytes(request.form['arg'])
            i = 0
            while i < argslen - 1:
                args[2+i] = bstr[i]
                i += 1
Vladislav Rykov's avatar
Vladislav Rykov committed
226

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

Vladislav Rykov's avatar
Vladislav Rykov committed
229
            pend.create(session['appkey'], session['devid'], base64_args)
Vladislav Rykov's avatar
Vladislav Rykov committed
230
231
232
233
234

        #print('msg = ', args)
        #print('base64 = ', base64_args)
        #print(type(request.form['arg'].encode('utf-8')))
        #print(request.form['arg'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
235
        
Vladislav Rykov's avatar
Vladislav Rykov committed
236
237
238
            return redirect(url_for('dev', id=session['devid']))
    else:
        return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
239

Vladislav Rykov's avatar
Vladislav Rykov committed
240

Vladislav Rykov's avatar
Vladislav Rykov committed
241
@app.route('/delete-dev')
Vladislav Rykov's avatar
Vladislav Rykov committed
242
def delete_dev():
Vladislav Rykov's avatar
Vladislav Rykov committed
243
244
    if 'name' in session and 'devid' in session:
        data.delete_table(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
245
        res = dd.delete(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
246

Vladislav Rykov's avatar
Vladislav Rykov committed
247
        return redirect(url_for('app_', appkey=session['appkey']))
Vladislav Rykov's avatar
Vladislav Rykov committed
248
249
    else:
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
250
251


Vladislav Rykov's avatar
Vladislav Rykov committed
252
@app.route('/dev-data')
Vladislav Rykov's avatar
Vladislav Rykov committed
253
def dev_data():
Vladislav Rykov's avatar
Vladislav Rykov committed
254
255
256
257
258
259
260
261
    if 'name' in session and 'devid' in session:
        last = data.get_last_n(session['appkey'], session['devid'], 10)  
        count = data.get_count(session['appkey'], session['devid'])

        last_ctr = 10
        if count[1][0] < 10:
            last_ctr = count[1][0]

Vladislav Rykov's avatar
Vladislav Rykov committed
262
263
        #print(last[1][2][2])
        #print(type(last[1][2][2]))
Vladislav Rykov's avatar
Vladislav Rykov committed
264
265
        #print(count)
        if count[1][0] > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
266
            return render_template('public/dev-data.html', data=last[1], total=count[1][0], lastctr=last_ctr, devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
267
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
268
            return render_template('public/dev-data.html', devname=session['devname'])
Vladislav Rykov's avatar
Vladislav Rykov committed
269
    else:
Vladislav Rykov's avatar
Vladislav Rykov committed
270
        return redirect(utl_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
271

Vladislav Rykov's avatar
Vladislav Rykov committed
272
@app.route('/data-csv')
Vladislav Rykov's avatar
Vladislav Rykov committed
273
def data_csv():
Vladislav Rykov's avatar
Vladislav Rykov committed
274
275
    if 'name' in session and 'devid' in session:
        dumpd = data.get_all(session['appkey'], session['devid'])
Vladislav Rykov's avatar
Vladislav Rykov committed
276

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

Vladislav Rykov's avatar
Vladislav Rykov committed
279
        with open(app.config['DATA_DOWNLOAD_DIR']+'/'+fn, 'w') as f: 
Vladislav Rykov's avatar
Vladislav Rykov committed
280
281
            for d in dumpd[1][0][2]:
                f.write(d)
Vladislav Rykov's avatar
Vladislav Rykov committed
282
283
                f.write(',')
            f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
284
285
286
287
288
289
        
            for row in dumpd[1]:
                for v in row[2]:
                    f.write(str(row[2][v]))
                    f.write(',')
                f.write('\n')
Vladislav Rykov's avatar
Vladislav Rykov committed
290
    
Vladislav Rykov's avatar
Vladislav Rykov committed
291
        return send_from_directory(app.config['DATA_DOWNLOAD_DIR'], fn, as_attachment=True)
Vladislav Rykov's avatar
Vladislav Rykov committed
292
293
    else:
        return redirect(utl_for('index'))