##// END OF EJS Templates
Added get_all method to BaseModel, requires to define cls variable in child models
marcink -
r2521:e4d2271e beta
parent child Browse files
Show More
@@ -42,7 +42,6 b''
42 # along with this program. If not, see <http://www.gnu.org/licenses/>.
42 # along with this program. If not, see <http://www.gnu.org/licenses/>.
43
43
44 import logging
44 import logging
45 from rhodecode.model.db import User, Repository, Permission
46 from rhodecode.model import meta
45 from rhodecode.model import meta
47
46
48 log = logging.getLogger(__name__)
47 log = logging.getLogger(__name__)
@@ -68,11 +67,13 b' class BaseModel(object):'
68 :param sa: If passed it reuses this session instead of creating a new one
67 :param sa: If passed it reuses this session instead of creating a new one
69 """
68 """
70
69
70 cls = None # override in child class
71
71 def __init__(self, sa=None):
72 def __init__(self, sa=None):
72 if sa is not None:
73 if sa is not None:
73 self.sa = sa
74 self.sa = sa
74 else:
75 else:
75 self.sa = meta.Session
76 self.sa = meta.Session()
76
77
77 def _get_instance(self, cls, instance, callback=None):
78 def _get_instance(self, cls, instance, callback=None):
78 """
79 """
@@ -104,6 +105,7 b' class BaseModel(object):'
104 :param user:
105 :param user:
105 :type user: UserID, username, or User instance
106 :type user: UserID, username, or User instance
106 """
107 """
108 from rhodecode.model.db import User
107 return self._get_instance(User, user,
109 return self._get_instance(User, user,
108 callback=User.get_by_username)
110 callback=User.get_by_username)
109
111
@@ -114,6 +116,7 b' class BaseModel(object):'
114 :param repository:
116 :param repository:
115 :type repository: RepoID, repository name or Repository Instance
117 :type repository: RepoID, repository name or Repository Instance
116 """
118 """
119 from rhodecode.model.db import Repository
117 return self._get_instance(Repository, repository,
120 return self._get_instance(Repository, repository,
118 callback=Repository.get_by_repo_name)
121 callback=Repository.get_by_repo_name)
119
122
@@ -124,5 +127,12 b' class BaseModel(object):'
124 :param permission:
127 :param permission:
125 :type permission: PermissionID, permission_name or Permission instance
128 :type permission: PermissionID, permission_name or Permission instance
126 """
129 """
130 from rhodecode.model.db import Permission
127 return self._get_instance(Permission, permission,
131 return self._get_instance(Permission, permission,
128 callback=Permission.get_by_key)
132 callback=Permission.get_by_key)
133
134 def get_all(self):
135 """
136 Returns all instances of what is defined in `cls` class variable
137 """
138 return self.cls.getAll()
General Comments 0
You need to be logged in to leave comments. Login now