Commit 1c5bebcb authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

user signup, psql conn, layout

parent 5eac8082
from flask import Flask, render_template, request, redirect, url_for, session
import psycopg2
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)
"""
cur.execute(query, (name, password))
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
@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'))
@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')
{% extends 'layout.html' %}
{% block title %} Home {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2> Welcome! </h2>
{% if session['name'] %}
<h4> {{session['name']}}, explore your applications or create a new one. </h4>
<ol>
<li> App1 </li>
<li> App2 </li>
</ol>
{% else %}
<p>Log in, please</p>
{% endif %}
</div>
</div>
{% endblock %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE-edge"/>
<title> {% block title %} {% endblock %} </title>
<meta name="viewport" content="with-device-width, initial-scale-1"/>
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Simple IoT Server</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
{% if session['name'] %}
<li><a href="/logout">Logout</a></li>
{% else %}
<li><a href="/login">Login</a></li>
<li><a href="/signup">Signup</a></li>
{% endif %}
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
{% block content %} {% endblock %}
</div><!-- /.container -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
{% extends 'layout.html' %}
{% block title %} Login: {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset3">
<form action="signup" method="post">
<div class="form-group">
<label>Username:</label><br>
<input type="text" id="username" name="username"><br>
</div>
<div class="form-group">
<label>Password:</label><br>
<input type="password" id="password" name="password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Login</button>
</div>
{% if feedback %}
<p class="text-danger float-right">{{ feedback }}</p>
{% endif %}
</form>
</div>
</div>
{% endblock %}
{% extends 'layout.html' %}
{% block title %} Sign up: {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset3">
<form action="signup" method="post">
<div class="form-group">
<label>Username:</label><br>
<input type="text" id="username" name="username"><br>
</div>
<div class="form-group">
<label>Password:</label><br>
<input type="password" id="password" name="password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Signup</button>
</div>
{% if feedback %}
<p class="text-danger float-right">{{ feedback }}</p>
{% endif %}
</form>
</div>
</div>
{% endblock %}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment