##// END OF EJS Templates
fixed typo
marcink -
r1692:b76bb93d beta
parent child Browse files
Show More
@@ -1,90 +1,90 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.model.users_group
3 rhodecode.model.users_group
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
5
6 users group model for RhodeCode
6 users group model for RhodeCode
7
7
8 :created_on: Oct 1, 2011
8 :created_on: Oct 1, 2011
9 :author: nvinot
9 :author: nvinot
10 :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr>
10 :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
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
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
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
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/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
25
26 import logging
26 import logging
27 import traceback
27 import traceback
28
28
29 from rhodecode.lib.caching_query import FromCache
29 from rhodecode.lib.caching_query import FromCache
30
30
31 from rhodecode.model import BaseMode
31 from rhodecode.model import BaseModel
32 from rhodecode.model.db import UsersGroupMember, UsersGroup
32 from rhodecode.model.db import UsersGroupMember, UsersGroup
33
33
34 log = logging.getLogger(__name__)
34 log = logging.getLogger(__name__)
35
35
36 class UsersGroupModel(BaseModel):
36 class UsersGroupModel(BaseModel):
37
37
38 def get(self, users_group_id, cache = False):
38 def get(self, users_group_id, cache = False):
39 users_group = UsersGroup.query()
39 users_group = UsersGroup.query()
40 if cache:
40 if cache:
41 users_group = users_group.options(FromCache("sql_cache_short",
41 users_group = users_group.options(FromCache("sql_cache_short",
42 "get_users_group_%s" % users_group_id))
42 "get_users_group_%s" % users_group_id))
43 return users_group.get(users_group_id)
43 return users_group.get(users_group_id)
44
44
45 def get_by_name(self, name, cache = False, case_insensitive = False):
45 def get_by_name(self, name, cache = False, case_insensitive = False):
46 users_group = UsersGroup.query()
46 users_group = UsersGroup.query()
47 if case_insensitive:
47 if case_insensitive:
48 users_group = users_group.filter(UsersGroup.users_group_name.ilike(name))
48 users_group = users_group.filter(UsersGroup.users_group_name.ilike(name))
49 else:
49 else:
50 users_group = users_group.filter(UsersGroup.users_group_name == name)
50 users_group = users_group.filter(UsersGroup.users_group_name == name)
51 if cache:
51 if cache:
52 users_group = users_group.options(FromCache("sql_cache_short",
52 users_group = users_group.options(FromCache("sql_cache_short",
53 "get_users_group_%s" % name))
53 "get_users_group_%s" % name))
54 return users_group.scalar()
54 return users_group.scalar()
55
55
56 def create(self, form_data):
56 def create(self, form_data):
57 try:
57 try:
58 new_users_group = UsersGroup()
58 new_users_group = UsersGroup()
59 for k, v in form_data.items():
59 for k, v in form_data.items():
60 setattr(new_users_group, k, v)
60 setattr(new_users_group, k, v)
61
61
62 self.sa.add(new_users_group)
62 self.sa.add(new_users_group)
63 self.sa.commit()
63 self.sa.commit()
64 return new_users_group
64 return new_users_group
65 except:
65 except:
66 log.error(traceback.format_exc())
66 log.error(traceback.format_exc())
67 self.sa.rollback()
67 self.sa.rollback()
68 raise
68 raise
69
69
70 def add_user_to_group(self, users_group, user):
70 def add_user_to_group(self, users_group, user):
71 for m in users_group.members:
71 for m in users_group.members:
72 u = m.user
72 u = m.user
73 if u.user_id == user.user_id:
73 if u.user_id == user.user_id:
74 return m
74 return m
75
75
76 try:
76 try:
77 users_group_member = UsersGroupMember()
77 users_group_member = UsersGroupMember()
78 users_group_member.user = user
78 users_group_member.user = user
79 users_group_member.users_group = users_group
79 users_group_member.users_group = users_group
80
80
81 users_group.members.append(users_group_member)
81 users_group.members.append(users_group_member)
82 user.group_member.append(users_group_member)
82 user.group_member.append(users_group_member)
83
83
84 self.sa.add(users_group_member)
84 self.sa.add(users_group_member)
85 self.sa.commit()
85 self.sa.commit()
86 return users_group_member
86 return users_group_member
87 except:
87 except:
88 log.error(traceback.format_exc())
88 log.error(traceback.format_exc())
89 self.sa.rollback()
89 self.sa.rollback()
90 raise
90 raise
General Comments 0
You need to be logged in to leave comments. Login now