Mercurial > kallithea
comparison rhodecode/model/users_group.py @ 1586:2ccb32ddcfd7 beta
Add API for repositories and groups (creation, permission)
author | Nicolas VINOT <aeris@imirhil.fr> |
---|---|
date | Sun, 02 Oct 2011 17:39:52 +0200 |
parents | |
children | 8898a79ac628 |
comparison
equal
deleted
inserted
replaced
1585:56276c716599 | 1586:2ccb32ddcfd7 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 """ | |
3 rhodecode.model.users_group | |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 | |
6 users group model for RhodeCode | |
7 | |
8 :created_on: Oct 1, 2011 | |
9 :author: nvinot | |
10 :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> | |
11 :license: GPLv3, see COPYING for more details. | |
12 """ | |
13 # This program is free software: you can redistribute it and/or modify | |
14 # it under the terms of the GNU General Public License as published by | |
15 # the Free Software Foundation, either version 3 of the License, or | |
16 # (at your option) any later version. | |
17 # | |
18 # This program is distributed in the hope that it will be useful, | |
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 # GNU General Public License for more details. | |
22 # | |
23 # You should have received a copy of the GNU General Public License | |
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | |
26 import logging | |
27 import traceback | |
28 | |
29 from rhodecode.model import BaseModel | |
30 from rhodecode.model.caching_query import FromCache | |
31 from rhodecode.model.db import UsersGroupMember, UsersGroup | |
32 | |
33 log = logging.getLogger(__name__) | |
34 | |
35 class UsersGroupModel(BaseModel): | |
36 | |
37 def get(self, users_group_id, cache=False): | |
38 users_group = UsersGroup.query() | |
39 if cache: | |
40 users_group = users_group.options(FromCache("sql_cache_short", | |
41 "get_users_group_%s" % users_group_id)) | |
42 return users_group.get(users_group_id) | |
43 | |
44 def get_by_name(self, name, cache=False, case_insensitive=False): | |
45 users_group = UsersGroup.query() | |
46 if case_insensitive: | |
47 users_group = users_group.filter(UsersGroup.users_group_name.ilike(name)) | |
48 else: | |
49 users_group = users_group.filter(UsersGroup.users_group_name == name) | |
50 if cache: | |
51 users_group = users_group.options(FromCache("sql_cache_short", | |
52 "get_users_group_%s" % name)) | |
53 return users_group.scalar() | |
54 | |
55 def create(self, form_data): | |
56 try: | |
57 new_users_group = UsersGroup() | |
58 for k, v in form_data.items(): | |
59 setattr(new_users_group, k, v) | |
60 | |
61 self.sa.add(new_users_group) | |
62 self.sa.commit() | |
63 return new_users_group | |
64 except: | |
65 log.error(traceback.format_exc()) | |
66 self.sa.rollback() | |
67 raise | |
68 | |
69 def add_user_to_group(self, users_group, user): | |
70 try: | |
71 users_group_member = UsersGroupMember() | |
72 users_group_member.user = user | |
73 users_group_member.users_group = users_group | |
74 | |
75 self.sa.add(users_group_member) | |
76 self.sa.commit() | |
77 return users_group_member | |
78 except: | |
79 log.error(traceback.format_exc()) | |
80 self.sa.rollback() | |
81 raise |