Last active
December 15, 2015 03:58
-
-
Save marcelnicolay/5197773 to your computer and use it in GitHub Desktop.
Torneira model example with repository methods and session queries
This file contains 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
# coding: utf-8 | |
import hashlib | |
from datetime import datetime | |
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime | |
from torneira.core.meta import TorneiraSession | |
from torneira.models.base import Model, Repository | |
class UserNotFound(Exception): | |
pass | |
class UserRepository(Repository): | |
def do_login(self, login, password): | |
session = TorneiraSession() | |
try: | |
user_query = session.query(User).filter(User.login == login)\ | |
.filter(User.password == hashlib.md5(password).hexdigest()) | |
user = user_query.one() | |
except NoResultFound: | |
raise UserNotFound | |
return user | |
def change_password(self, old_password, new_password): | |
if self.spasswordenha != hashlib.md5(old_password).hexdigest(): | |
return False | |
self.password = hashlib.md5(new_password).hexdigest() | |
self.save() | |
return True | |
class User(Model, UserRepository): | |
""" | |
Classe que define o objeto Usuário | |
""" | |
__tablename__ = 'user' | |
__table_args__ = {"extend_existing": True} | |
PENDING = 0 | |
AVAILABLE = 1 | |
id = Column('id_user', Integer, primary_key=True, nullable=False) | |
name = Column(String(100), nullable=False) | |
login = Column(String(40), nullable=False, unique=True) | |
password = Column(String(255), nullable=False) | |
email = Column(String(255), nullable=False) | |
cpf = Column(String(255), nullable=False) | |
created_ad = Column(DateTime, nullable=False, default=datetime.now) | |
status = Column(Integer, default=1, nullable=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment