##// END OF EJS Templates
Share common getter functions in base model, and remove duplicated functions from other models
marcink -
r2432:d3ac7491 codereview
parent child Browse files
Show More
@@ -42,7 +42,7 b''
42 42 # along with this program. If not, see <http://www.gnu.org/licenses/>.
43 43
44 44 import logging
45
45 from rhodecode.model.db import User, Repository, Permission
46 46 from rhodecode.model import meta
47 47
48 48 log = logging.getLogger(__name__)
@@ -96,3 +96,33 b' class BaseModel(object):'
96 96 )
97 97 else:
98 98 return callback(instance)
99
100 def _get_user(self, user):
101 """
102 Helper method to get user by ID, or username fallback
103
104 :param user:
105 :type user: UserID, username, or User instance
106 """
107 return self._get_instance(User, user,
108 callback=User.get_by_username)
109
110 def _get_repo(self, repository):
111 """
112 Helper method to get repository by ID, or repository name
113
114 :param repository:
115 :type repository: RepoID, repository name or Repository Instance
116 """
117 return self._get_instance(Repository, repository,
118 callback=Repository.get_by_repo_name)
119
120 def _get_perm(self, permission):
121 """
122 Helper method to get permission by ID, or permission name
123
124 :param permission:
125 :type permission: PermissionID, permission_name or Permission instance
126 """
127 return self._get_instance(Permission, permission,
128 callback=Permission.get_by_key)
@@ -24,13 +24,9 b''
24 24
25 25
26 26 import logging
27 import traceback
28 27
29 from pylons.i18n.translation import _
30
31 from rhodecode.lib.utils2 import safe_unicode
32 28 from rhodecode.model import BaseModel
33 from rhodecode.model.db import ChangesetStatus, Repository, User
29 from rhodecode.model.db import ChangesetStatus
34 30
35 31 log = logging.getLogger(__name__)
36 32
@@ -40,13 +36,6 b' class ChangesetStatusModel(BaseModel):'
40 36 def __get_changeset_status(self, changeset_status):
41 37 return self._get_instance(ChangesetStatus, changeset_status)
42 38
43 def __get_repo(self, repository):
44 return self._get_instance(Repository, repository,
45 callback=Repository.get_by_repo_name)
46
47 def __get_user(self, user):
48 return self._get_instance(User, user, callback=User.get_by_username)
49
50 39 def get_status(self, repo, revision):
51 40 """
52 41 Returns status of changeset for given revision and version 0
@@ -58,7 +47,7 b' class ChangesetStatusModel(BaseModel):'
58 47 :param revision: 40char hash
59 48 :type revision: str
60 49 """
61 repo = self.__get_repo(repo)
50 repo = self._get_repo(repo)
62 51
63 52 status = ChangesetStatus.query()\
64 53 .filter(ChangesetStatus.repo == repo)\
@@ -84,7 +73,7 b' class ChangesetStatusModel(BaseModel):'
84 73 :param comment:
85 74 :type comment:
86 75 """
87 repo = self.__get_repo(repo)
76 repo = self._get_repo(repo)
88 77
89 78 cur_statuses = ChangesetStatus.query()\
90 79 .filter(ChangesetStatus.repo == repo)\
@@ -95,8 +84,8 b' class ChangesetStatusModel(BaseModel):'
95 84 st.version += 1
96 85 self.sa.add(st)
97 86 new_status = ChangesetStatus()
98 new_status.author = self.__get_user(user)
99 new_status.repo = self.__get_repo(repo)
87 new_status.author = self._get_user(user)
88 new_status.repo = self._get_repo(repo)
100 89 new_status.status = status
101 90 new_status.revision = revision
102 91 new_status.comment = comment
@@ -42,9 +42,6 b' log = logging.getLogger(__name__)'
42 42
43 43 class NotificationModel(BaseModel):
44 44
45 def __get_user(self, user):
46 return self._get_instance(User, user, callback=User.get_by_username)
47
48 45 def __get_notification(self, notification):
49 46 if isinstance(notification, Notification):
50 47 return notification
@@ -77,12 +74,12 b' class NotificationModel(BaseModel):'
77 74 if recipients and not getattr(recipients, '__iter__', False):
78 75 raise Exception('recipients must be a list of iterable')
79 76
80 created_by_obj = self.__get_user(created_by)
77 created_by_obj = self._get_user(created_by)
81 78
82 79 if recipients:
83 80 recipients_objs = []
84 81 for u in recipients:
85 obj = self.__get_user(u)
82 obj = self._get_user(u)
86 83 if obj:
87 84 recipients_objs.append(obj)
88 85 recipients_objs = set(recipients_objs)
@@ -126,7 +123,7 b' class NotificationModel(BaseModel):'
126 123 # we don't want to remove actual notification just the assignment
127 124 try:
128 125 notification = self.__get_notification(notification)
129 user = self.__get_user(user)
126 user = self._get_user(user)
130 127 if notification and user:
131 128 obj = UserNotification.query()\
132 129 .filter(UserNotification.user == user)\
@@ -140,29 +137,29 b' class NotificationModel(BaseModel):'
140 137 raise
141 138
142 139 def get_for_user(self, user):
143 user = self.__get_user(user)
140 user = self._get_user(user)
144 141 return user.notifications
145 142
146 143 def mark_all_read_for_user(self, user):
147 user = self.__get_user(user)
144 user = self._get_user(user)
148 145 UserNotification.query()\
149 146 .filter(UserNotification.read == False)\
150 147 .update({'read': True})
151 148
152 149 def get_unread_cnt_for_user(self, user):
153 user = self.__get_user(user)
150 user = self._get_user(user)
154 151 return UserNotification.query()\
155 152 .filter(UserNotification.read == False)\
156 153 .filter(UserNotification.user == user).count()
157 154
158 155 def get_unread_for_user(self, user):
159 user = self.__get_user(user)
156 user = self._get_user(user)
160 157 return [x.notification for x in UserNotification.query()\
161 158 .filter(UserNotification.read == False)\
162 159 .filter(UserNotification.user == user).all()]
163 160
164 161 def get_user_notification(self, user, notification):
165 user = self.__get_user(user)
162 user = self._get_user(user)
166 163 notification = self.__get_notification(notification)
167 164
168 165 return UserNotification.query()\
@@ -48,9 +48,6 b' log = logging.getLogger(__name__)'
48 48
49 49 class RepoModel(BaseModel):
50 50
51 def __get_user(self, user):
52 return self._get_instance(User, user, callback=User.get_by_username)
53
54 51 def __get_users_group(self, users_group):
55 52 return self._get_instance(UsersGroup, users_group,
56 53 callback=UsersGroup.get_by_group_name)
@@ -59,14 +56,6 b' class RepoModel(BaseModel):'
59 56 return self._get_instance(RepoGroup, repos_group,
60 57 callback=RepoGroup.get_by_group_name)
61 58
62 def __get_repo(self, repository):
63 return self._get_instance(Repository, repository,
64 callback=Repository.get_by_repo_name)
65
66 def __get_perm(self, permission):
67 return self._get_instance(Permission, permission,
68 callback=Permission.get_by_key)
69
70 59 @LazyProperty
71 60 def repos_path(self):
72 61 """
@@ -86,7 +75,7 b' class RepoModel(BaseModel):'
86 75 return repo.scalar()
87 76
88 77 def get_repo(self, repository):
89 return self.__get_repo(repository)
78 return self._get_repo(repository)
90 79
91 80 def get_by_repo_name(self, repo_name, cache=False):
92 81 repo = self.sa.query(Repository)\
@@ -311,7 +300,7 b' class RepoModel(BaseModel):'
311 300 run_task(tasks.create_repo_fork, form_data, cur_user)
312 301
313 302 def delete(self, repo):
314 repo = self.__get_repo(repo)
303 repo = self._get_repo(repo)
315 304 try:
316 305 self.sa.delete(repo)
317 306 self.__delete_repo(repo)
@@ -328,9 +317,9 b' class RepoModel(BaseModel):'
328 317 :param user: Instance of User, user_id or username
329 318 :param perm: Instance of Permission, or permission_name
330 319 """
331 user = self.__get_user(user)
332 repo = self.__get_repo(repo)
333 permission = self.__get_perm(perm)
320 user = self._get_user(user)
321 repo = self._get_repo(repo)
322 permission = self._get_perm(perm)
334 323
335 324 # check if we have that permission already
336 325 obj = self.sa.query(UserRepoToPerm)\
@@ -353,8 +342,8 b' class RepoModel(BaseModel):'
353 342 :param user: Instance of User, user_id or username
354 343 """
355 344
356 user = self.__get_user(user)
357 repo = self.__get_repo(repo)
345 user = self._get_user(user)
346 repo = self._get_repo(repo)
358 347
359 348 obj = self.sa.query(UserRepoToPerm)\
360 349 .filter(UserRepoToPerm.repository == repo)\
@@ -372,9 +361,9 b' class RepoModel(BaseModel):'
372 361 or users group name
373 362 :param perm: Instance of Permission, or permission_name
374 363 """
375 repo = self.__get_repo(repo)
364 repo = self._get_repo(repo)
376 365 group_name = self.__get_users_group(group_name)
377 permission = self.__get_perm(perm)
366 permission = self._get_perm(perm)
378 367
379 368 # check if we have that permission already
380 369 obj = self.sa.query(UsersGroupRepoToPerm)\
@@ -399,7 +388,7 b' class RepoModel(BaseModel):'
399 388 :param group_name: Instance of UserGroup, users_group_id,
400 389 or users group name
401 390 """
402 repo = self.__get_repo(repo)
391 repo = self._get_repo(repo)
403 392 group_name = self.__get_users_group(group_name)
404 393
405 394 obj = self.sa.query(UsersGroupRepoToPerm)\
@@ -26,28 +26,17 b''
26 26
27 27 import logging
28 28 from rhodecode.model import BaseModel
29 from rhodecode.model.db import UserRepoToPerm, UsersGroupRepoToPerm, Permission,\
30 User, Repository
29 from rhodecode.model.db import UserRepoToPerm, UsersGroupRepoToPerm, \
30 Permission
31 31
32 32 log = logging.getLogger(__name__)
33 33
34 34
35 35 class RepositoryPermissionModel(BaseModel):
36 36
37 def __get_user(self, user):
38 return self._get_instance(User, user, callback=User.get_by_username)
39
40 def __get_repo(self, repository):
41 return self._get_instance(Repository, repository,
42 callback=Repository.get_by_repo_name)
43
44 def __get_perm(self, permission):
45 return self._get_instance(Permission, permission,
46 callback=Permission.get_by_key)
47
48 37 def get_user_permission(self, repository, user):
49 repository = self.__get_repo(repository)
50 user = self.__get_user(user)
38 repository = self._get_repo(repository)
39 user = self._get_user(user)
51 40
52 41 return UserRepoToPerm.query() \
53 42 .filter(UserRepoToPerm.user == user) \
@@ -39,9 +39,6 b' log = logging.getLogger(__name__)'
39 39
40 40 class ReposGroupModel(BaseModel):
41 41
42 def __get_user(self, user):
43 return self._get_instance(User, user, callback=User.get_by_username)
44
45 42 def __get_users_group(self, users_group):
46 43 return self._get_instance(UsersGroup, users_group,
47 44 callback=UsersGroup.get_by_group_name)
@@ -50,10 +47,6 b' class ReposGroupModel(BaseModel):'
50 47 return self._get_instance(RepoGroup, repos_group,
51 48 callback=RepoGroup.get_by_group_name)
52 49
53 def __get_perm(self, permission):
54 return self._get_instance(Permission, permission,
55 callback=Permission.get_by_key)
56
57 50 @LazyProperty
58 51 def repos_path(self):
59 52 """
@@ -227,8 +220,8 b' class ReposGroupModel(BaseModel):'
227 220 """
228 221
229 222 repos_group = self.__get_repos_group(repos_group)
230 user = self.__get_user(user)
231 permission = self.__get_perm(perm)
223 user = self._get_user(user)
224 permission = self._get_perm(perm)
232 225
233 226 # check if we have that permission already
234 227 obj = self.sa.query(UserRepoGroupToPerm)\
@@ -253,7 +246,7 b' class ReposGroupModel(BaseModel):'
253 246 """
254 247
255 248 repos_group = self.__get_repos_group(repos_group)
256 user = self.__get_user(user)
249 user = self._get_user(user)
257 250
258 251 obj = self.sa.query(UserRepoGroupToPerm)\
259 252 .filter(UserRepoGroupToPerm.user == user)\
@@ -274,7 +267,7 b' class ReposGroupModel(BaseModel):'
274 267 """
275 268 repos_group = self.__get_repos_group(repos_group)
276 269 group_name = self.__get_users_group(group_name)
277 permission = self.__get_perm(perm)
270 permission = self._get_perm(perm)
278 271
279 272 # check if we have that permission already
280 273 obj = self.sa.query(UsersGroupRepoGroupToPerm)\
@@ -35,8 +35,8 b' from rhodecode.lib.caching_query import '
35 35 from rhodecode.model import BaseModel
36 36 from rhodecode.model.db import User, UserRepoToPerm, Repository, Permission, \
37 37 UserToPerm, UsersGroupRepoToPerm, UsersGroupToPerm, UsersGroupMember, \
38 Notification, RepoGroup, UserRepoGroupToPerm, UsersGroup,\
39 UsersGroupRepoGroupToPerm, UserEmailMap
38 Notification, RepoGroup, UserRepoGroupToPerm, UsersGroupRepoGroupToPerm, \
39 UserEmailMap
40 40 from rhodecode.lib.exceptions import DefaultUserException, \
41 41 UserOwnsReposException
42 42
@@ -61,13 +61,6 b' PERM_WEIGHTS = {'
61 61
62 62 class UserModel(BaseModel):
63 63
64 def __get_user(self, user):
65 return self._get_instance(User, user, callback=User.get_by_username)
66
67 def __get_perm(self, permission):
68 return self._get_instance(Permission, permission,
69 callback=Permission.get_by_key)
70
71 64 def get(self, user_id, cache=False):
72 65 user = self.sa.query(User)
73 66 if cache:
@@ -76,7 +69,7 b' class UserModel(BaseModel):'
76 69 return user.get(user_id)
77 70
78 71 def get_user(self, user):
79 return self.__get_user(user)
72 return self._get_user(user)
80 73
81 74 def get_by_username(self, username, cache=False, case_insensitive=False):
82 75
@@ -292,7 +285,7 b' class UserModel(BaseModel):'
292 285 raise
293 286
294 287 def delete(self, user):
295 user = self.__get_user(user)
288 user = self._get_user(user)
296 289
297 290 try:
298 291 if user.username == 'default':
@@ -545,7 +538,7 b' class UserModel(BaseModel):'
545 538 raise Exception('perm needs to be an instance of Permission class '
546 539 'got %s instead' % type(perm))
547 540
548 user = self.__get_user(user)
541 user = self._get_user(user)
549 542
550 543 return UserToPerm.query().filter(UserToPerm.user == user)\
551 544 .filter(UserToPerm.permission == perm).scalar() is not None
@@ -557,8 +550,8 b' class UserModel(BaseModel):'
557 550 :param user:
558 551 :param perm:
559 552 """
560 user = self.__get_user(user)
561 perm = self.__get_perm(perm)
553 user = self._get_user(user)
554 perm = self._get_perm(perm)
562 555 # if this permission is already granted skip it
563 556 _perm = UserToPerm.query()\
564 557 .filter(UserToPerm.user == user)\
@@ -578,8 +571,8 b' class UserModel(BaseModel):'
578 571 :param user:
579 572 :param perm:
580 573 """
581 user = self.__get_user(user)
582 perm = self.__get_perm(perm)
574 user = self._get_user(user)
575 perm = self._get_perm(perm)
583 576
584 577 obj = UserToPerm.query()\
585 578 .filter(UserToPerm.user == user)\
@@ -595,7 +588,7 b' class UserModel(BaseModel):'
595 588 :param user:
596 589 :param email:
597 590 """
598 user = self.__get_user(user)
591 user = self._get_user(user)
599 592 obj = UserEmailMap()
600 593 obj.user = user
601 594 obj.email = email
@@ -609,7 +602,7 b' class UserModel(BaseModel):'
609 602 :param user:
610 603 :param email_id:
611 604 """
612 user = self.__get_user(user)
605 user = self._get_user(user)
613 606 obj = UserEmailMap.query().get(email_id)
614 607 if obj:
615 608 self.sa.delete(obj) No newline at end of file
@@ -37,17 +37,10 b' log = logging.getLogger(__name__)'
37 37
38 38 class UsersGroupModel(BaseModel):
39 39
40 def __get_user(self, user):
41 return self._get_instance(User, user, callback=User.get_by_username)
42
43 40 def __get_users_group(self, users_group):
44 41 return self._get_instance(UsersGroup, users_group,
45 42 callback=UsersGroup.get_by_group_name)
46 43
47 def __get_perm(self, permission):
48 return self._get_instance(Permission, permission,
49 callback=Permission.get_by_key)
50
51 44 def get(self, users_group_id, cache=False):
52 45 return UsersGroup.get(users_group_id)
53 46
@@ -115,7 +108,7 b' class UsersGroupModel(BaseModel):'
115 108
116 109 def add_user_to_group(self, users_group, user):
117 110 users_group = self.__get_users_group(users_group)
118 user = self.__get_user(user)
111 user = self._get_user(user)
119 112
120 113 for m in users_group.members:
121 114 u = m.user
@@ -138,7 +131,7 b' class UsersGroupModel(BaseModel):'
138 131
139 132 def remove_user_from_group(self, users_group, user):
140 133 users_group = self.__get_users_group(users_group)
141 user = self.__get_user(user)
134 user = self._get_user(user)
142 135
143 136 users_group_member = None
144 137 for m in users_group.members:
@@ -160,7 +153,7 b' class UsersGroupModel(BaseModel):'
160 153
161 154 def has_perm(self, users_group, perm):
162 155 users_group = self.__get_users_group(users_group)
163 perm = self.__get_perm(perm)
156 perm = self._get_perm(perm)
164 157
165 158 return UsersGroupToPerm.query()\
166 159 .filter(UsersGroupToPerm.users_group == users_group)\
@@ -187,7 +180,7 b' class UsersGroupModel(BaseModel):'
187 180
188 181 def revoke_perm(self, users_group, perm):
189 182 users_group = self.__get_users_group(users_group)
190 perm = self.__get_perm(perm)
183 perm = self._get_perm(perm)
191 184
192 185 obj = UsersGroupToPerm.query()\
193 186 .filter(UsersGroupToPerm.users_group == users_group)\
General Comments 0
You need to be logged in to leave comments. Login now