Created
May 5, 2012 20:09
-
-
Save shazow/2605272 to your computer and use it in GitHub Desktop.
Listopi snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# listopi/test/__init__.py | |
from unittest import TestCase | |
from pyramid import testing | |
from sqlalchemy import create_engine | |
from listopi.model import metadata, Session | |
class TestModel(TestCase): | |
def setUp(self): | |
self.config = testing.setUp() | |
engine = create_engine('sqlite://') | |
Session.configure(bind=engine) | |
metadata.create_all(engine) | |
def tearDown(self): | |
Session.remove() | |
testing.tearDown() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# listopi/model/objects.py | |
from .meta import BaseModel | |
from listopi.lib.helpers import random_string | |
from sqlalchemy import orm, types | |
from sqlalchemy import Column, ForeignKey, Index, PrimaryKeyConstraint | |
from datetime import datetime | |
import scrypt | |
import logging | |
__all__ = ['User'] | |
log = logging.getLogger(__name__) | |
class User(BaseModel): | |
__tablename__ = 'user' | |
id = Column(types.Integer, primary_key=True) | |
time_created = Column(types.DateTime, default=datetime.now, nullable=False) | |
time_updated = Column(types.DateTime, onupdate=datetime.now) | |
is_admin = Column(types.Boolean, default=False, nullable=False) | |
# Email (with a token for verification and recovery) | |
email = Column(types.Unicode, nullable=False, index=True, unique=True) | |
email_token = Column(types.String(16), default=lambda: random_string(16), nullable=False) | |
password_hash = Column(types.String, nullable=False) | |
def set_password(self, password, salt_length=64, maxtime=0.0001): | |
""" | |
:param password: | |
Password to set for the user. | |
:param salt_length: | |
A random string of this length will be generated and encrypted | |
using the password. | |
:param maxtime: | |
Minimum time spent encrypting the password. This is very low by | |
default (0.01 seconds). Pass in a larger value in production | |
(preferably configured in the .ini) | |
""" | |
self.password_hash = scrypt.encrypt(random_string(salt_length), password, maxtime=maxtime) | |
def compare_password(self, password, maxtime=0.5): | |
""" | |
:param password: | |
Password input to compare against. | |
:param maxtime: | |
Must be larger than the time used to encrypt the original password. | |
""" | |
try: | |
scrypt.decrypt(self.password_hash, password, maxtime=maxtime) | |
return True | |
except scrypt.error: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment