server.py 3.59 KB
Newer Older
1
2
from flask import Flask, render_template, request, redirect, url_for, session
import psycopg2
Vladislav Rykov's avatar
Vladislav Rykov committed
3
import bcrypt
4
import misc
Vladislav Rykov's avatar
Vladislav Rykov committed
5
import dao.user.user as ud
Vladislav Rykov's avatar
Vladislav Rykov committed
6
import dao.application.application as ad
7
import dao.device.device as dd
8
9
10


server = Flask(__name__, template_folder='templates/')
Vladislav Rykov's avatar
Vladislav Rykov committed
11

12

13
@server.route('/')
14
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
15
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
16
17
        ah = ad.ApplicationDao()
        apps = ah.get_list(session['name'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
18
        print('apps: ', apps)
19
20
21
22
        if apps[0]:
            return render_template('index.html', apps=apps[1])
        else:
            return render_template('index.html', feedback=apps[1])
23

24
25
    return render_template('index.html')

26
27
28


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

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
            return render_template('signup.html', feedback=feedback)
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
40
41
42
43
            uh = ud.UserDao()
            res = uh.create(username, password)
            if (not res[0]):
                return render_template('signup.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
44
45
            else:
                session['name'] = username
46
        
Vladislav Rykov's avatar
Vladislav Rykov committed
47
                return redirect(url_for('index'))
48

49
50
51


@server.route('/login', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
52
def login():
Vladislav Rykov's avatar
Vladislav Rykov committed
53
54
55
56
    if request.method == 'GET':
        return render_template('login.html')
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
57
        password = request.form['password'].encode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
58
59
60
61
62

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
            return render_template('login.html', feedback=feedback)
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
63
64
65
66
            uh = ud.UserDao()
            res = uh.get(username, password)
            if (not res[0]):
                return render_template('login.html', feedback=msg[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
67
68
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
69
        
Vladislav Rykov's avatar
Vladislav Rykov committed
70
71
72
                return redirect(url_for('index'))


73
74

@server.route('/logout')
Vladislav Rykov's avatar
Vladislav Rykov committed
75
76
77
def logout():
    session.clear()
    return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
78
79


80
81
82
83
84
85
86
87
88

@server.route('/new-app')
def new_application():
    return render_template('new-app.html')



@server.route('/app', methods=['GET', 'POST'])
def app():
Vladislav Rykov's avatar
Vladislav Rykov committed
89
    ah = ad.ApplicationDao()
90
    if request.method == 'GET':
91
        dh = dd.DeviceDao()
Vladislav Rykov's avatar
Vladislav Rykov committed
92
        app = ah.get(request.args.get('appkey'))
93
94
95
        devs = dh.get_list(app[1][1])
        print('devs : ', devs)
        return render_template('app.html', app=app[1], devs=devs[1])
96
97
98
99
100
    else:
        if request.form['appname'] == '':
            error = 'Application name cannot be empty.'
            return render_template('new-app.html', feedback=error)
        else:
Vladislav Rykov's avatar
Vladislav Rykov committed
101
102
            res = ah.create(request.form['appname'], session['name'], request.form['appdesc'])
            
Vladislav Rykov's avatar
Vladislav Rykov committed
103
104
105
            if not res[0]:
                return render_template('new-app.html', feedback=res[1])

Vladislav Rykov's avatar
Vladislav Rykov committed
106
107
108
109
            #res = new_app_devs(request.form['appname'])
            #if not res[0]:
            #    rm_app(request.form['appname'])
            #    return render_template('new-app.html', feedback=res[1])
Vladislav Rykov's avatar
Vladislav Rykov committed
110
            
Vladislav Rykov's avatar
Vladislav Rykov committed
111
112
113
114
            #if not res[0] or not rer[0]:
            #    return render_template('new-app.html', feedback=str(res[1])+'|'+str(rer[1]))
            #else:
            return redirect(url_for('index'))
115

116
117

if __name__ == '__main__':
118
119
120
121
    server.secret_key = 'sdjfklsjf^$654sd^#sPH'
    server.run(debug = True, host='0.0.0.0')