Mercurial > kallithea
diff rhodecode/model/db.py @ 1065:5d676b6ab71c beta
Moved BaseModel into base class for declarative base. Added some handy methods into
BaseModel like get() and query()
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 19 Feb 2011 01:06:47 +0100 |
parents | 51b70569c330 |
children | 70bf0be59653 |
line wrap: on
line diff
--- a/rhodecode/model/db.py Sat Feb 19 01:05:20 2011 +0100 +++ b/rhodecode/model/db.py Sat Feb 19 01:06:47 2011 +0100 @@ -30,46 +30,14 @@ from sqlalchemy import * from sqlalchemy.exc import DatabaseError -from sqlalchemy.orm import relationship, backref, class_mapper +from sqlalchemy.orm import relationship, backref from sqlalchemy.orm.session import Session from rhodecode.model.meta import Base log = logging.getLogger(__name__) -class BaseModel(object): - - @classmethod - def _get_keys(cls): - """return column names for this model """ - return class_mapper(cls).c.keys() - - def get_dict(self): - """return dict with keys and values corresponding - to this model data """ - - d = {} - for k in self._get_keys(): - d[k] = getattr(self, k) - return d - - def get_appstruct(self): - """return list with keys and values tupples corresponding - to this model data """ - - l = [] - for k in self._get_keys(): - l.append((k, getattr(self, k),)) - return l - - def populate_obj(self, populate_dict): - """populate model with data from given populate_dict""" - - for k in self._get_keys(): - if k in populate_dict: - setattr(self, k, populate_dict[k]) - -class RhodeCodeSettings(Base, BaseModel): +class RhodeCodeSettings(Base): __tablename__ = 'rhodecode_settings' __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -84,7 +52,7 @@ return "<%s('%s:%s')>" % (self.__class__.__name__, self.app_settings_name, self.app_settings_value) -class RhodeCodeUi(Base, BaseModel): +class RhodeCodeUi(Base): __tablename__ = 'rhodecode_ui' __table_args__ = {'useexisting':True} ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -94,7 +62,7 @@ ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) -class User(Base, BaseModel): +class User(Base): __tablename__ = 'users' __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -115,11 +83,11 @@ user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') group_member = relationship('UsersGroupMember', cascade='all') + @property def full_contact(self): return '%s %s <%s>' % (self.name, self.lastname, self.email) - @property def is_admin(self): return self.admin @@ -128,6 +96,11 @@ return "<%s('id:%s:%s')>" % (self.__class__.__name__, self.user_id, self.username) + @classmethod + def by_username(cls, username): + return Session.query(cls).filter(cls.username == username).one() + + def update_lastlogin(self): """Update user lastlogin""" @@ -141,7 +114,7 @@ session.rollback() -class UserLog(Base, BaseModel): +class UserLog(Base): __tablename__ = 'user_logs' __table_args__ = {'useexisting':True} user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -160,7 +133,7 @@ repository = relationship('Repository') -class UsersGroup(Base, BaseModel): +class UsersGroup(Base): __tablename__ = 'users_groups' __table_args__ = {'useexisting':True} @@ -170,7 +143,7 @@ members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined") -class UsersGroupMember(Base, BaseModel): +class UsersGroupMember(Base): __tablename__ = 'users_groups_members' __table_args__ = {'useexisting':True} @@ -185,7 +158,7 @@ self.users_group_id = gr_id self.user_id = u_id -class Repository(Base, BaseModel): +class Repository(Base): __tablename__ = 'repositories' __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -214,7 +187,12 @@ return "<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, self.repo_name) -class Group(Base, BaseModel): + @classmethod + def by_repo_name(cls, repo_name): + return Session.query(cls).filter(cls.repo_name == repo_name).one() + + +class Group(Base): __tablename__ = 'groups' __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) @@ -233,7 +211,7 @@ return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, self.group_name) -class Permission(Base, BaseModel): +class Permission(Base): __tablename__ = 'permissions' __table_args__ = {'useexisting':True} permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -244,7 +222,7 @@ return "<%s('%s:%s')>" % (self.__class__.__name__, self.permission_id, self.permission_name) -class RepoToPerm(Base, BaseModel): +class RepoToPerm(Base): __tablename__ = 'repo_to_perm' __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -256,7 +234,7 @@ permission = relationship('Permission') repository = relationship('Repository') -class UserToPerm(Base, BaseModel): +class UserToPerm(Base): __tablename__ = 'user_to_perm' __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -267,7 +245,7 @@ permission = relationship('Permission') -class UsersGroupToPerm(Base, BaseModel): +class UsersGroupToPerm(Base): __tablename__ = 'users_group_to_perm' __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True}) users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -279,7 +257,7 @@ permission = relationship('Permission') repository = relationship('Repository') -class GroupToPerm(Base, BaseModel): +class GroupToPerm(Base): __tablename__ = 'group_to_perm' __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) @@ -292,7 +270,7 @@ permission = relationship('Permission') group = relationship('Group') -class Statistics(Base, BaseModel): +class Statistics(Base): __tablename__ = 'statistics' __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -304,7 +282,7 @@ repository = relationship('Repository', single_parent=True) -class UserFollowing(Base, BaseModel): +class UserFollowing(Base): __tablename__ = 'user_followings' __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), UniqueConstraint('user_id', 'follows_user_id') @@ -320,7 +298,7 @@ follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') follows_repository = relationship('Repository', order_by='Repository.repo_name') -class CacheInvalidation(Base, BaseModel): +class CacheInvalidation(Base): __tablename__ = 'cache_invalidation' __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -338,7 +316,7 @@ return "<%s('%s:%s')>" % (self.__class__.__name__, self.cache_id, self.cache_key) -class DbMigrateVersion(Base, BaseModel): +class DbMigrateVersion(Base): __tablename__ = 'db_migrate_version' __table_args__ = {'useexisting':True} repository_id = Column('repository_id', String(250), primary_key=True)