AƱadido sistema simple de login
[webpysample.git] / db.py
diff --git a/db.py b/db.py
index 23d4713..628a2b8 100644 (file)
--- a/db.py
+++ b/db.py
@@ -2,6 +2,8 @@ from sqlalchemy import *
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.orm import sessionmaker, scoped_session
 
+from hashlib import sha256 as sha
+
 Base = declarative_base()
 metadata = Base.metadata
 
@@ -15,6 +17,22 @@ class Test(Base):
     def __init__(self, test):
         self.test = test
 
+class User(Base):
+    __tablename__ = 'users'
+
+    name = Column(String(60), primary_key=True)
+    password = Column(String(128))
+
+    def __init__(self, name, password):
+        self.name = unicode(name)
+        self.password = unicode(sha(password).hexdigest())
+
+    def auth(self, password):
+        return self.password == sha(password).hexdigest()
+
+    def __unicode__(self):
+        return '<User "%s">' % self.name
+
 def connect(database='sqlite:///database.sqlite'):
     db = create_engine(database, echo=False)
     session = scoped_session(sessionmaker(bind=db))