Show More
@@ -31,7 +31,7 b' from datetime import date' | |||||
31 |
|
31 | |||
32 | from sqlalchemy import * |
|
32 | from sqlalchemy import * | |
33 | from sqlalchemy.exc import DatabaseError |
|
33 | from sqlalchemy.exc import DatabaseError | |
34 | from sqlalchemy.orm import relationship, backref, joinedload |
|
34 | from sqlalchemy.orm import relationship, backref, joinedload, class_mapper | |
35 | from sqlalchemy.orm.interfaces import MapperExtension |
|
35 | from sqlalchemy.orm.interfaces import MapperExtension | |
36 |
|
36 | |||
37 | from beaker.cache import cache_region, region_invalidate |
|
37 | from beaker.cache import cache_region, region_invalidate | |
@@ -42,22 +42,91 b' from vcs.exceptions import RepositoryErr' | |||||
42 | from vcs.utils.lazy import LazyProperty |
|
42 | from vcs.utils.lazy import LazyProperty | |
43 | from vcs.nodes import FileNode |
|
43 | from vcs.nodes import FileNode | |
44 |
|
44 | |||
45 | from rhodecode.lib import str2bool |
|
45 | from rhodecode.lib import str2bool, json | |
46 | from rhodecode.model.meta import Base, Session |
|
46 | from rhodecode.model.meta import Base, Session | |
47 | from rhodecode.model.caching_query import FromCache |
|
47 | from rhodecode.model.caching_query import FromCache | |
48 |
|
48 | |||
49 | log = logging.getLogger(__name__) |
|
49 | log = logging.getLogger(__name__) | |
50 |
|
50 | |||
51 | #============================================================================== |
|
51 | #============================================================================== | |
52 | # MAPPER EXTENSIONS |
|
52 | # BASE CLASSES | |
53 | #============================================================================== |
|
53 | #============================================================================== | |
54 |
|
54 | |||
55 | class RepositoryMapper(MapperExtension): |
|
55 | class ModelSerializer(json.JSONEncoder): | |
56 | def after_update(self, mapper, connection, instance): |
|
56 | """ | |
57 | pass |
|
57 | Simple Serializer for JSON, | |
|
58 | ||||
|
59 | usage:: | |||
|
60 | ||||
|
61 | to make object customized for serialization implement a __json__ | |||
|
62 | method that will return a dict for serialization into json | |||
|
63 | ||||
|
64 | example:: | |||
|
65 | ||||
|
66 | class Task(object): | |||
|
67 | ||||
|
68 | def __init__(self, name, value): | |||
|
69 | self.name = name | |||
|
70 | self.value = value | |||
|
71 | ||||
|
72 | def __json__(self): | |||
|
73 | return dict(name=self.name, | |||
|
74 | value=self.value) | |||
|
75 | ||||
|
76 | """ | |||
|
77 | ||||
|
78 | def default(self, obj): | |||
|
79 | ||||
|
80 | if hasattr(obj, '__json__'): | |||
|
81 | return obj.__json__() | |||
|
82 | else: | |||
|
83 | return json.JSONEncoder.default(self, obj) | |||
|
84 | ||||
|
85 | class BaseModel(object): | |||
|
86 | """Base Model for all classess | |||
|
87 | ||||
|
88 | """ | |||
|
89 | ||||
|
90 | @classmethod | |||
|
91 | def _get_keys(cls): | |||
|
92 | """return column names for this model """ | |||
|
93 | return class_mapper(cls).c.keys() | |||
|
94 | ||||
|
95 | def get_dict(self): | |||
|
96 | """return dict with keys and values corresponding | |||
|
97 | to this model data """ | |||
|
98 | ||||
|
99 | d = {} | |||
|
100 | for k in self._get_keys(): | |||
|
101 | d[k] = getattr(self, k) | |||
|
102 | return d | |||
|
103 | ||||
|
104 | def get_appstruct(self): | |||
|
105 | """return list with keys and values tupples corresponding | |||
|
106 | to this model data """ | |||
|
107 | ||||
|
108 | l = [] | |||
|
109 | for k in self._get_keys(): | |||
|
110 | l.append((k, getattr(self, k),)) | |||
|
111 | return l | |||
|
112 | ||||
|
113 | def populate_obj(self, populate_dict): | |||
|
114 | """populate model with data from given populate_dict""" | |||
|
115 | ||||
|
116 | for k in self._get_keys(): | |||
|
117 | if k in populate_dict: | |||
|
118 | setattr(self, k, populate_dict[k]) | |||
|
119 | ||||
|
120 | @classmethod | |||
|
121 | def query(cls): | |||
|
122 | return Session.query(cls) | |||
|
123 | ||||
|
124 | @classmethod | |||
|
125 | def get(cls, id_): | |||
|
126 | return Session.query(cls).get(id_) | |||
58 |
|
127 | |||
59 |
|
128 | |||
60 | class RhodeCodeSettings(Base): |
|
129 | class RhodeCodeSettings(Base, BaseModel): | |
61 | __tablename__ = 'rhodecode_settings' |
|
130 | __tablename__ = 'rhodecode_settings' | |
62 | __table_args__ = (UniqueConstraint('app_settings_name'), {'extend_existing':True}) |
|
131 | __table_args__ = (UniqueConstraint('app_settings_name'), {'extend_existing':True}) | |
63 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
132 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -109,7 +178,7 b' class RhodeCodeSettings(Base):' | |||||
109 | return fd |
|
178 | return fd | |
110 |
|
179 | |||
111 |
|
180 | |||
112 | class RhodeCodeUi(Base): |
|
181 | class RhodeCodeUi(Base, BaseModel): | |
113 | __tablename__ = 'rhodecode_ui' |
|
182 | __tablename__ = 'rhodecode_ui' | |
114 | __table_args__ = {'extend_existing':True} |
|
183 | __table_args__ = {'extend_existing':True} | |
115 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
184 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -124,7 +193,7 b' class RhodeCodeUi(Base):' | |||||
124 | return Session.query(cls).filter(cls.ui_key == key) |
|
193 | return Session.query(cls).filter(cls.ui_key == key) | |
125 |
|
194 | |||
126 |
|
195 | |||
127 | class User(Base): |
|
196 | class User(Base, BaseModel): | |
128 | __tablename__ = 'users' |
|
197 | __tablename__ = 'users' | |
129 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True}) |
|
198 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True}) | |
130 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
199 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -183,7 +252,7 b' class User(Base):' | |||||
183 | session.rollback() |
|
252 | session.rollback() | |
184 |
|
253 | |||
185 |
|
254 | |||
186 | class UserLog(Base): |
|
255 | class UserLog(Base, BaseModel): | |
187 | __tablename__ = 'user_logs' |
|
256 | __tablename__ = 'user_logs' | |
188 | __table_args__ = {'extend_existing':True} |
|
257 | __table_args__ = {'extend_existing':True} | |
189 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
258 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -202,7 +271,7 b' class UserLog(Base):' | |||||
202 | repository = relationship('Repository') |
|
271 | repository = relationship('Repository') | |
203 |
|
272 | |||
204 |
|
273 | |||
205 | class UsersGroup(Base): |
|
274 | class UsersGroup(Base, BaseModel): | |
206 | __tablename__ = 'users_groups' |
|
275 | __tablename__ = 'users_groups' | |
207 | __table_args__ = {'extend_existing':True} |
|
276 | __table_args__ = {'extend_existing':True} | |
208 |
|
277 | |||
@@ -226,7 +295,7 b' class UsersGroup(Base):' | |||||
226 | "get_user_%s" % group_name)) |
|
295 | "get_user_%s" % group_name)) | |
227 | return gr.scalar() |
|
296 | return gr.scalar() | |
228 |
|
297 | |||
229 | class UsersGroupMember(Base): |
|
298 | class UsersGroupMember(Base, BaseModel): | |
230 | __tablename__ = 'users_groups_members' |
|
299 | __tablename__ = 'users_groups_members' | |
231 | __table_args__ = {'extend_existing':True} |
|
300 | __table_args__ = {'extend_existing':True} | |
232 |
|
301 | |||
@@ -241,10 +310,9 b' class UsersGroupMember(Base):' | |||||
241 | self.users_group_id = gr_id |
|
310 | self.users_group_id = gr_id | |
242 | self.user_id = u_id |
|
311 | self.user_id = u_id | |
243 |
|
312 | |||
244 | class Repository(Base): |
|
313 | class Repository(Base, BaseModel): | |
245 | __tablename__ = 'repositories' |
|
314 | __tablename__ = 'repositories' | |
246 | __table_args__ = (UniqueConstraint('repo_name'), {'extend_existing':True},) |
|
315 | __table_args__ = (UniqueConstraint('repo_name'), {'extend_existing':True},) | |
247 | __mapper_args__ = {'extension':RepositoryMapper()} |
|
|||
248 |
|
316 | |||
249 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
317 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
250 | repo_name = Column("repo_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
318 | repo_name = Column("repo_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
@@ -440,7 +508,7 b' class Repository(Base):' | |||||
440 | return repo |
|
508 | return repo | |
441 |
|
509 | |||
442 |
|
510 | |||
443 | class Group(Base): |
|
511 | class Group(Base, BaseModel): | |
444 | __tablename__ = 'groups' |
|
512 | __tablename__ = 'groups' | |
445 | __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), |
|
513 | __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), | |
446 | CheckConstraint('group_id != group_parent_id'), {'extend_existing':True},) |
|
514 | CheckConstraint('group_id != group_parent_id'), {'extend_existing':True},) | |
@@ -516,7 +584,7 b' class Group(Base):' | |||||
516 |
|
584 | |||
517 | return cnt + children_count(self) |
|
585 | return cnt + children_count(self) | |
518 |
|
586 | |||
519 | class Permission(Base): |
|
587 | class Permission(Base, BaseModel): | |
520 | __tablename__ = 'permissions' |
|
588 | __tablename__ = 'permissions' | |
521 | __table_args__ = {'extend_existing':True} |
|
589 | __table_args__ = {'extend_existing':True} | |
522 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
590 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -531,7 +599,7 b' class Permission(Base):' | |||||
531 | def get_by_key(cls, key): |
|
599 | def get_by_key(cls, key): | |
532 | return Session.query(cls).filter(cls.permission_name == key).scalar() |
|
600 | return Session.query(cls).filter(cls.permission_name == key).scalar() | |
533 |
|
601 | |||
534 | class RepoToPerm(Base): |
|
602 | class RepoToPerm(Base, BaseModel): | |
535 | __tablename__ = 'repo_to_perm' |
|
603 | __tablename__ = 'repo_to_perm' | |
536 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'extend_existing':True}) |
|
604 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'extend_existing':True}) | |
537 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
605 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -543,7 +611,7 b' class RepoToPerm(Base):' | |||||
543 | permission = relationship('Permission') |
|
611 | permission = relationship('Permission') | |
544 | repository = relationship('Repository') |
|
612 | repository = relationship('Repository') | |
545 |
|
613 | |||
546 | class UserToPerm(Base): |
|
614 | class UserToPerm(Base, BaseModel): | |
547 | __tablename__ = 'user_to_perm' |
|
615 | __tablename__ = 'user_to_perm' | |
548 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'extend_existing':True}) |
|
616 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'extend_existing':True}) | |
549 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
617 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -588,7 +656,7 b' class UserToPerm(Base):' | |||||
588 | except: |
|
656 | except: | |
589 | Session.rollback() |
|
657 | Session.rollback() | |
590 |
|
658 | |||
591 | class UsersGroupRepoToPerm(Base): |
|
659 | class UsersGroupRepoToPerm(Base, BaseModel): | |
592 | __tablename__ = 'users_group_repo_to_perm' |
|
660 | __tablename__ = 'users_group_repo_to_perm' | |
593 | __table_args__ = (UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), {'extend_existing':True}) |
|
661 | __table_args__ = (UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), {'extend_existing':True}) | |
594 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
662 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -601,7 +669,7 b' class UsersGroupRepoToPerm(Base):' | |||||
601 | repository = relationship('Repository') |
|
669 | repository = relationship('Repository') | |
602 |
|
670 | |||
603 |
|
671 | |||
604 | class UsersGroupToPerm(Base): |
|
672 | class UsersGroupToPerm(Base, BaseModel): | |
605 | __tablename__ = 'users_group_to_perm' |
|
673 | __tablename__ = 'users_group_to_perm' | |
606 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
674 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
607 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
675 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) | |
@@ -649,7 +717,7 b' class UsersGroupToPerm(Base):' | |||||
649 | Session.rollback() |
|
717 | Session.rollback() | |
650 |
|
718 | |||
651 |
|
719 | |||
652 | class GroupToPerm(Base): |
|
720 | class GroupToPerm(Base, BaseModel): | |
653 | __tablename__ = 'group_to_perm' |
|
721 | __tablename__ = 'group_to_perm' | |
654 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True}) |
|
722 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True}) | |
655 |
|
723 | |||
@@ -662,7 +730,7 b' class GroupToPerm(Base):' | |||||
662 | permission = relationship('Permission') |
|
730 | permission = relationship('Permission') | |
663 | group = relationship('Group') |
|
731 | group = relationship('Group') | |
664 |
|
732 | |||
665 | class Statistics(Base): |
|
733 | class Statistics(Base, BaseModel): | |
666 | __tablename__ = 'statistics' |
|
734 | __tablename__ = 'statistics' | |
667 | __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing':True}) |
|
735 | __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing':True}) | |
668 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
736 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -674,7 +742,7 b' class Statistics(Base):' | |||||
674 |
|
742 | |||
675 | repository = relationship('Repository', single_parent=True) |
|
743 | repository = relationship('Repository', single_parent=True) | |
676 |
|
744 | |||
677 | class UserFollowing(Base): |
|
745 | class UserFollowing(Base, BaseModel): | |
678 | __tablename__ = 'user_followings' |
|
746 | __tablename__ = 'user_followings' | |
679 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
747 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), | |
680 | UniqueConstraint('user_id', 'follows_user_id') |
|
748 | UniqueConstraint('user_id', 'follows_user_id') | |
@@ -692,12 +760,11 b' class UserFollowing(Base):' | |||||
692 | follows_repository = relationship('Repository', order_by='Repository.repo_name') |
|
760 | follows_repository = relationship('Repository', order_by='Repository.repo_name') | |
693 |
|
761 | |||
694 |
|
762 | |||
695 |
|
||||
696 | @classmethod |
|
763 | @classmethod | |
697 | def get_repo_followers(cls, repo_id): |
|
764 | def get_repo_followers(cls, repo_id): | |
698 | return Session.query(cls).filter(cls.follows_repo_id == repo_id) |
|
765 | return Session.query(cls).filter(cls.follows_repo_id == repo_id) | |
699 |
|
766 | |||
700 | class CacheInvalidation(Base): |
|
767 | class CacheInvalidation(Base, BaseModel): | |
701 | __tablename__ = 'cache_invalidation' |
|
768 | __tablename__ = 'cache_invalidation' | |
702 | __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing':True}) |
|
769 | __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing':True}) | |
703 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
770 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
@@ -715,7 +782,7 b' class CacheInvalidation(Base):' | |||||
715 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
782 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
716 | self.cache_id, self.cache_key) |
|
783 | self.cache_id, self.cache_key) | |
717 |
|
784 | |||
718 | class DbMigrateVersion(Base): |
|
785 | class DbMigrateVersion(Base, BaseModel): | |
719 | __tablename__ = 'db_migrate_version' |
|
786 | __tablename__ = 'db_migrate_version' | |
720 | __table_args__ = {'extend_existing':True} |
|
787 | __table_args__ = {'extend_existing':True} | |
721 | repository_id = Column('repository_id', String(250), primary_key=True) |
|
788 | repository_id = Column('repository_id', String(250), primary_key=True) |
@@ -1,6 +1,6 b'' | |||||
1 | """SQLAlchemy Metadata and Session object""" |
|
1 | """SQLAlchemy Metadata and Session object""" | |
2 | from sqlalchemy.ext.declarative import declarative_base |
|
2 | from sqlalchemy.ext.declarative import declarative_base | |
3 |
from sqlalchemy.orm import scoped_session, sessionmaker |
|
3 | from sqlalchemy.orm import scoped_session, sessionmaker | |
4 | from beaker import cache |
|
4 | from beaker import cache | |
5 |
|
5 | |||
6 | from rhodecode.model import caching_query |
|
6 | from rhodecode.model import caching_query | |
@@ -19,53 +19,8 b' Session = scoped_session(' | |||||
19 | ) |
|
19 | ) | |
20 | ) |
|
20 | ) | |
21 |
|
21 | |||
22 |
|
||||
23 | class BaseModel(object): |
|
|||
24 | """Base Model for all classess |
|
|||
25 |
|
||||
26 | """ |
|
|||
27 |
|
||||
28 | @classmethod |
|
|||
29 | def _get_keys(cls): |
|
|||
30 | """return column names for this model """ |
|
|||
31 | return class_mapper(cls).c.keys() |
|
|||
32 |
|
||||
33 | def get_dict(self): |
|
|||
34 | """return dict with keys and values corresponding |
|
|||
35 | to this model data """ |
|
|||
36 |
|
||||
37 | d = {} |
|
|||
38 | for k in self._get_keys(): |
|
|||
39 | d[k] = getattr(self, k) |
|
|||
40 | return d |
|
|||
41 |
|
||||
42 | def get_appstruct(self): |
|
|||
43 | """return list with keys and values tupples corresponding |
|
|||
44 | to this model data """ |
|
|||
45 |
|
||||
46 | l = [] |
|
|||
47 | for k in self._get_keys(): |
|
|||
48 | l.append((k, getattr(self, k),)) |
|
|||
49 | return l |
|
|||
50 |
|
||||
51 | def populate_obj(self, populate_dict): |
|
|||
52 | """populate model with data from given populate_dict""" |
|
|||
53 |
|
||||
54 | for k in self._get_keys(): |
|
|||
55 | if k in populate_dict: |
|
|||
56 | setattr(self, k, populate_dict[k]) |
|
|||
57 |
|
||||
58 | @classmethod |
|
|||
59 | def query(cls): |
|
|||
60 | return Session.query(cls) |
|
|||
61 |
|
||||
62 | @classmethod |
|
|||
63 | def get(cls, id_): |
|
|||
64 | return Session.query(cls).get(id_) |
|
|||
65 |
|
||||
66 |
|
||||
67 | # The declarative Base |
|
22 | # The declarative Base | |
68 |
Base = declarative_base( |
|
23 | Base = declarative_base() | |
69 |
|
24 | |||
70 | #to use cache use this in query |
|
25 | #to use cache use this in query | |
71 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
|
26 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
General Comments 0
You need to be logged in to leave comments.
Login now