From 97f983b73c5a27cd8785575bf941e66d747cdd82 Mon Sep 17 00:00:00 2001 From: Marian Sigler Date: Fri, 14 Sep 2012 01:28:02 +0200 Subject: first web stuff --- app.py | 34 +++++++++++++++++++++++++++++++--- forms.py | 9 +++++++++ static/layout.css | 3 +++ templates/_macros.html | 16 ++++++++++++++++ templates/base.html | 18 ++++++++++++++++++ templates/index.html | 5 +++++ templates/register.html | 12 ++++++++++++ utils.py | 21 +++++++++++++++++++++ 8 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 forms.py create mode 100644 static/layout.css create mode 100644 templates/_macros.html create mode 100644 templates/base.html create mode 100644 templates/index.html create mode 100644 templates/register.html create mode 100644 utils.py diff --git a/app.py b/app.py index a23c962..8166834 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,39 @@ # -*- coding: utf-8 -*- -from flask import Flask +from flask import Flask, request +from utils import templated +from forms import RegisterForm + app = Flask(__name__) @app.route('/') +@templated('index.html') def index(): - return 'Hello World!' + return {} + +@app.route('/register', methods=['GET', 'POST']) +@templated('register.html') +def register(): + form = RegisterForm(request.form) + if request.method == 'POST' and form.validate(): + username = form.username.data + mail = form.mail.data + password = form.password.data + return '

501 Not Implemented

' + return {'form': form} if __name__ == '__main__': - app.run() + app.run(debug=True) + + + + +# wir brauchen: +# registrieren +# login +# passwort ändern (master-passwort, einzelne) +# email ändern + +# später: +# account löschen +# openid-provider (ggf mehr Details: Realname, Zeitzone, ...) diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..273d02f --- /dev/null +++ b/forms.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +from wtforms import Form, validators, TextField, PasswordField + +class RegisterForm(Form): + username = TextField('Benutzername', [validators.Length(min=4, max=20)]) + mail = TextField('E-Mail-Adresse', [validators.Length(min=6, max=50)]) + password = PasswordField('Passwort', [validators.Required(), + validators.EqualTo('password_confirm', message=u'Passwörter stimmen nicht überein')]) + password_confirm = PasswordField(u'Passwort bestätigen') diff --git a/static/layout.css b/static/layout.css new file mode 100644 index 0000000..d649f78 --- /dev/null +++ b/static/layout.css @@ -0,0 +1,3 @@ +form .errors { + color: #D00; +} diff --git a/templates/_macros.html b/templates/_macros.html new file mode 100644 index 0000000..2c933be --- /dev/null +++ b/templates/_macros.html @@ -0,0 +1,16 @@ +{% macro render_field(field) %} +
{{ field.label }} +
{{ field(**kwargs)|safe }} + {% if field.errors|length == 1 %} +
+ {{ field.errors.0 }} +
+ {% elif field.errors %} + + {% endif %} +
+{% endmacro %} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..6e4e403 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,18 @@ +{%- set styles = ['layout.css'] + styles|default([]) %} +{%- set scripts = [] + scripts|default([]) %} + + + + {% if title %}{{ title }} – {% endif %}spline accounts + {%- for script in scripts %} + + {%- endfor %} + {%- for style in styles %} + + {%- endfor %} + + +

{% if title %}{{ title }}{% else %}spline accounts{% endif %}

+ {% block content %}{% endblock %} + + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9ee3b0f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,5 @@ +{%- extends 'base.html' %} +{%- block content %} +

Willkommen bei spline accounts!

+

Account erstellen

+{%- endblock %} diff --git a/templates/register.html b/templates/register.html new file mode 100644 index 0000000..094056e --- /dev/null +++ b/templates/register.html @@ -0,0 +1,12 @@ +{%- extends 'base.html' %} +{%- from '_macros.html' import render_field %} +{%- set title = 'Account erstellen' %} +{%- block content %} +
+ {{ render_field(form.username) }} + {{ render_field(form.mail) }} + {{ render_field(form.password) }} + {{ render_field(form.password_confirm) }} + +
+{%- endblock %} diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..2f3b433 --- /dev/null +++ b/utils.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +from functools import wraps +from flask import request, render_template + +# from http://flask.pocoo.org/docs/patterns/viewdecorators/#templating-decorator +def templated(template=None): + def decorator(f): + @wraps(f) + def decorated_function(*args, **kwargs): + template_name = template + if template_name is None: + template_name = request.endpoint \ + .replace('.', '/') + '.html' + ctx = f(*args, **kwargs) + if ctx is None: + ctx = {} + elif not isinstance(ctx, dict): + return ctx + return render_template(template_name, **ctx) + return decorated_function + return decorator -- cgit v1.2.3-1-g7c22