server.py 4.73 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

Vladislav Rykov's avatar
Vladislav Rykov committed
13
14
15
def prep_ids(dev_list):
    return "any"

16
@server.route('/')
17
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
18
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
19
20
        ah = ad.ApplicationDao()
        apps = ah.get_list(session['name'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
21
22

        session.pop('appkey', None)
Vladislav Rykov's avatar
Vladislav Rykov committed
23
        print('apps: ', apps)
24
25
26
27
        if apps[0]:
            return render_template('index.html', apps=apps[1])
        else:
            return render_template('index.html', feedback=apps[1])
28
29
30
31



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

        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
43
44
45
46
            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
47
48
            else:
                session['name'] = username
49
        
Vladislav Rykov's avatar
Vladislav Rykov committed
50
                return redirect(url_for('index'))
51

52
53
54


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

        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
66
67
68
69
            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
70
71
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
72
        
Vladislav Rykov's avatar
Vladislav Rykov committed
73
74
75
                return redirect(url_for('index'))


76
77

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


83
84
85
86
87
88
89
90
91

@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
92
    ah = ad.ApplicationDao()
93
    if request.method == 'GET':
94
        dh = dd.DeviceDao()
Vladislav Rykov's avatar
Vladislav Rykov committed
95
96
97
98
99
100
101
102
        appkey = None
        if 'appkey' in session:
            appkey = session['appkey']
        else:
            appkey = request.args.get('appkey')
            session['appkey'] = request.args.get('appkey')

        app = ah.get(appkey)
103
        devs = dh.get_list(app[1][1])
Vladislav Rykov's avatar
Vladislav Rykov committed
104
        
105
106
        print('devs : ', devs)
        return render_template('app.html', app=app[1], devs=devs[1])
107
108
109
110
111
    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
112
113
            res = ah.create(request.form['appname'], session['name'], request.form['appdesc'])
            
Vladislav Rykov's avatar
Vladislav Rykov committed
114
115
116
            if not res[0]:
                return render_template('new-app.html', feedback=res[1])

Vladislav Rykov's avatar
Vladislav Rykov committed
117
118
119
120
            #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
121
            
Vladislav Rykov's avatar
Vladislav Rykov committed
122
123
124
125
            #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'))
126

127

Vladislav Rykov's avatar
Vladislav Rykov committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@server.route('/add-dev')
def new_dev():
    free_ids = 'whatever'
    
    dh = dd.DeviceDao()
    dev_list = dh.get_list(session['appkey'])
    
    print('dev list : ', dev_list)

    if not dev_list[0]:
        return render_template('add-dev.html', feedback=dev_list[1])
    else:
        return render_template('add-dev.html', free_ids=prep_ids(dev_list[1]))
 


@server.route('/dev', methods=['GET', 'POST'])
def dev():
    dh = dd.DeviceDao()
    if request.method == 'GET':
        dev = dh.get(session['appkey'], request.args.get('dev_id'))
        ltup = 'recently'

        return render_template('dev.html', dev=dev[1], ltup=ltup)
    else:
        res = dh.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
        
        if not res[0]:
            return render_template('add-dev.html', feedback=res[1])
        else:
            return redirect(url_for('app'))


161
if __name__ == '__main__':
162
163
164
165
    server.secret_key = 'sdjfklsjf^$654sd^#sPH'
    server.run(debug = True, host='0.0.0.0')