Autenticación, sesiones, templates y Sub-Aplicaciones en Web.py con PostgreSQL

Como puedo usar sesión, autenticación, templates y sub-app en un proyecto?

Solución:

Crear una sub-app admin.py:

  1 # -*- coding:utf-8 -*-
  2 import web
  3 
  4 urls = (
  5     "", "re",
  6     "/", "dashboard"
  7 )
  8 
  9 render = web.template.render("templates")
 10 
 11 # Decorador
 12 def Session(url):
 13     def check(*args, **kwargs):
 14         """
 15             Decorador usado para verificar que 
 16             la sesión este activa.
 17         """
 18         print "Verify....", web.ctx.homepath
 19         try:
 20             if not web.ctx.session.loggedin:
 21                 pass
 22                 #raise web.seeother(web.ctx.homedomain) 
 23         except: raise web.seeother(web.ctx.homedomain)
 24         return url(*args, **kwargs)
 25     return check
 26 
 27 class re:
 28     def GET(self): raise web.seeother('/')
 29 
 30 class dashboard:
 31     @Session
 32     def GET(self):
 33         # you can access the session information
 34         web.ctx.session.count += 1
 35         return render.dashboard()
 36 
 37 
 38 app_admin = web.application(urls, locals())

Crear el código principal en code.py:

  1 # -*- coding: utf-8 -*-
  2 """
  3 Author: Jorge A. Toro <jolthgs@gmail.com>
  4 
  5 Session with Sub-App and DBStore - Web.py 0.37
  6 
  7 Create Database "test1" and "tsessions" table with the following schema into PostgreSQL:
  8 
  9 $ createdb test1
 10 $ psql -d test1
 11 
 12 create table tsessions (
 13     session_id char(128) UNIQUE NOT NULL,
 14     atime timestamp NOT NULL default current_timestamp,
 15     data text
 16 );
 17 
 18 create table privileges (
 19         id SERIAL,
 20         name VARCHAR(50) NOT NULL UNIQUE,
 21         PRIMARY KEY (id)
 22 );
 23 
 24 create table users (
 25     id serial primary key,
 26     username varchar(10) UNIQUE NOT NULL,
 27     password varchar(10),
 28     privilege_id INTEGER NOT NULL
 29 );
 30 
 31 ALTER TABLE users ADD CONSTRAINT privilege_id_fk FOREIGN KEY (privilege_id) 
 32 REFERENCES privileges(id) MATCH FULL ON UPDATE CASCADE ON DELETE SET NULL;
 33 
 34 insert into privileges (name) values ('admin');
 35 insert into users (username, password, privilege_id) values ('jolth','qwerty', 1);
 36 
 37 """
 38 import web
 39 import admin # Import Sub-App
 40 
 41 # DEBUG
 42 web.config.debug = False
 43 
 44 # URL's
 45 urls = (
 46     "/", "login",
 47     #"/dashboard", "dashboard",
 48     "/logout", "logout",
 49     "/admin", admin.app_admin # Admin Application
 50 )
 51 
 52 app = web.application(urls, locals())
 53 
 54 # Sessions
 55 db = web.database(dbn="postgres", db="test1", user="postgres", pw="qwerty")
 56 store = web.session.DBStore(db, "tsessions")
 57 session = web.session.Session(app, store, initializer={"count": 0})
 58 
 59 # Templates
 60 render = web.template.render("templates") #render = web.template.render("templates", globals={"session": session})
 61 
 62 # processor (web.loadhook)
 63 def session_hook():
 64     """
 65         Permite compartir la sesión a la 
 66         Sub-App.
 67     """
 68     web.ctx.session = session
 69     web.template.Template.globals['session'] = session # sessions avaible in template
 70 
 71 app.add_processor(web.loadhook(session_hook))
 72 
 73 
 74 class login:
 75     def GET(self):
 76         """
 77             Página de Login
 78         """
 79         return render.login()
 80 
 81     def POST(self):
 82         """
 83             Comprueba si el usuario se puede logear
 84             y crea atributos para la sesión.
 85         """
 86         import sys
 87         inputData = web.input()
 88 
 89         check = db.query("select * from users where username=$username and password=$password",
 90                 vars=inputData)
 91 
 92         if check:
 93             session.loggedin = True
 94             session.username = inputData.username
 95             print >> sys.stderr, "Login Successful"
 96             raise web.seeother("/admin") # call sub-app admin dashboard
 97         else:
 98             print >> sys.stderr, "Login Failed"
 99             return render.login("usuario o contraseña inválida")
100 
101 class logout:
102     def GET(self):
103         """
104             Mata la sesión
105         """
106         #session.loggedin = False
107         session.kill()
108         return 'Logout.....'
109 
110 
111 if __name__ == "__main__": app.run()

Crear un sistema de plantillas con login.html:

  1 $def with (message=None)
  2 <!doctype html>
  3 <html lang="es">
  4 <head>
  5     <meta charset="utf-8">
  6     <title>Test :: Login</title>
  7 </head>
  8 <body>
  9     <form method="post" action="/">
 10         <input type="text" name="username" placeholder="Username" required title="Se necesita un nombre de usuario">
 11         <input type="password" name="password" placeholder="Password" required title="Se necesita una contraseña">
 12         <button type="submit">Entrar</button>
 13     </form>
 14     <p>$message</p>
 15 </body>
 16 </html>

y dashboard.html:

  1 $def with ()
  2 <!doctype html>
  3 <html lang="es">
  4 <head>
  5     <title>Dashboard</title>
  6 </head>
  7 <body>
  8 usuario: $session.username
  9 
 10 count: $session.count
 11 <br>
 12 <br>
 13 <a href="/logout">Logout</a>
 14 </body>
 15 </html>

Autor: Jorge A. Toro

Deja un comentario

Blog de WordPress.com.

Subir ↑