server.py 4.66 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
8
9
10
11
12


APP_KEY_LEN = 8


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

14

15
16
17
18
19
20
21

def new_app_devs(appkey):
    suc = (True, 'app_devs created')
    try:
        conn = psycopg2.connect('dbname=gateway')
        cur  = conn.cursor()
        query = """
Vladislav Rykov's avatar
Vladislav Rykov committed
22
        CREATE TABLE devs_%s (
23
24
25
26
27
28
29
30
31
            name VARCHAR(30) NOT NULL,
            dev_id NUMERIC(3) PRIMARY KEY,
            app_key VARCHAR(80),
            description VARCHAR(200)
            FOREIGN KEY (app_key) REFERENCES applications(app_key)
        );
        """
        cur.execute(query, (appkey,))
        conn.commit()
Vladislav Rykov's avatar
Vladislav Rykov committed
32
        print('Devs table created')
33
    except (Exception, psycopg2.DatabaseError) as error:
Vladislav Rykov's avatar
Vladislav Rykov committed
34
        print('Error creating devs table: ', error)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        suc = (False, error)
    finally:
        if (conn):
            cur.close()
            conn.close()
        
    return suc





def get_devs(appkey):
    res = []
    try:
        conn = psycopg2.connect('dbname=gateway')
        cur  = conn.cursor()
        query = """
        SELECT * FROM
            devs-%s
        """
        cur.execute(query, (appkey,))
        res = cur.fetchall()
    except (Exception, psycopg2.DatabaseError) as error:
        print('Error querying applications: ', error)
    finally:
        if (conn):
            cur.close()
            conn.close()
        
    return res





@server.route('/')
72
def index():
Vladislav Rykov's avatar
Vladislav Rykov committed
73
    if 'name' in session and len(session['name']) > 0:
Vladislav Rykov's avatar
Vladislav Rykov committed
74
75
        ah = ad.ApplicationDao()
        apps = ah.get_list(session['name'].encode('utf-8'))
Vladislav Rykov's avatar
Vladislav Rykov committed
76
        print('apps: ', apps)
Vladislav Rykov's avatar
Vladislav Rykov committed
77
        return render_template('index.html', apps=apps[1])
78

79
80
    return render_template('index.html')

81
82
83


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

        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
95
96
97
98
            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
99
100
            else:
                session['name'] = username
101
        
Vladislav Rykov's avatar
Vladislav Rykov committed
102
                return redirect(url_for('index'))
103

104
105
106


@server.route('/login', methods=['GET', 'POST'])
Vladislav Rykov's avatar
Vladislav Rykov committed
107
def login():
Vladislav Rykov's avatar
Vladislav Rykov committed
108
109
110
111
    if request.method == 'GET':
        return render_template('login.html')
    else: 
        username = request.form['username']
Vladislav Rykov's avatar
Vladislav Rykov committed
112
        password = request.form['password'].encode('utf-8')
Vladislav Rykov's avatar
Vladislav Rykov committed
113
114
115
116
117

        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
118
119
120
121
            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
122
123
            else:
                session['name'] = username
Vladislav Rykov's avatar
Vladislav Rykov committed
124
        
Vladislav Rykov's avatar
Vladislav Rykov committed
125
126
127
                return redirect(url_for('index'))


128
129

@server.route('/logout')
Vladislav Rykov's avatar
Vladislav Rykov committed
130
131
132
def logout():
    session.clear()
    return redirect(url_for('index'))
Vladislav Rykov's avatar
Vladislav Rykov committed
133
134


135
136
137
138
139
140
141
142
143

@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
144
    ah = ad.ApplicationDao()
145
    if request.method == 'GET':
Vladislav Rykov's avatar
Vladislav Rykov committed
146
        app = ah.get(request.args.get('appkey'))
147
148
        devs = get_devs(app[1])
        
Vladislav Rykov's avatar
Vladislav Rykov committed
149
        return render_template('app.html', app=app[1], devs=devs)
150
151
152
153
154
    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
155
156
            res = ah.create(request.form['appname'], session['name'], request.form['appdesc'])
            
Vladislav Rykov's avatar
Vladislav Rykov committed
157
158
159
            if not res[0]:
                return render_template('new-app.html', feedback=res[1])

Vladislav Rykov's avatar
Vladislav Rykov committed
160
161
162
163
            #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
164
            
Vladislav Rykov's avatar
Vladislav Rykov committed
165
166
167
168
            #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'))
169

170
171

if __name__ == '__main__':
172
173
174
175
    server.secret_key = 'sdjfklsjf^$654sd^#sPH'
    server.run(debug = True, host='0.0.0.0')