aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--famille.dbbin0 -> 3072 bytes
-rw-r--r--famille.py77
-rw-r--r--schema.sql6
-rw-r--r--templates/base.html45
-rw-r--r--templates/layout.html31
-rw-r--r--templates/login.html17
7 files changed, 133 insertions, 45 deletions
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
--- /dev/null
+++ b/famille.db
Binary files 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 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
-<head>
- <title>{% block title %}Site de la famille{% endblock title %}</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link rel="stylesheet" media="screen" type="text/css" title="Design" href="/static/style.css" />
- <link rel="alternate" type="application/rss+xml" title="Les actualités" href="rss.xml" />
- <link rel="icon" type="image/png" href="/views/favicon.png" />
- <script src="/library/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $.fn.wait = function(time, type) {
- time = time || 1000;
- type = type || "fx";
- return this.queue(type, function() {
- var self = this;
- setTimeout(function() {
- $(self).dequeue();
- }, time);
- });
- };
- </script>
-</head>
-<body>
-<div class="container">
-<div class="sheet">
-<div class="top">
- <p><strong><?=$_SESSION['username']?></strong> (<a href="/user/edit">Mon compte</a> | <a href="/disconnect.php">Se déconnecter</a>)</p>
-</div>
-<div class="main_menu">
- <table>
- <tr>
- <td><a href="/">Accueil</a></td>
- <td><a href="/article/">Articles</a></td>
- <td><a href="/user/">Famille</a></td>
- <td><a href="/system/">Système</a></td>
- </tr>
-</table>
-</div>
-<div class="main">
-{% block content %}{% endblock %}
-</div>
-</div>
-</div>
-</body>
-</html>
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 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+ <head>
+ <title>{% block title %}Site de la famille{% endblock %}</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <link rel="stylesheet" media="screen" type="text/css" href="{{ url_for('static', filename = 'style.css') }}" />
+ <!-- <link rel="alternate" type="application/rss+xml" title="Les actualités" href="rss.xml" /> -->
+ </head>
+ <body>
+ <div class="container">
+ <div class="sheet">
+ <div class="top">
+ (<a href="/user/edit">Mon compte</a> | <a href="/disconnect.php">Se déconnecter</a>)</p>
+ </div>
+ <div class="main_menu">
+ <table>
+ <tr>
+ <td><a href="/">Accueil</a></td>
+ <td><a href="/article/">Articles</a></td>
+ <td><a href="/user/">Famille</a></td>
+ <td><a href="/system/">Système</a></td>
+ </tr>
+ </table>
+ </div>
+ <div class="main">
+ {% block content %}{% endblock %}
+ </div>
+ </div>
+ </div>
+ </body>
+</html>
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 %}
+
+<h2>Login</h2>
+{% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
+ <form id = "connection" action="{{ url_for('login') }}" method="post">
+ <fieldset>
+ <legend>Connexion</legend>
+ <p>
+ <label for="name">Nom : </label>
+ <input type="text" id="name" name=username /><br/>
+ <label for="password">Mot de passe : </label>
+ <input type="password" id="password" name=password /><br/>
+ </p>
+ <p class="submit"><input type="submit" value="Se connecter"/></p>
+ </form>
+ {% endblock %}