changeset 3:79fc37fe85a6

Zabawy z Flaskiem ciąg dalszy, zaczyna to jakoś wyglądać
author michalr
date Tue, 22 Feb 2011 19:25:10 +0000
parents e0061735c327
children cf786ee26a22
files frontend/config.ini frontend/mfrontend/templates/layout.html frontend/mfrontend/templates/login.html frontend/mfrontend/utils.py frontend/mfrontend/views/frontend.py
diffstat 5 files changed, 66 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontend/config.ini	Tue Feb 22 19:25:10 2011 +0000
@@ -0,0 +1,9 @@
+[Basic]
+# W przypadku sqlite, ścieżka do pliku bazy
+Database = ../baza.sqlite
+# Wygenerować losowy dla każdej instalacji bazy
+Secret_Key = TakBardzoTajne
+# Dla poważnej instalacji ustawić na 0
+Debug = 1
+# Algorytm wyliczający hashe dla haseł
+HashCrypto = sha1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontend/mfrontend/templates/layout.html	Tue Feb 22 19:25:10 2011 +0000
@@ -0,0 +1,16 @@
+ <!doctype html>
+<title>Flaskr</title>
+<div class=page>
+  <h1>NO HEJ</h1>
+  <div class=metanav>
+  {% if not session.logged_in %}
+    <a href="{{ url_for('login') }}">log in</a>
+  {% else %}
+    <a href="{{ url_for('logout') }}">log out</a>
+  {% endif %}
+  </div>
+  {% for message in get_flashed_messages() %}
+    <div class=flash>{{ message }}</div>
+  {% endfor %}
+  {% block body %}{% endblock %}
+</div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontend/mfrontend/templates/login.html	Tue Feb 22 19:25:10 2011 +0000
@@ -0,0 +1,14 @@
+{% extends "layout.html" %}
+{% block body %}
+  <h2>Login</h2>
+  {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
+  <form action="{{ url_for('login') }}" method=post>
+    <dl>
+      <dt>Username:
+      <dd><input type=text name=username>
+      <dt>Password:
+      <dd><input type=password name=password>
+      <dd><input type=submit value=Login>
+    </dl>
+  </form>
+{% endblock %} 
--- a/frontend/mfrontend/utils.py	Tue Feb 22 18:37:32 2011 +0000
+++ b/frontend/mfrontend/utils.py	Tue Feb 22 19:25:10 2011 +0000
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-import random, string
+import random, string, hashlib
 from ConfigParser import SafeConfigParser
 
 def randomString(n):
@@ -16,9 +16,19 @@
     """Funkcja tworząca domyślny plik .ini z ustawieniami aplikacji"""
     config = SafeConfigParser()
     config.add_section('Basic')
-    config.set('Basic', 'Database', '../baza.sqlite')
+    config.set('Basic', 'Database', 'baza.sqlite')
     config.set('Basic', 'Debug', '0')
     config.set('Basic', 'Secret_Key', randomString(10))
+    config.set('Basic', 'HashCrypto', 'sha512')
     with open('config.ini', 'wb') as configfile:
         config.write(configfile)
 
+def hashPassword(password):
+    """Funkcja hashuje hasła."""
+    config = SafeConfigParser()
+    config.read('config.ini')
+    m = hashlib.new(config.get('Basic', 'HashCrypto'))
+    m.update(config.get('Basic', 'Secret_Key'))
+    m.update(password)
+    return m.hexdigest()
+
--- a/frontend/mfrontend/views/frontend.py	Tue Feb 22 18:37:32 2011 +0000
+++ b/frontend/mfrontend/views/frontend.py	Tue Feb 22 19:25:10 2011 +0000
@@ -1,8 +1,21 @@
-from flask import Module
-from mfrontend import db
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+from flask import Module, render_template, request, url_for
+from mfrontend import db, utils
 frontend = Module(__name__)
 
 @frontend.route('/')
 def index():
     lol = db.query_db('select id, username, password from users')
     return lol[0]['username']
+
+@frontend.route('/login', methods=['GET', 'POST'])
+def login():
+    if request.method == 'POST':
+        return "Uname: {0}<br />Passwd: {1}<br />Hashpwd: {2}".format(
+            request.form['username'],
+            request.form['password'],
+            utils.hashPassword(request.form['password']))
+    return render_template('login.html')
+