From 6a1d6a7365fb917784d378cf3b3683c85bb81b48 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Mon, 5 Nov 2012 00:05:32 -0500 Subject: starting to hook things up --- .gitignore | 2 ++ famille.db | Bin 0 -> 3072 bytes famille.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ schema.sql | 6 ++++ templates/base.html | 45 ----------------------------- templates/layout.html | 31 ++++++++++++++++++++ templates/login.html | 17 +++++++++++ 7 files changed, 133 insertions(+), 45 deletions(-) create mode 100644 .gitignore create mode 100644 famille.db create mode 100644 famille.py create mode 100644 schema.sql delete mode 100644 templates/base.html create mode 100644 templates/layout.html create mode 100644 templates/login.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f54eb28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +\#*# diff --git a/famille.db b/famille.db new file mode 100644 index 0000000..5092263 Binary files /dev/null and b/famille.db differ diff --git a/famille.py b/famille.py new file mode 100644 index 0000000..4c0f2e3 --- /dev/null +++ b/famille.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +import sqlite3 +#all the imports +from flask import Flask, request, session, g, redirect, url_for, \ + abort, render_template, flash, _app_ctx_stack +import hashlib + +# configuration +app = Flask(__name__) +app.config.from_envvar('CONF') + +def connect_db(): + return sqlite3.connect(app.config['DATABASE']) + +def init_db(): + """Creates the database tables.""" + with app.app_context(): + db = get_db() + db.row_factory = sqlite3.Row + with app.open_resource('schema.sql') as f: + db.cursor().executescript(f.read()) + db.commit() + +def get_db(): + """Opens a new database connection if there is none yet for the + current application context. + """ + top = _app_ctx_stack.top + if not hasattr(top, 'sqlite_db'): + top.sqlite_db = sqlite3.connect(app.config['DATABASE']) + return top.sqlite_db + +def query_db(query, args=(), one=False): + cur = get_db().execute(query, args) + rv = cur.fetchall() + cur.close() + return (rv[0] if rv else None) if one else rv + +@app.teardown_appcontext +def close_db_connection(exception): + """Closes the database again at the end of the request.""" + top = _app_ctx_stack.top + if hasattr(top, 'sqlite_db'): + top.sqlite_db.close() + + +@app.route('/') +def main(): + try: + if session['logged_in']: + return redirect(url_for('nouvelles')) + else: + return redirect(url_for('login')) + except KeyError: + return redirect(url_for('login')) + +@app.route('/nouvelles') +def show_news(): + pass + +@app.route('/login', methods=['GET', 'POST']) +def login(): + error = None + if request.method == 'POST': + #TODO: do something better, maybe scrypt + if query_db('select * from users where user = ? and password = ?', + (request.form['username'], + hashlib.md5(request.form['password']).hexdigest())): + session['logged_in'] = True + return redirect(url_for('nouvelles')) + else: + error = u'Utilisateur non enregistré' + return render_template('login.html', error=error) + +if __name__=="__main__": + init_db() + app.run() diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..3c3231f --- /dev/null +++ b/schema.sql @@ -0,0 +1,6 @@ +drop table if exists users; +create table users ( + id integer primary key autoincrement, + user string not null, + password string not null +); diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index 1e2580f..0000000 --- a/templates/base.html +++ /dev/null @@ -1,45 +0,0 @@ - - - - {% block title %}Site de la famille{% endblock title %} - - - - - - - - -
-
- - -
-{% block content %}{% endblock %} -
-
-
- - diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..be923ea --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,31 @@ + + + + {% block title %}Site de la famille{% endblock %} + + + + + +
+
+ + +
+ {% block content %}{% endblock %} +
+
+
+ + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..d1b94ee --- /dev/null +++ b/templates/login.html @@ -0,0 +1,17 @@ +{% extends "layout.html" %} +{% block content %} + +

Login

+{% if error %}

Error: {{ error }}{% endif %} +

+
+ Connexion +

+ +
+ +
+

+

+ + {% endblock %} -- cgit v1.2.3-70-g09d2