def test_create_and_remove(self): usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe', email=u'*****@*****.**', name=u'u1', lastname=u'u1') Session.commit() self.assertEqual(User.get_by_username(u'test_user'), usr) # make users group users_group = UsersGroupModel().create('some_example_group') Session.commit() UsersGroupModel().add_user_to_group(users_group, usr) Session.commit() self.assertEqual(UsersGroup.get(users_group.users_group_id), users_group) self.assertEqual(UsersGroupMember.query().count(), 1) UserModel().delete(usr.user_id) Session.commit() self.assertEqual(UsersGroupMember.query().all(), [])
def add_user_to_group(self, users_group, user): users_group = self.__get_users_group(users_group) user = self.__get_user(user) for m in users_group.members: u = m.user if u.user_id == user.user_id: return True try: users_group_member = UsersGroupMember() users_group_member.user = user users_group_member.users_group = users_group users_group.members.append(users_group_member) user.group_member.append(users_group_member) self.sa.add(users_group_member) return users_group_member except: log.error(traceback.format_exc()) raise
def update(self, users_group, form_data): try: users_group = self.__get_users_group(users_group) for k, v in form_data.items(): if k == 'users_group_members': users_group.members = [] self.sa.flush() members_list = [] if v: v = [v] if isinstance(v, basestring) else v for u_id in set(v): member = UsersGroupMember(users_group.users_group_id, u_id) members_list.append(member) setattr(users_group, 'members', members_list) setattr(users_group, k, v) self.sa.add(users_group) except: log.error(traceback.format_exc()) raise