Last active
August 29, 2015 14:18
-
-
Save 2garryn/e8ec0ec4f23d099045ca to your computer and use it in GitHub Desktop.
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
class User(Base): | |
__tablename__ = 'users' | |
id = Column(Integer, primary_key=True) | |
name = Column(String(32), unique=True, nullable=False) | |
email = Column(String(64), unique=True, nullable=False) | |
password = Column(String(64), nullable=False) | |
class Device(Base): | |
__tablename__ = 'devices' | |
id = Column(Integer, primary_key=True) | |
owner_id = Column(Integer, ForeignKey(User.id)) | |
device_name = Column(String(32), nullable=False) | |
device_type = Column(Enum(*definitions.DEVICE_TYPES), nullable=False) | |
device_id = Column(String(36), primary_key=True) |
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
def device_register(session, username, password, device_name, device_type): | |
try: | |
user = session.query(User).filter(User.name == username and User.password == password).one() | |
uid = str(uuid.uuid1()) | |
new_device = Device(owner_id = user.id, device_name = device_name, device_type = device_type, device_id = uid) | |
session.add(new_device) | |
session.commit() | |
return True, uid | |
except NoResultFound as e: | |
session.rollback() | |
return False, "Wrong username or password" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment