server.py 2.98 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
4
import bcrypt

5
6
7
8
9
10
11
12
13
14
15
16
17
18

app = Flask(__name__, template_folder='templates/')

def new_user(name, password):
    suc = (True, 'User added')
    try:
        conn = psycopg2.connect('dbname=gateway')
        cur  = conn.cursor()
        query = """
        INSERT INTO
            users
        VALUES
            (%s, %s)
        """
Vladislav Rykov's avatar
Vladislav Rykov committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        cur.execute(query, (name, bcrypt.hashpw(password, bcrypt.gensalt())))
        conn.commit()
        print('User added')
    except (Exception, psycopg2.DatabaseError) as error:
        print('Error adding a user: ', error)
        suc = (False, error)
    finally:
        if (conn):
            cur.close()
            conn.close()
        
    return suc

def chk_user(name, password):
    suc = (True, 'Success')
    try:
        conn = psycopg2.connect('dbname=gateway')
        cur  = conn.cursor()
        query = """
        INSERT INTO
            users
        VALUES
            (%s, %s)
        """
        cur.execute(query, (name, bcrypt.hashpw(password, bcrypt.gensalt())))
44
45
46
47
48
49
50
51
52
53
54
55
56
        conn.commit()
        print('User added')
    except (Exception, psycopg2.DatabaseError) as error:
        print('Error adding a user: ', error)
        suc = (False, error)
    finally:
        if (conn):
            cur.close()
            conn.close()
        
    return suc


Vladislav Rykov's avatar
Vladislav Rykov committed
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return render_template('signup.html')
    else: 
        username = request.form['username']
        password = request.form['password']

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
            return render_template('signup.html', feedback=feedback)
        else:
            res, msg = new_user(username, password)
            if (not res):
                return render_template('signup.html', feedback=msg)

            session['name'] = username
        
            return redirect(url_for('index'))

Vladislav Rykov's avatar
Vladislav Rykov committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
@app.route('/login', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return render_template('login.html')
    else: 
        username = request.form['username']
        password = request.form['password']

        if (username == '' or password == ''):
            feedback = 'Username or password fields cannot be empty'
            return render_template('login.html', feedback=feedback)
        else:
            res, msg = chk_user(username, password)
            if (not res):
                return render_template('signup.html', feedback=msg)

            session['name'] = username
        
            return redirect(url_for('index'))


103
104
105
106
107
108
109
@app.route('/apps')
def apps():
    return '<h1>Manage your apps, ' + app.conf['username'] + '</h1>'

if __name__ == '__main__':
    app.secret_key = 'sdjfklsjf^$654sd^#sPH'
    app.run(debug = True, host='0.0.0.0')