# HG changeset patch # User Michał Rudowicz # Date 1299491268 -3600 # Node ID 58a993029d9fac281657fa8d9cdf33457c8333c4 # Parent 9a0a9fa7f91d6d00eb34018b64448098798aaaf9 Dodawanie nowych zadań i dekorator sprawdzający zalogowanie. diff -r 9a0a9fa7f91d -r 58a993029d9f frontend/mfrontend/db.py --- a/frontend/mfrontend/db.py Mon Feb 28 19:25:51 2011 +0100 +++ b/frontend/mfrontend/db.py Mon Mar 07 10:47:48 2011 +0100 @@ -134,3 +134,15 @@ return query_db("SELECT username FROM users WHERE id = ?;", [user_id], one=True)["username"] +def get_user_id(user_name): + """ Funkcja pobiera identyfikator użytkownika na podstawie nazwy. + @param user_name Nazwa użytkownika, którego indentyfikator ma zostać zwrócony. + @return Identyfikator użytkownika. + """ + return query_db("SELECT id FROM users WHERE username = ?;", + [user_name], one=True)["id"] + +def new_job(id, label, hash): + query_db("INSERT INTO jobs (owner_id, label, hash) VALUES (?,?,?);", + [id, label, hash]) + g.db.commit() diff -r 9a0a9fa7f91d -r 58a993029d9f frontend/mfrontend/decorators.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frontend/mfrontend/decorators.py Mon Mar 07 10:47:48 2011 +0100 @@ -0,0 +1,17 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from functools import wraps +from flask import g, request, redirect, url_for, session, flash + +def login_required(f): + @wraps(f) + def decorated_function(*args, **kwargs): + try: + if not session["logged_in"]: + raise Exception() + except Exception as e: + flash(u"Nie masz uprawnień do oglądania żądanej strony.") + return redirect(url_for('frontend.login')) + return f(*args, **kwargs) + return decorated_function diff -r 9a0a9fa7f91d -r 58a993029d9f frontend/mfrontend/templates/layout.html --- a/frontend/mfrontend/templates/layout.html Mon Feb 28 19:25:51 2011 +0100 +++ b/frontend/mfrontend/templates/layout.html Mon Mar 07 10:47:48 2011 +0100 @@ -1,14 +1,16 @@ NASZA APLIKACJA!
-

NO HEJ

+

NO HEJ

{% if not session.logged_in %} Zaloguj się | Załóż konto {% else %} - Wyloguj się | - Lista zadań + Strona główna | + Lista zadań | + Dodaj zadanie | + Wyloguj się {% endif %}
{% for message in get_flashed_messages() %} diff -r 9a0a9fa7f91d -r 58a993029d9f frontend/mfrontend/templates/newjob.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frontend/mfrontend/templates/newjob.html Mon Mar 07 10:47:48 2011 +0100 @@ -0,0 +1,14 @@ +{% extends "layout.html" %} +{% block body %} +

Login

+ {% if error %}

Błąd: {{ error }}{% endif %} +

+
+
Opis zadania: +
+
Hash: +
+
+
+
+{% endblock %} diff -r 9a0a9fa7f91d -r 58a993029d9f frontend/mfrontend/views/frontend.py --- a/frontend/mfrontend/views/frontend.py Mon Feb 28 19:25:51 2011 +0100 +++ b/frontend/mfrontend/views/frontend.py Mon Mar 07 10:47:48 2011 +0100 @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- from flask import Module, render_template, request, url_for -from flask import escape, session, redirect, flash, abort +from flask import escape, session, redirect, flash, abort, g from mfrontend import db, utils, exceptions frontend = Module(__name__) @@ -26,8 +26,9 @@ try: if db.user_can_login(request.form['username'], request.form['password']) : + session['user_id'] = db.get_user_id(request.form['username']) + session['user_name'] = request.form['username'] session['logged_in'] = True - session['logged_user'] = request.form['username'] flash(u'Zalogowano') return redirect(url_for('index')) else: @@ -44,6 +45,7 @@ a w efekcie wylogowanie z systemu. """ session.clear() + g.user = None flash('Wylogowano') return redirect(url_for('index')) diff -r 9a0a9fa7f91d -r 58a993029d9f frontend/mfrontend/views/jobcontrol.py --- a/frontend/mfrontend/views/jobcontrol.py Mon Feb 28 19:25:51 2011 +0100 +++ b/frontend/mfrontend/views/jobcontrol.py Mon Mar 07 10:47:48 2011 +0100 @@ -2,11 +2,25 @@ # -*- coding: utf-8 -*- from flask import Module, render_template, request, url_for -from flask import escape, session, redirect, flash, abort -from mfrontend import db, utils, exceptions +from flask import escape, session, redirect, flash, abort, g +from mfrontend import db, utils, exceptions, decorators jobcontrol = Module(__name__) @jobcontrol.route('/') +@decorators.login_required def index(): jobs = db.get_jobs(None, None, None) return render_template('joblist.html', jobs=jobs) + +@jobcontrol.route('/add/', methods=['GET', 'POST']) +@decorators.login_required +def add(): + if request.method == 'POST': + try: + db.new_job(session['user_id'], + request.form["label"], + request.form["hash"]) + return redirect(url_for('index')) + except Exception as e: + flash(e) + return render_template('newjob.html')