def login_verify_by_token(self, source, token): """ 通过第三方token进行验证 :param source: 用户来源 :param token: 第三方Token :return: 成功返回用户对象,否则None """ if source in self._source: if source == 'qq': if not (UserMongo.objects(qq_token=token).update( set__last_login_time=datetime.datetime.now) and User.objects.filter(qq_token=token).update( set__last_login_time=datetime.datetime.now)): return None elif source == 'wechat': if not (UserMongo.objects(wechat_token=token).update( set__last_login_time=datetime.datetime.now) and User.objects.filter(wechat_token=token).update( set__last_login_time=datetime.datetime.now)): return None return True else: # 来源不正确 self._error_response = HttpResponse(json.dumps(self._error_msg['source_error'])) return
def new_user_handler(self, source, token, username): """ 第三方新用户处理器 :param source: 用户来源 :return: token: 第三方Token """ assert source in self._source and token != '' if source == 'qq': UserMongo(username=username, reg_source=source, qq_token=token).save() elif source == 'wechat': UserMongo(username=username, reg_source=source, wechat_token=token).save()
def _on_follow_mongo(self, user, follow_id): """ 关注时的数据库处理程序,mongo后端 :param user: 已验证的用户对象 :param follow_id: 目标用户ID: :return: 成功返回真,失败返回假 """ assert isinstance(user, UserMongo) and follow_id > 0 # 重置错误对象 self._follow_error_code_mongo = None # 查询是否已关注过目标用户 if FollowMongo.objects(uid=user.id, follow_id=follow_id): # 已关注情形 self._follow_error_code_mongo = self._error_msg[ 'follow_done_error']['code'] return False # 处理请求 following = FollowMongo(uid=user.id, follow_uid=follow_id) following.save() if [ user.update(push__followings=follow_id, inc__following_num=1), UserMongo.objects(id=follow_id).update(push__followers=user.id, inc__follower_num=1) ] == [None, None]: # 处理失败 self._follow_error_code_mongo = self._error_msg[ 'follow_general_error']['code'] return False return True
def _on_collect_mongo(self, user, market_id): """ 收藏市场时的数据库处理程序,mongo后端 :param user: 已验证的用户对象 :param market_id: 市场ID: :return: 成功返回真,失败返回假 """ assert isinstance(user, UserMongo) and market_id > 0 # 重置错误对象 self._follow_error_code_mongo = None # 查询是否已关注过目标用户 if not FollowMongo.objects(uid=user.id, follow_id=market_id): # 未关注情形 self._follow_error_code_mongo = self._error_msg[ 'follow_none_error']['code'] return False # 处理请求 if [ user.update(pull__followings=follow_id, dec__following_num=1), UserMongo.objects(id=follow_id).update(pull__followers=user.id, dec__follower_num=1) ] == [None, None]: # 处理失败 self._follow_error_code_mongo = self._error_msg[ 'follow_general_error']['code'] return False return True
def register_database_handler_mongo(self, username, password, mobile): """ 注册时的mongo数据库处理器 :return: 成功返回真,失败返回假 """ # 写入数据库 new_user = UserMongo(username=username) new_user.password = make_password(password) new_user.mobile = mobile new_user.last_login_time = datetime.datetime.now new_user.save() return True
def _on_publish_topic_mongo(self, title, content, users): """ 发表主题时的数据库处理程序,mongo后端 :param title: 标题 :param content: 内容 :param users: 已验证的用户对象 :return: 成功返回真,失败返回假 """ assert title != '' and content != '' and len( users) >= 1 and isinstance(users[0], UserMongo) new_topic = TopicMongo(title=title, content=content, author=users[0]) new_topic.save() result = UserMongo.objects(id=users[0].id).update( push__topics=new_topic, inc__topic_num=1) return result is not None
def unfollow_user(self, request): """ 取消关注用户 :param request 请求 :return: """ # 验证权限 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 获取参数 follow_id = request.POST.get('id', 0) if follow_id == 0: return HttpResponse(json.dumps( self._error_msg['follow_uid_error'])) # 查询是否已关注过目标用户 follow = FollowMongo.objects(uid=user.id, follow_id=follow_id).first() if not follow: return HttpResponse( json.dumps(self._error_msg['follow_none_error'])) # 处理请求 if not user.update(pull__followings=follow_id, dec__following_num=1) or not UserMongo.objects( id=follow_id).update(pull__followers=user.id, dec__follower_num=1): return HttpResponse(json.dumps(self._error_msg['general_error'])) follow.delete() # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))