users_group.py
90 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
|
r1586 | # -*- coding: utf-8 -*- | ||
""" | ||||
rhodecode.model.users_group | ||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
users group model for RhodeCode | ||||
:created_on: Oct 1, 2011 | ||||
:author: nvinot | ||||
:copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> | ||||
:license: GPLv3, see COPYING for more details. | ||||
""" | ||||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
import logging | ||||
import traceback | ||||
r1669 | from rhodecode.lib.caching_query import FromCache | |||
from rhodecode.model import BaseMode | ||||
|
r1586 | from rhodecode.model.db import UsersGroupMember, UsersGroup | ||
|
r1593 | log = logging.getLogger(__name__) | ||
|
r1586 | |||
|
r1593 | class UsersGroupModel(BaseModel): | ||
|
r1586 | |||
|
r1593 | def get(self, users_group_id, cache = False): | ||
|
r1586 | users_group = UsersGroup.query() | ||
if cache: | ||||
|
r1593 | users_group = users_group.options(FromCache("sql_cache_short", | ||
"get_users_group_%s" % users_group_id)) | ||||
return users_group.get(users_group_id) | ||||
|
r1586 | |||
|
r1593 | def get_by_name(self, name, cache = False, case_insensitive = False): | ||
|
r1586 | users_group = UsersGroup.query() | ||
if case_insensitive: | ||||
|
r1593 | users_group = users_group.filter(UsersGroup.users_group_name.ilike(name)) | ||
|
r1586 | else: | ||
|
r1593 | users_group = users_group.filter(UsersGroup.users_group_name == name) | ||
|
r1586 | if cache: | ||
|
r1593 | users_group = users_group.options(FromCache("sql_cache_short", | ||
"get_users_group_%s" % name)) | ||||
|
r1586 | return users_group.scalar() | ||
|
r1593 | def create(self, form_data): | ||
|
r1586 | try: | ||
new_users_group = UsersGroup() | ||||
for k, v in form_data.items(): | ||||
|
r1593 | setattr(new_users_group, k, v) | ||
|
r1586 | |||
|
r1593 | self.sa.add(new_users_group) | ||
|
r1586 | self.sa.commit() | ||
return new_users_group | ||||
except: | ||||
|
r1593 | log.error(traceback.format_exc()) | ||
|
r1586 | self.sa.rollback() | ||
raise | ||||
|
r1593 | def add_user_to_group(self, users_group, user): | ||
|
r1589 | for m in users_group.members: | ||
u = m.user | ||||
if u.user_id == user.user_id: | ||||
return m | ||||
|
r1586 | try: | ||
users_group_member = UsersGroupMember() | ||||
users_group_member.user = user | ||||
users_group_member.users_group = users_group | ||||
|
r1593 | users_group.members.append(users_group_member) | ||
user.group_member.append(users_group_member) | ||||
|
r1587 | |||
|
r1593 | self.sa.add(users_group_member) | ||
|
r1586 | self.sa.commit() | ||
return users_group_member | ||||
except: | ||||
|
r1593 | log.error(traceback.format_exc()) | ||
|
r1586 | self.sa.rollback() | ||
|
r1587 | raise | ||