Created
February 17, 2016 12:31
-
-
Save pauleveritt/2a1d9040a585ee1b4acc to your computer and use it in GitHub Desktop.
SQLAlchemy models for resources
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
class ToDo(BaseObject): | |
__tablename__ = 'todo' | |
id = Column(Integer, primary_key=True) | |
title = Column(Text) | |
acl = Column(ArrayType) | |
default_acl = [ | |
(Allow, Everyone, 'view'), | |
(Allow, 'group:editors', 'edit') | |
] | |
owner_id = Column(Integer, ForeignKey('users.id')) | |
owner = relationship('User') | |
def __acl__(self=None): | |
return getattr(self, 'acl', None) or ToDo.default_acl | |
@classmethod | |
def by_id(cls, todo_id): | |
# Do an int() just in case '1' was passed in | |
return Session.query(cls).filter_by(id=int(todo_id)).first() | |
@classmethod | |
def list(cls): | |
return Session.query(cls).order_by(cls.title) | |
def todo_factory(request): | |
todo_id = request.matchdict.get('id') | |
if todo_id is None: | |
# Return the class | |
return ToDo | |
todo = ToDo.by_id(int(todo_id)) | |
if not todo: | |
raise HTTPNotFound() | |
return todo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment