def query_by_username(self, session_key, username): ''' username 기반 쿼리 함수. 다른 사용자의 정보를 알아내는 데 사용한다. @type session_key: string @param session_key: 사용자 Login Session @type username: string @param username: User ID to send Query @rtype: ttypes.PublicUserInformation @return: 1. 쿼리 성공: query_dic 2. 쿼리 실패: 1. 존재하지 않는 아이디: InvalidOperation('QUERY_ID_NOT_EXIST') 2. 로그인되지 않은 유저: InvalidOperation('NOT_LOGGEDIN') 3. 데이터베이스 오류: InvalidOperation('DATABASE_ERROR') ''' # TODO: Exception 내용 정정 username = smart_unicode(username) session = model.Session() query_user = self._get_user(session, username, 'Query username does not exist') query_user_dict = filter_dict(query_user.__dict__, USER_QUERY_WHITELIST) if query_user_dict['last_logout_time']: query_user_dict['last_logout_time'] = datetime2timestamp( query_user_dict['last_logout_time']) else: query_user_dict['last_logout_time'] = 0 session.close() return PublicUserInformation(**query_user_dict)
def get_info(self, session_key): ''' 회원 정보 수정을 위해 현재 로그인된 회원 자신의 정보를 가져오는 함수. 다른 사용자의 정보를 열람하는 query와 다름. @type session_key: string @param session_key: 사용자 Login Session @rtype: ttypes.UserInformation @return: 1. 가져오기 성공: user_dic 2. 가져오기 실패: 1. 로그인되지 않은 유저: InvalidOperation('NOT_LOGGEDIN') 2. 존재하지 않는 회원: InvalidOperation('MEMBER_NOT_EXIST') 3. 데이터베이스 오류: InvalidOperation('DATABASE_ERROR') ''' session = model.Session() username = self.engine.login_manager.get_session(session_key).username username = smart_unicode(username) user = self._get_user(session, username, 'member does not exist') user_dict = filter_dict(user.__dict__, USER_PUBLIC_WHITELIST) if user_dict['last_logout_time']: user_dict['last_logout_time'] = datetime2timestamp( user_dict['last_logout_time']) else: user_dict['last_logout_time'] = 0 session.close() return UserInformation(**user_dict)
def query_by_nick(self, session_key, nickname): ''' nickname 기반 쿼리 함수. 다른 사용자의 정보를 알아내는 데 사용한다. @type session_key: string @param session_key: 사용자 Login Session @type username: string @param nickname: User Nickname to send Query @rtype: ttypes.PublicUserInformation @return: 1. 쿼리 성공: query_dic 2. 쿼리 실패: 1. 존재하지 않는 닉네임: InvalidOperation('QUERY_NICK_NOT_EXIST' 2. 로그인되지 않은 유저: InvalidOperation('NOT_LOGGEDIN' 3. 데이터베이스 오류: InvalidOperation('DATABASE_ERROR' ''' # TODO: 쿼리 밖으로 빼기 nickname = smart_unicode(nickname) session = model.Session() try: query_user = session.query(model.User).filter_by( nickname=nickname).filter_by(deleted=False).one() query_user_dict = filter_dict(query_user.__dict__, USER_QUERY_WHITELIST) if query_user_dict['last_logout_time']: query_user_dict['last_logout_time'] = datetime2timestamp( query_user_dict['last_logout_time']) else: query_user_dict['last_logout_time'] = 0 session.close() return PublicUserInformation(**query_user_dict) except InvalidRequestError: session.close() raise InvalidOperation('query nickname does not exist')
def get_bbs_managers(self, board_name): ''' 현 게시판 관리자(들)을 return한다. 프론트엔드에서 게시판 정보도 함께 나타내기 위해 board_name도 포함되어있다. @type board_name: string @param board_name: 관리자를 확인할 게시판 @rtype: list<PublicUserInformation> @return: 보드의 관리자들의 list ''' session = model.Session() board_id = self.get_board_id(board_name) try: bbs_managers = [] managements = session.query(model.BBSManager).filter_by(board_id=board_id).all() for management in managements: query = session.query(model.User).filter_by(id=management.manager_id).one() manager_dict = filter_dict(query.__dict__, USER_QUERY_WHITELIST) if manager_dict['last_logout_time']: manager_dict['last_logout_time'] = datetime2timestamp(manager_dict['last_logout_time']) else: manager_dict['last_logout_time'] = 0 manager = PublicUserInformation(**manager_dict) bbs_managers.append(manager) session.close() return bbs_managers except InvalidRequestError: session.close() raise InvalidOperation('bbs manager not exist')
def _get_dict(self, item, whitelist=None): item_dict = item.__dict__ if 'author_id' in item_dict: item_dict['author_username'] = item.author.username item_dict['author_nickname'] = item.author.nickname del item_dict['author_id'] if 'board_id' in item_dict: item_dict['board_name'] = item.board.board_name del item_dict['board_id'] if 'heading_id' in item_dict: if item.heading == None: item_dict['heading'] = u'' else: item_dict['heading'] = item.heading.heading if 'root_id' in item_dict: if not item_dict['root_id']: item_dict['root_id'] = item_dict['id'] if 'date' in item_dict: item_dict['date'] = datetime2timestamp(item_dict['date']) if 'last_modified_date' in item_dict: item_dict['last_modified_date'] = datetime2timestamp( item_dict['last_modified_date']) if whitelist: filtered_dict = filter_dict(item_dict, whitelist) else: filtered_dict = item_dict return filtered_dict
def _get_dict(self, item, whitelist=None, blacklist_users=None): ''' @type item: model.Message @param item: dictionary 로 바꿀 객체 (여기서는 메시지) @type whitelist: list<string> @param whitelist: dictionary 에 남아있을 필드의 목록 @type blacklist_users: list<string> @param blacklist_users: Blacklist 로 등록되어 있는 사용자의 목록 @rtype: dict @return: item 에서 whitelist 에 있는 필드만 남기고 적절히 dictionary 로 변환한 결과물 ''' item_dict = item.__dict__ if 'from_id' in item_dict: item_dict['from_'] = item.from_user.username item_dict['from_nickname'] = item.from_user.nickname del item_dict['from_id'] if 'to_id' in item_dict: item_dict['to'] = item.to_user.username item_dict['to_nickname'] = item.to_user.nickname del item_dict['to_id'] if 'sent_time' in item_dict: item_dict['sent_time']=datetime2timestamp( item_dict['sent_time']) if blacklist_users: if 'from_' in item_dict: if item_dict['from_'] in blacklist_users: item_dict['blacklisted'] = True else: item_dict['blacklisted'] = False if whitelist: filtered_dict = filter_dict(item_dict, whitelist) else: filtered_dict = item_dict return filtered_dict
def _get_dict(self, item, whitelist=None): item_dict = item.__dict__ if 'author_id' in item_dict: item_dict['author_username'] = item.author.username item_dict['author_nickname'] = item.author.nickname del item_dict['author_id'] if 'board_id' in item_dict: item_dict['board_name'] = item.board.board_name del item_dict['board_id'] if 'heading_id' in item_dict: if item.heading == None: item_dict['heading'] = u'' else: item_dict['heading'] = item.heading.heading if 'root_id' in item_dict: if not item_dict['root_id']: item_dict['root_id'] = item_dict['id'] if 'date' in item_dict: item_dict['date'] = datetime2timestamp(item_dict['date']) if 'last_modified_date' in item_dict: item_dict['last_modified_date'] = datetime2timestamp(item_dict['last_modified_date']) if whitelist: filtered_dict = filter_dict(item_dict, whitelist) else: filtered_dict = item_dict return filtered_dict
def _get_dict(self, item, whitelist=None): item_dict = item.__dict__ if 'issued_date' in item_dict: item_dict['issued_date'] = datetime2timestamp(item_dict['issued_date']) if 'due_date' in item_dict: item_dict['due_date'] = datetime2timestamp(item_dict['due_date']) if whitelist: filtered_dict = filter_dict(item_dict, whitelist) else: filtered_dict = item_dict return filtered_dict
def _get_dict(self, item, whitelist=None): item_dict = item.__dict__ if 'issued_date' in item_dict: item_dict['issued_date'] = datetime2timestamp( item_dict['issued_date']) if 'due_date' in item_dict: item_dict['due_date'] = datetime2timestamp(item_dict['due_date']) if whitelist: filtered_dict = filter_dict(item_dict, whitelist) else: filtered_dict = item_dict return filtered_dict
def _get_dict(self, item, whitelist=None): ''' @type item: model.User @param item: dictionary 로 바꿀 객체 (여기서는 사용자) @type whitelist: list @param whitelist: dictionary 에 남아있을 필드의 목록 @rtype: dict @return: item 에서 whitelist 에 있는 필드만 남기고 적절히 dictionary 로 변환한 결과물 ''' item_dict = item.__dict__ if whitelist: filtered_dict = filter_dict(item_dict, whitelist) else: filtered_dict = item_dict return filtered_dict
def _get_dict(self, item, whitelist): ''' @type item: model.Blacklist @type whitelist: list<string> @rtype: ttypes.BlacklistInformation ''' item_dict = item.__dict__ if item_dict['user_id']: item_dict['username'] = item.user.username del item_dict['user_id'] if item_dict['blacklisted_user_id']: item_dict['blacklisted_user_username'] = item.target_user.username item_dict['blacklisted_user_nickname'] = item.target_user.nickname del item_dict['blacklisted_user_id'] item_dict['last_modified_date'] = \ datetime2timestamp(item_dict['last_modified_date']) item_dict['blacklisted_date'] = \ datetime2timestamp(item_dict['blacklisted_date']) filtered_dict = filter_dict(item_dict, whitelist) return BlacklistInformation(**filtered_dict)
def test_filter_dict(self): self.assertEqual({'user_id': 1}, libs.filter_dict({ 'user_id': 1, 'dummy': 'garbage' }, ['user_id']))
def test_filter_dict(self): self.assertEqual({'user_id': 1}, libs.filter_dict({'user_id': 1, 'dummy': 'garbage'}, ['user_id']))