Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add User model

  • Loading branch information...
commit 41c10e8c9b80c26387886be4a303407f52680bea 1 parent e58100c
Marc Lagrange rhaamo authored
Showing with 54 additions and 8 deletions.
  1. +2 −1  codular/models/__init__.py
  2. +52 −7 codular/models/user.py
3  codular/models/__init__.py
View
@@ -1,4 +1,5 @@
# Package
from .meta import DBSession
-from .user import MyModel
+from .user import User
+
59 codular/models/user.py
View
@@ -1,16 +1,61 @@
-from .meta import Base
+from .meta import (Base, DBSession)
+
+from sqlalchemy.orm import (
+ synonym,
+)
from sqlalchemy import (
Column,
Index,
Integer,
Text,
+ Unicode
)
-class MyModel(Base):
- __tablename__ = 'models'
- id = Column(Integer, primary_key=True)
- name = Column(Text)
- value = Column(Integer)
+import cryptacular.bcrypt
+
+crypt = cryptacular.bcrypt.BCRYPTPasswordManager()
+
+"""
+Return a unicode hash of the password, using BCrypt from cryptacular
+"""
+def hash_password(password):
+ return unicode(crypt.encode(password))
+
+class User(Base):
+ """
+ User model
+ """
+ __tablename__ = 'users'
+ user_id = Column(Integer, primary_key=True)
+ username = Column(Unicode(20), unique=True)
+ name = Column(Unicode(50))
+ email = Column(Unicode(50))
+
+ _password = Column('password', Unicode(60))
+
+ def _get_password(self):
+ return self._password
+
+ def _set_password(self, password):
+ self._password = hash_password(password)
+
+ password = property(_get_password, _set_password)
+ password = synonym('_password', descriptor=password)
+
+ def __init__(self, username, password, name, email):
+ self.username = username
+ self.name = name
+ self.email = email
+ self.password = password
+
+ @classmethod
+ def get_by_username(cls, username):
+ return DBSession.query(cls).filter(cls.username == username).first
-Index('my_index', MyModel.name, unique=True, mysql_length=255)
+ @classmethod
+ def check_password(cls, username, password):
+ user = cls.get_by_username(username)
+ if not user:
+ return False
+ return crypt.check(user.password, password)
Please sign in to comment.
Something went wrong with that request. Please try again.