Permalink
Browse files

Setup basic server.

  • Loading branch information...
lepture committed Nov 21, 2013
1 parent cd288c9 commit d1e3b6de1982894b97a719b04d9f0161e5739074
Showing with 66 additions and 0 deletions.
  1. +46 −0 app.py
  2. +20 −0 templates/home.html
View
46 app.py
@@ -0,0 +1,46 @@
# coding: utf-8
from flask import Flask
from flask import session, request
from flask import render_template, redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, template_folder='templates')
app.debug = True
app.secret_key = 'secret'
app.config.update({
'SQLALCHEMY_DATABASE_URI': 'sqlite:///db.sqlite',
})
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40), unique=True)
def current_user():
if 'id' in session:
uid = session['id']
return User.query.get(uid)
return None
@app.route('/', methods=('GET', 'POST'))
def home():
if request.method == 'POST':
username = request.form.get('username')
user = User.query.filter_by(username=username).first()
if not user:
user = User(username=username)
db.session.add(user)
db.session.commit()
session['id'] = user.id
return redirect('/')
user = current_user()
return render_template('home.html', user=user)
if __name__ == '__main__':
db.create_all()
app.run()
View
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% if user %}
<p>You are {{ user.username }}</p>
{% else %}
<p>You are not authenticated</p>
{% endif %}
<p>Type any username:</p>
<form method="post" action="/">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>

0 comments on commit d1e3b6d

Please sign in to comment.