]> git.zarvox.org Git - imoo.git/commitdiff
Basic templates, helpers, login page
authorDrew Fisher <drew@aerofs.com>
Mon, 10 Mar 2014 08:11:23 +0000 (01:11 -0700)
committerDrew Fisher <drew@aerofs.com>
Mon, 10 Mar 2014 08:11:23 +0000 (01:11 -0700)
Doesn't actually do anything, but exercises:

1) Flask-WTF
2) CSRF tokens
3) Templating
4) Static files

config.py
imoo/forms.py
imoo/static/style.css [new file with mode: 0644]
imoo/templates/_formhelpers.html [new file with mode: 0644]
imoo/templates/base.html [new file with mode: 0644]
imoo/templates/index.html [new file with mode: 0644]
imoo/templates/login.html [new file with mode: 0644]
imoo/templates/main.html [new file with mode: 0644]
imoo/views.py

index 0291ace4ea8d609cf454e5a9f644cd15ca616c51..ce12112ed6d740fc7e0bfa2fc2f2735b57bc8241 100644 (file)
--- a/config.py
+++ b/config.py
@@ -1,4 +1,18 @@
 import os
 
 basedir = os.path.abspath(os.path.dirname(__file__))
+
+# SQLAlchemy connection string
 SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'state', 'database.db')
+
+# CSRF protection: generate a CSRF secret key if none exists
+CSRF_ENABLED=True
+csrf_keyfile = os.path.join(basedir, 'state', 'csrf_secret')
+if not os.path.exists(csrf_keyfile):
+    with open("/dev/urandom") as rng:
+        key = rng.read(64)
+    with open(csrf_keyfile, "w") as f:
+        f.write(key)
+
+with open(csrf_keyfile) as f:
+        SECRET_KEY = f.read()
index 27cb4d5d23bb3292a5d6156943c4a4d73dbcc1cd..3032ff6f7a68dc8c3111ce83f6a9ea2333353ff2 100644 (file)
@@ -2,4 +2,6 @@ from flask.ext.wtf import Form
 from wtforms import TextField, PasswordField, IntegerField, DateField, SelectField, TextAreaField
 from wtforms.validators import ValidationError, InputRequired, Email, Length, Optional
 
-
+class LoginForm(Form):
+    username = TextField("Username", validators=[InputRequired()])
+    password = PasswordField("Password", validators=[InputRequired()])
diff --git a/imoo/static/style.css b/imoo/static/style.css
new file mode 100644 (file)
index 0000000..090399d
--- /dev/null
@@ -0,0 +1,8 @@
+/* Yeah, this isn't a proper stylesheet.  Frontend devs, replace this at will */
+
+header > #messages > ul {
+       background-color: #dedede;
+}
+header > #messages > ul > li {
+       list-style: none;
+}
diff --git a/imoo/templates/_formhelpers.html b/imoo/templates/_formhelpers.html
new file mode 100644 (file)
index 0000000..dea9d03
--- /dev/null
@@ -0,0 +1,15 @@
+{% macro render_field(field) %}
+<div class="form-label">{{ field.label }}</div>
+<div class="form-field">{{ field(**kwargs)|safe }}</div>
+{% if field.errors %}
+    <ul class=errors>
+    {% for error in field.errors %}
+        <li><span style="color: red;">{{ error }}</span></li>
+    {% endfor %}
+    </ul>
+{% endif %}
+{% endmacro %}
+
+{% macro submit_button(button_text) %}
+<input type="submit" value="{{ button_text }}">
+{% endmacro %}
diff --git a/imoo/templates/base.html b/imoo/templates/base.html
new file mode 100644 (file)
index 0000000..f774306
--- /dev/null
@@ -0,0 +1,11 @@
+<!doctype html>
+<html lang="en-US">
+    <head>
+        <title>{% block title %}IMOO{% endblock %}</title>
+        <link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet" />
+        {% block head %}{% endblock %}
+    </head>
+    <body>
+        {% block layout %}{% endblock %}
+    </body>
+</html>
diff --git a/imoo/templates/index.html b/imoo/templates/index.html
new file mode 100644 (file)
index 0000000..c8b2098
--- /dev/null
@@ -0,0 +1,11 @@
+{% extends 'main.html' %}
+
+{% block header %}
+<h1>It works.</h1>
+{% endblock header %}
+
+{% block content %}
+<ul>
+    <li><a href="/login">Log in</a></li>
+</ul>
+{% endblock %}
diff --git a/imoo/templates/login.html b/imoo/templates/login.html
new file mode 100644 (file)
index 0000000..4e4d141
--- /dev/null
@@ -0,0 +1,13 @@
+{% extends 'main.html' %}
+{% from '_formhelpers.html' import render_field, submit_button %}
+
+{% block content %}
+<h1>LOG IN FIRST, SCRUB</h1>
+
+<form action="" method="post">
+    {{ form.hidden_tag() }}
+    {{ render_field(form.username) }}
+    {{ render_field(form.password) }}
+    {{ submit_button("Login") }}
+</form>
+{% endblock %}
diff --git a/imoo/templates/main.html b/imoo/templates/main.html
new file mode 100644 (file)
index 0000000..b78d5ee
--- /dev/null
@@ -0,0 +1,25 @@
+{% extends 'base.html' %}
+
+{% block layout %}
+    <header>
+        {% with messages = get_flashed_messages(with_categories=True) %}
+        {% if messages %}
+        <div id="messages">
+        <ul>
+        {% for category, message in messages %}
+            <li class="{{ category }}">{{ message }}</li>
+        {% endfor %}
+        </ul>
+        </div>
+        {% endif %}
+        {% endwith %}
+        {% block header %}
+        {% endblock %}
+    </header>
+    <main>
+        {% block content %}{% endblock %}
+    </main>
+    <footer>
+        {% block footer %}{% endblock %}
+    </footer>
+{% endblock %}
index 2205630e4cfb917ee1802352787c39db25e7034d..07072349904db5e1da02595124f98dec97fbce85 100644 (file)
@@ -1,4 +1,4 @@
-from flask import Blueprint
+from flask import Blueprint, render_template, flash
 
 from imoo import db, login_manager
 from . import forms, models
@@ -6,5 +6,12 @@ from . import forms, models
 blueprint = Blueprint('main', __name__, template_folder='templates')
 
 @blueprint.route("/")
-def test():
-    return "<!doctype html><html><body><h1>HI THERE</h1></body></html>"
+def index():
+    return render_template('index.html')
+
+@blueprint.route("/login", methods=["GET", "POST"])
+def login():
+    form = forms.LoginForm()
+    if form.validate_on_submit():
+        flash(u"login form submitted", "success")
+    return render_template('login.html', form=form)