changeset 10:58a993029d9f

Dodawanie nowych zadań i dekorator sprawdzający zalogowanie.
author Michał Rudowicz <michal.rudowicz@fl9.eu>
date Mon, 07 Mar 2011 10:47:48 +0100
parents 9a0a9fa7f91d
children 69ecfe51d585
files frontend/mfrontend/db.py frontend/mfrontend/decorators.py frontend/mfrontend/templates/layout.html frontend/mfrontend/templates/newjob.html frontend/mfrontend/views/frontend.py frontend/mfrontend/views/jobcontrol.py
diffstat 6 files changed, 68 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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()
--- /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
--- 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 @@
  <!doctype html>
 <title>NASZA APLIKACJA!</title>
 <div class=page>
-  <h1>NO HEJ</h1>
+    <h1>NO HEJ</h1>
   <div class=metanav>
   {% if not session.logged_in %}
   <a href="{{ url_for('frontend.login') }}">Zaloguj się</a> | 
   <a href="{{ url_for('frontend.register') }}">Załóż konto</a>
   {% else %}
-  <a href="{{ url_for('frontend.logout') }}">Wyloguj się</a> | 
-  <a href="{{ url_for('jobcontrol.index') }}">Lista zadań</a>
+  <a href="{{ url_for('frontend.index') }}">Strona główna</a> | 
+  <a href="{{ url_for('jobcontrol.index') }}">Lista zadań</a> | 
+  <a href="{{ url_for('jobcontrol.add') }}">Dodaj zadanie</a> | 
+  <a href="{{ url_for('frontend.logout') }}">Wyloguj się</a>
   {% endif %}
   </div>
   {% for message in get_flashed_messages() %}
--- /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 %}
+  <h2>Login</h2>
+  {% if error %}<p class=error><strong>Błąd:</strong> {{ error }}{% endif %}
+  <form action="{{ url_for('add') }}" method=post>
+    <dl>
+      <dt>Opis zadania:
+      <dd><input type=text name=label>
+      <dt>Hash:
+      <dd><input type=text name=hash>
+      <dd><input type=submit value=Dodaj>
+    </dl>
+  </form>
+{% endblock %} 
--- 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'))
 
--- 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')