Skip to content

Commit

Permalink
Setup basic server.
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Nov 21, 2013
1 parent cd288c9 commit d1e3b6d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
@@ -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.