# -*- 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/>.importloggingimporttracebackfromrhodecode.lib.caching_queryimportFromCachefromrhodecode.modelimportBaseModefromrhodecode.model.dbimportUsersGroupMember,UsersGrouplog=logging.getLogger(__name__)classUsersGroupModel(BaseModel):defget(self,users_group_id,cache=False):users_group=UsersGroup.query()ifcache:users_group=users_group.options(FromCache("sql_cache_short","get_users_group_%s"%users_group_id))returnusers_group.get(users_group_id)defget_by_name(self,name,cache=False,case_insensitive=False):users_group=UsersGroup.query()ifcase_insensitive:users_group=users_group.filter(UsersGroup.users_group_name.ilike(name))else:users_group=users_group.filter(UsersGroup.users_group_name==name)ifcache:users_group=users_group.options(FromCache("sql_cache_short","get_users_group_%s"%name))returnusers_group.scalar()defcreate(self,form_data):try:new_users_group=UsersGroup()fork,vinform_data.items():setattr(new_users_group,k,v)self.sa.add(new_users_group)self.sa.commit()returnnew_users_groupexcept:log.error(traceback.format_exc())self.sa.rollback()raisedefadd_user_to_group(self,users_group,user):forminusers_group.members:u=m.userifu.user_id==user.user_id:returnmtry:users_group_member=UsersGroupMember()users_group_member.user=userusers_group_member.users_group=users_groupusers_group.members.append(users_group_member)user.group_member.append(users_group_member)self.sa.add(users_group_member)self.sa.commit()returnusers_group_memberexcept:log.error(traceback.format_exc())self.sa.rollback()raise