From df25a48abd69292af55f3356a48a737da829cd20 Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Wed, 7 Nov 2012 01:27:36 +0100 Subject: Add basic news feature: list, show, add --- famille.py | 46 ++++++++++++++++++++++++++++++----------- schema.sql | 10 +++++++++ templates/news/add.html | 9 ++++++++ templates/news/list.html | 19 +++++++++++++++++ templates/news/news_detail.html | 41 ------------------------------------ templates/news/news_list.html | 23 --------------------- templates/news/post.html | 8 ------- templates/news/show.html | 39 ++++++++++++++++++++++++++++++++++ 8 files changed, 111 insertions(+), 84 deletions(-) create mode 100644 templates/news/add.html create mode 100644 templates/news/list.html delete mode 100644 templates/news/news_detail.html delete mode 100644 templates/news/news_list.html delete mode 100644 templates/news/post.html create mode 100644 templates/news/show.html diff --git a/famille.py b/famille.py index 5a09c5e..4739f25 100644 --- a/famille.py +++ b/famille.py @@ -10,11 +10,6 @@ import hashlib app = Flask(__name__) app.config.from_envvar('CONF') -def connect_db(): - conn = sqlite3.connect(app.config['DATABASE']) - conn.row_factory = sqlite3.Row - return conn - def query_db(query, args=(), one=False): cur = g.db.execute(query, args) rv = cur.fetchone() if one else cur.fetchall() @@ -23,7 +18,9 @@ def query_db(query, args=(), one=False): @app.before_request def before_request(): - g.db = connect_db() + conn = sqlite3.connect(app.config['DATABASE']) + conn.row_factory = sqlite3.Row + g.db = conn @app.teardown_appcontext def close_db_connection(exception): @@ -33,7 +30,7 @@ def close_db_connection(exception): def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): - if 'username' not in session: + if 'name' not in session: return redirect(url_for('login', next=request.url)) return f(*args, **kwargs) return decorated_function @@ -41,12 +38,36 @@ def login_required(f): @app.route('/') @login_required def main(): - return redirect(url_for('show_news')) + return redirect(url_for('list_news')) -@app.route('/nouvelles') -def show_news(): - return render_template("toto.html") +@app.route('/news/') +@login_required +def list_news(): + print "toto" + news = query_db("SELECT * FROM news LEFT JOIN users ON news.user_id = users.id") + return render_template("news/list.html", news=news) +@app.route('/news/add/', methods=['GET', 'POST']) +@login_required +def add_news(): + if request.method == 'POST': + cur = g.db.execute("INSERT INTO news ('title', 'content', 'user_id') " + "VALUES (?, ?, ?)", + (request.form['title'], request.form['content'], + session['user_id'])) + news_id = cur.lastrowid + g.db.commit() + return redirect(url_for('show_news', news_id=news_id)) + elif request.method == 'GET': + return render_template("news/add.html") + +@app.route('/news//') +@login_required +def show_news(news_id): + news = query_db("SELECT * FROM news LEFT JOIN users ON news.user_id = users.id " + "WHERE news.id = ?", (news_id,), True) + return render_template("news/show.html", news=news) + @app.route('/login', methods=['GET', 'POST']) def login(): error = None @@ -57,7 +78,8 @@ def login(): if user: if user['password'] == password: session['name'] = user['name'] - return redirect(url_for('show_news')) + session['user_id'] = user['id'] + return redirect(url_for('list_news')) else: error = u'Mot de passe incorrect' else: diff --git a/schema.sql b/schema.sql index 15787e6..2119322 100644 --- a/schema.sql +++ b/schema.sql @@ -4,3 +4,13 @@ create table users ( name string not null, password string not null ); + +drop table if exists news; +create table news ( + id integer primary key autoincrement, + title string not null, + date string default (strftime('%Y-%m-%dT%H:%M:%S','now')), + content string not null, + user_id integer, + FOREIGN KEY(user_id) REFERENCES users(id) +); diff --git a/templates/news/add.html b/templates/news/add.html new file mode 100644 index 0000000..c2bc018 --- /dev/null +++ b/templates/news/add.html @@ -0,0 +1,9 @@ +{% extends 'layout.html' %} + +{% block content %} +
+
+ + +
+{% endblock %} diff --git a/templates/news/list.html b/templates/news/list.html new file mode 100644 index 0000000..6ddf85a --- /dev/null +++ b/templates/news/list.html @@ -0,0 +1,19 @@ +{% extends 'layout.html' %} + +{% block content %} +
+ {% for news in news %} +
+

+ {{ news.title }} +

+

Posté par {{ news.name }} le {{ news.date }}

+
+ +
+ {{ news.content }} +
+ {% endfor %} +
+
+{% endblock %} diff --git a/templates/news/news_detail.html b/templates/news/news_detail.html deleted file mode 100644 index f65b91e..0000000 --- a/templates/news/news_detail.html +++ /dev/null @@ -1,41 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} -{% with news.comment_set.all as comments %} -
-

Détails

-
- -
-

{{news.title}}

- -
-{{news.text}} -
- -
- -

Commentaires

-{% for comment in comments %} -

Posté par {{comment.author}} le {{comment.date}}

-
-{{comment.text}} -
-{% endfor %} - -

Ajouter un commentaire

- -
- - -

-
- -
-{% endwith %} -{% endblock %} diff --git a/templates/news/news_list.html b/templates/news/news_list.html deleted file mode 100644 index 2119a68..0000000 --- a/templates/news/news_list.html +++ /dev/null @@ -1,23 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} -
- {% for news in news_list %} -
-

{{ news.title }}

- -

Posté par {{ news.author.username }} le {{ news.date }}

-
- -

{% with news.comment__count as comment_count %} - {{ comment_count }} commentaire{{comment_count|pluralize}} | - Ajouter un commentaire -

{% endwith %} - -
- {{ news.text }} -
- {% endfor %} -
-
-{% endblock %} diff --git a/templates/news/post.html b/templates/news/post.html deleted file mode 100644 index 56ad2e4..0000000 --- a/templates/news/post.html +++ /dev/null @@ -1,8 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} - -{{ form.as_p }} - - -{% endblock %} diff --git a/templates/news/show.html b/templates/news/show.html new file mode 100644 index 0000000..da188fe --- /dev/null +++ b/templates/news/show.html @@ -0,0 +1,39 @@ +{% extends 'layout.html' %} + +{% block content %} +
+

Détails

+
+ +
+

{{news.title}}

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