Created
October 31, 2013 07:29
-
-
Save mmahesh/7245561 to your computer and use it in GitHub Desktop.
models
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
users_tags_table = Table('users_tags', Base.metadata, | |
Column('users_id', Integer, ForeignKey('users.id')), | |
Column('tags_id', Integer, ForeignKey('tags.id')) | |
) | |
users_tags_table = Table('users_groups', Base.metadata, | |
Column('users_id', Integer, ForeignKey('users.id')), | |
Column('groups_id', Integer, ForeignKey('groups.id')) | |
) | |
class Users(Base): | |
"User model should contain" | |
__tablename__ = "users" | |
id = Column(Integer, primary_key=True) | |
name = Column(Unicode(255), nullable=False) | |
username = Column(Unicode(255),nullable=False) | |
email_primary = Column(Unicode(255), nullable=False) | |
email_secondary = Column(Unicode(255), nullable=True) | |
_password = Column(Unicode(255),nullable=False) | |
bio = Column(Text, nullable=True) | |
tags = relationship("Tags", | |
secondary=users_tags_table, | |
backref="users") | |
groups = relationship("Groups", | |
secondary =users_groups_table, | |
backref="users") | |
spam_user = Column(Boolean,server_default='FALSE') | |
def check_password(self, password): | |
return self.password == password | |
@property | |
def password(self): | |
return self._password | |
@classmethod | |
def all(cls): | |
return DBSession.query(Users).order_by(sa.desc(Users.created)) | |
@classmethod | |
def by_id(cls, id): | |
return DBSession.query(Users).filter(Users.id == id).first() | |
@classmethod | |
def by_username(cls, username): | |
return DBSession.query(Users).filter(Users.username == username).first() | |
groups_tags_table = Table('groups_tags', Base.metadata, | |
Column('groups_id', Integer, ForeignKey('groups.id')), | |
Column('tags_id', Integer, ForeignKey('tags.id')) | |
) | |
groups_users_table = Table('groups_users', Base.metadata, | |
Column('groups_id', Integer, ForeignKey('groups.id')), | |
Column('users_id', Integer, ForeignKey('users.id')) | |
) | |
class Groups(Base): | |
__tablename__ = "groups" | |
id = Column(Integer, primary_key=True) | |
name = Column(Unicode(), nullable=False) | |
username = Column(Unicode(255),unique=True,nullable=False,index=True) | |
created = Column(DateTime, default=datetime.datetime.utcnow) | |
tags = relationship("Tags", | |
secondary=groups_tags_table, | |
backref="Groups") | |
groups_members = relationship("Users", | |
secondary = users_groups_table, | |
backref="Groups") | |
created = Column(DateTime, default=datetime.datetime.utcnow) | |
@classmethod | |
def all(cls): | |
return DBSession.query(Groups).order_by(sa.desc(Groups.created)) | |
@classmethod | |
def by_id(cls, id): | |
return DBSession.query(Groups).filter(Groups.id == id).first() | |
class Tags(Base): | |
__tablename__ = "tags" | |
id = Column(Integer, primary_key=True) | |
name = Column(Text, nullable=False, unique=True) | |
description = Column(Text, nullable=True) | |
@classmethod | |
def all(cls): | |
return DBSession.query(Tags).order_by(sa.desc(Tags.created)) | |
@classmethod | |
def by_id(cls, id): | |
return DBSession.query(Tags).filter(Tags.id == id).first() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment