diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2012-11-07 01:27:36 +0100 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2012-11-07 01:27:36 +0100 |
| commit | df25a48abd69292af55f3356a48a737da829cd20 (patch) | |
| tree | fdb3ba7d659ee5dd379eb41b284bac6832a2d729 | |
| parent | e143cf83baf0c50da27251cee4f5e3a8929586a6 (diff) | |
| download | famille-flask-df25a48abd69292af55f3356a48a737da829cd20.tar.gz | |
Add basic news feature: list, show, add
| -rw-r--r-- | famille.py | 46 | ||||
| -rw-r--r-- | schema.sql | 10 | ||||
| -rw-r--r-- | templates/news/add.html | 9 | ||||
| -rw-r--r-- | templates/news/list.html | 19 | ||||
| -rw-r--r-- | templates/news/news_list.html | 23 | ||||
| -rw-r--r-- | templates/news/post.html | 8 | ||||
| -rw-r--r-- | templates/news/show.html (renamed from templates/news/news_detail.html) | 14 |
7 files changed, 78 insertions, 51 deletions
@@ -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/<int:news_id>/') +@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: @@ -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 %} +<form action="{{ url_for('add_news')}}" method="post"> + <input type=text size=80 name=title /><br/> + <textarea name=content rows=20 cols=80></textarea> +<input type="submit" value="Submit" /> +</form> +{% 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 %} +<div class="news"> + {% for news in news %} + <div class="news_head"> + <h2><a href="{{ url_for('show_news', news_id = news.id)}}"> + {{ news.title }}</a> + </h2> + <p>Posté par <a href="">{{ news.name }}</a> le {{ news.date }}</p> + </div> + + <div class="news_content"> + {{ news.content }} + </div> + {% endfor %} +</div> +<div style="clear:both"/> +{% 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 %} -<div class="news"> - {% for news in news_list %} - <div class="news_head"> - <h2><a href="{% url news-details pk=news.id %}">{{ news.title }}</a></h2> - <!--div style="float:right"><a href="/home/edit/id/<?=$news['ident']?>">Modifier cette actualité</a></div--> - <p>Posté par <a href="/user/view/name/<?=$news['name']?>">{{ news.author.username }}</a> le {{ news.date }}</p> - </div> - - <p class="comments">{% with news.comment__count as comment_count %} - <a href="{% url news-details pk=news.id %}#comment">{{ comment_count }} commentaire{{comment_count|pluralize}}</a> | - <a href="{% url news-details pk=news.id %}#add">Ajouter un commentaire</a> - </p>{% endwith %} - - <div class="news_content"> - {{ news.text }} - </div> - {% endfor %} -</div> -<div style="clear:both"/> -{% 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 action="{% url news.views.post %}" method="post"> -{{ form.as_p }} -<input type="submit" value="Submit" /> -</form> -{% endblock %} diff --git a/templates/news/news_detail.html b/templates/news/show.html index f65b91e..da188fe 100644 --- a/templates/news/news_detail.html +++ b/templates/news/show.html @@ -1,24 +1,23 @@ -{% extends 'base.html' %} +{% extends 'layout.html' %} {% block content %} -{% with news.comment_set.all as comments %} <div class="quickview"/> <h2>Détails</h2> <ul> - <li>Auteur : <a href="/user/view/name/<?=$this->news['name']?>">{{news.author}}</a></li> + <li>Auteur : <a href="">{{news.name}}</a></li> <li>Date : {{news.date}} </li> -<li><a href="#comments">{{coments}}</a></li> +<!--<li><a href="#comments">{{coments}}</a></li>--> </div> <div class="news"/> <h1>{{news.title}}</h1> <div class="news_content"> -{{news.text}} +{{news.content}} </div> <hr/> - +<!-- <h2 id="comments">Commentaires</h2> {% for comment in comments %} <h3 class="comment">Posté par <a href="/user/view/name/<?=$comment['name']?>">{{comment.author}}</a> le {{comment.date}} </h3> @@ -34,8 +33,7 @@ </textarea> <p class="submit"><input type="submit" value="Ajouter"/></p> +--> </div> - <div style="clear:both"/> -{% endwith %} {% endblock %} |
