def wrap(item: ManageLog): # 总不能把MANAGE_OPERATION的内容换个号码,抄一遍写在上面。 # 因此选择过滤掉一些,其他全部归为一类。 if item.operation == MOP.USER_CREDIT_CHANGE: if item.note == '每日签到': # 签到得分忽略 return elif item.operation == MOP.USER_EXP_CHANGE: if item.note == '每日登录': # 签到得分忽略 return if item.operation == MOP.TOPIC_BOARD_MOVE: moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][0])]) moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][1])]) return { 'type': NOTIF_TYPE.MANAGE_INFO_ABOUT_ME, 'time': item.time, 'loc_post_type': item.related_type, 'loc_post_id': item.related_id, 'loc_post_title': None, 'sender_ids': (item.user_id, ), 'receiver_id': user_id, 'from_post_type': None, # 来源为managelog,但并非post 'from_post_id': item.id, 'related_type': item.related_type, # 提醒类型较为特殊 'related_id': item.related_id, 'data': { 'op': item.operation, 'role': item.role, 'value': item.value, 'title': None # 进行二次填充 } }
async def update(self): post = await self.post_data() print('提交的更新内容', post) try: account = UserOAuth.get(UserOAuth.login_id == post['loginId'], UserOAuth.platform == post['platform']) except UserOAuth.DoesNotExist: account = None except KeyError: account = None print('keyerror') if account: if post['state'] == str(POST_STATE.APPLY): # 该post['id']是user表中的id post_user_id = to_bin(post['id']) UserOAuth.update( user_id=post_user_id, state=POST_STATE.NORMAL).where( UserOAuth.login_id == post['loginId']).execute() self.finish(RETCODE.SUCCESS) else: print('非法参数') self.finish(RETCODE.FAILED) else: self.finish(RETCODE.FAILED) print('failed')
def get_by_token(cls, token) -> Optional['UserToken']: if isinstance(token, str): try: token = to_bin(token) except binascii.Error: return t = cls.get_by_pk(token) if t and time.time() < t.expire: return t
async def check_reset_key(cls, uid, code) -> Union['User', None]: """ 检查uid与code这一组密码重置密钥是否有效 :param uid: :param code: :return: """ if not code: return if isinstance(uid, str): uid = to_bin(uid) if isinstance(code, str): code = to_bin(code) if len(code) == 24: rkey = RK_USER_RESET_KEY_BY_USER_ID % uid if await redis.get(rkey) == code: try: u = cls.get(cls.id == uid) await redis.delete(rkey) return u except cls.DoesNotExist: pass
def get_by_token(cls, token) -> Optional['UserToken']: if isinstance(token, str): try: token = to_bin(token) except binascii.Error: return try: t = cls.get(cls.id == token, time.time() < cls.expire, cls.deleted_at.is_null(True)) except peewee.DoesNotExist: pass
def to_native(self, value, context=None): if isinstance(value, (memoryview, bytes)): return value if isinstance(value, str): is_hex = all(c in string.hexdigits for c in value) if not is_hex: raise ConversionError(self.messages['hash_hex']) if len(value) % 2 == 1: value = '0' + value return to_bin(value)
async def check_actcode(cls, uid, code): """ 检查账户激活码是否可用,若可用,激活账户 :param uid: :param code: :return: """ if not code: return if isinstance(uid, str): uid = to_bin(uid) if isinstance(code, str): code = to_bin(code) if len(code) == 8: rkey = RK_USER_ACTCODE_BY_USER_ID % uid if await redis.get(rkey) == code: try: u = cls.get(cls.id == uid, cls.group == USER_GROUP.INACTIVE) u.group = USER_GROUP.NORMAL u.save() await redis.delete(rkey) return u except cls.DoesNotExist: pass
def teardown_user_token(self: Union['BaseUserViewMixin', 'BaseView'], token=sentinel): """ invalidate the token here""" u: User = self.current_user if u: if token is None: # clear all tokens UserToken.delete().where(UserToken.user_id == u.id).execute() return if token is sentinel: # clear current token try: token = to_bin(self.get_user_token()) except binascii.Error: return UserToken.delete().where(UserToken.user_id == u.id, UserToken.id == token).execute()
def board_check(form, field): """ 检查板块是否存在,是否可以允许当前用户创建文章或写入 """ try: board_id = to_bin(field.data) except TypeError: raise ValidationError('板块ID无效') board = Board.get_by_id(board_id) if not board: raise ValidationError('板块不存在') can_post_rank = 100 if set(form.view.roles) & {'forum_master', 'superuser', 'admin'} else 0 if can_post_rank >= board.can_post_rank: return True raise ValidationError('没有权限选择此板块')
async def check_reg_code_by_email(cls, email, code: Union[str, bytes]): """ 检查账户激活码是否可用 :param uid: :param code: :return: """ if not code: return if isinstance(code, str): code = to_bin(code) if len(code) == 8: email_bytes = email.encode('utf-8') rk_code = RK_USER_REG_CODE_BY_EMAIL % email_bytes rk_times = RK_USER_REG_CODE_AVAILABLE_TIMES_BY_EMAIL % email_bytes rk_pw = RK_USER_REG_PASSWORD % email_bytes if await redis.get(rk_code) == code: # 检查可用次数,decr的返回值是执行后的 if int(await redis.decr(rk_times)) <= 0: return await cls.reg_code_cleanup(email) # 无问题,取出储存值 return (await redis.get(rk_pw)).decode('utf-8')
def get_user_by_key(self, key): if not key: return try: return User.get_by_key(to_bin(key)) except: pass
def func(info): info['related_type'] = src['type'] info['related_id'] = to_bin(src['id']) info['related_user_id'] = uid
def fetch_notif_of_log(user_id, last_manage_log_id=b'\x00'): from model.manage_log import ManageLog, MOP if last_manage_log_id is None: last_manage_log_id = b'\x00' item_lst = ManageLog.select().where( ManageLog.related_user_id == user_id, ManageLog.id > last_manage_log_id, ManageLog.operation.in_( (MOP.POST_STATE_CHANGE, MOP.USER_PASSWORD_CHANGE, MOP.USER_PASSWORD_RESET, MOP.USER_KEY_RESET, MOP.USER_GROUP_CHANGE, MOP.USER_CREDIT_CHANGE, MOP.USER_REPUTE_CHANGE, MOP.USER_NICKNAME_CHANGE, MOP.TOPIC_BOARD_MOVE, MOP.TOPIC_AWESOME_CHANGE, MOP.TOPIC_STICKY_WEIGHT_CHANGE) ) ).order_by(ManageLog.id.desc()) moves = [] def wrap(item: ManageLog): # 总不能把MANAGE_OPERATION的内容换个号码,抄一遍写在上面。 # 因此选择过滤掉一些,其他全部归为一类。 if item.operation == MOP.USER_CREDIT_CHANGE: if item.note == '每日签到': # 签到得分忽略 return elif item.operation == MOP.USER_EXP_CHANGE: if item.note == '每日登录': # 签到得分忽略 return if item.operation == MOP.TOPIC_BOARD_MOVE: moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][0])]) moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][1])]) return { 'type': NOTIF_TYPE.MANAGE_INFO_ABOUT_ME, 'time': item.time, 'loc_post_type': item.related_type, 'loc_post_id': item.related_id, 'loc_post_title': None, 'sender_ids': (item.user_id,), 'receiver_id': user_id, 'from_post_type': None, # 来源为managelog,但并非post 'from_post_id': item.id, 'related_type': item.related_type, # 提醒类型较为特殊 'related_id': item.related_id, 'data': { 'op': item.operation, 'role': item.role, 'value': item.value, 'title': None # 进行二次填充 } } ret_items = list(filter(lambda x: x, map(wrap, item_lst))) info = POST_TYPES.get_post_title_by_list(*[[i['related_type'], i['related_id']] for i in ret_items]) info2 = POST_TYPES.get_post_title_by_list(*moves) for i in ret_items: t = info.get(get_bytes_from_blob(i['related_id']), None) if t: i['data']['title'] = t if i['related_type'] == POST_TYPES.COMMENT: # 这里不是批量,可能要付出较大代价 c: Comment = Comment.get_by_id(i['related_id']) if not c: continue p = POST_TYPES.get_post(c.related_type, c.related_id) if not p: continue i['loc_post_type'] = c.related_type i['loc_post_id'] = c.related_id i['loc_post_title'] = p.get_title() i['data']['comment'] = { 'related_type': c.related_type, 'related_id': c.related_id, 'related_title': p.get_title(), 'post_number': c.post_number, 'content': c.content } if i['data']['op'] == MOP.TOPIC_BOARD_MOVE: val = i['data']['value']['change'] i['data']['move_info'] = [ info2.get(to_bin(val[0]), None), info2.get(to_bin(val[1]), None) ] return ret_items
def get_user_by_token(self: Union['BaseUserViewMixin', 'BaseView'], token) -> Type[BaseUser]: try: return User.get_by_key(to_bin(token)) except: pass