def follow_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'])) # 处理请求 if not self._on_follow(users, follow_id): return self._follow_error_response # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def digg_post(self, request): """ 赞贴子 :param request: 请求 :return: """ # 验证权限 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 获取参数 post_type = request.POST.get('type', 0) post_id = request.POST.get('id', 0) if post_type not in [0, 1]: return HttpResponse(json.dumps(self._error_msg['post_type_error'])) if post_id <= 0: return HttpResponse(json.dumps(self._error_msg['post_id_error'])) # 处理请求 if not self._on_digg(post_type, post_id): return HttpResponse(json.dumps(self._error_msg['post_id_error'])) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def uncollect_market(self, request): """ 从收藏的市场中删除目标市场 :param request 请求 :return: """ # 验证权限 user = Auth.get_user(request) if not user: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 获取参数 market_id = request.POST.get('id', 0) if market_id == 0: return HttpResponse(json.dumps(self._error_msg['market_id_error'])) # 查询是否已收藏过目标市场 if market_id not in user.collected_markets: return HttpResponse( json.dumps(self._error_msg['market_none_error'])) # 处理请求 if not user.update(pull__collected_markets=market_id, dec__collected_market_num=1): return HttpResponse(json.dumps(self._error_msg['general_error'])) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def read_notify(self, request): """ 通知被读取时的处理程序 :param request: 请求 :return: """ # 验证权限 user = Auth.get_user(request) if not user: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def get_notify(self, request): """ 获取通知 :param request: 请求 :return: """ # 验证权限 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def get_user_data(self, request): """ 获取当前用户数据 :param request: 请求 :return: 用户数据 """ # 权限验证 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(self._error_msg['permission_error'])) # 获取数据 data = {} user = users[0] data['id'] = user.id data['username'] = user.username data['mobile'] = user.mobile data['topic_num'] = user.topic_num data['following_num'] = user.following_num data['follower_num'] = user.follower_num data['collected_market_num'] = user.collected_market_num result = copy.copy(self._success_msg) result['data'] = data # 返回正确响应 return HttpResponse(json.dumps(result))
def login_verify(self, username, password): """ 登录验证 :param username: 用户名 :param password: 密码 :return: 成功返回用户对象,否则None """ return Auth.authenticate(username, password)
def publish_topic(self, request): """ 发表主题 :param request: 请求 :return: """ # 验证权限 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 获取参数 title = request.POST.get('title', '') content = request.POST.get('content', '') # 验证标题 if len(title) > 50: return HttpResponse(json.dumps(self._error_msg['title_len_error'])) for w in self._forbidden_words: if title.find(w) >= 0: return HttpResponse( json.dumps(self._error_msg['title_words_error'])) # 验证内容 if len(content) > 10000: return HttpResponse( json.dumps(self._error_msg['content_len_error'])) for w in self._forbidden_words: if content.find(w) >= 0: return HttpResponse( json.dumps(self._error_msg['content_words_error'])) # 处理请求 if not self._on_publish_topic(title, content, users): return HttpResponse(json.dumps(self._error_msg['general_error'])) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def publish_comment(self, request): """ 发表评论 :param request: 请求 """ # 验证权限 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(Auth.get_no_permission_msg())) # 获取参数 content = request.POST.get('content', '') post_type = request.POST.get('type', 0) post_id = request.POST.get('id', 0) if post_type not in [0, 1]: return HttpResponse(json.dumps(self._error_msg['post_type_error'])) if post_id == 0: return HttpResponse(json.dumps(self._error_msg['post_id_error'])) # 验证内容 if len(content) > 10000: return HttpResponse( json.dumps(self._error_msg['content_len_error'])) for w in self._forbidden_words: if content.find(w) >= 0: return HttpResponse( json.dumps(self._error_msg['content_words_error'])) # 处理请求 if not self._on_publish_comment(post_id, post_type, content, users): return HttpResponse(json.dumps(self._error_msg['general_error'])) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
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))
def change_password(self, request): """ 修改密码 :param request: 请求 :return: """ # 权限验证 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(self._error_msg['permission_error'])) # 获取参数 new_password = request.POST.get('new_password', '') if re.match(r'[\w\d]{8,20}', new_password) is None: return HttpResponse(json.dumps(self._error_msg['pass_error'])) # 处理请求 if not self._on_change_password(users, make_password(new_password)): return HttpResponse(json.dumps(self._error_msg['general_error'])) # 返回正确响应 return HttpResponse(json.dumps(self._success_msg))
def upload_portrait(self, request): """ 上传头像 :param request: 请求 :return: """ # 权限验证 users = Auth.get_authenticated_user(request) if not users: return HttpResponse(json.dumps(self._error_msg['permission_error'])) # 获取参数 portrait_image = request.POST.get('portrait', b'') if not portrait_image: return HttpResponse(json.dumps(self._error_msg['portrait_error'])) # 上传到七牛云 data = {'file': portrait_image} upload_result = http_fetch(THIRD_PARTY_API['QINIU'], data) if not upload_result: # 上传失败 return HttpResponse(json.dumps(self._error_msg['portrait_upload_error'])) else: # 上传成功,获取url portrait_url = upload_result.url # 更新用户数据 if not self._on_upload(users, portrait_url): return HttpResponse(json.dumps(self._error_msg['general_error'])) # 返回正确响应 result = copy.copy(self._success_msg) result['data'] = portrait_url return HttpResponse(json.dumps(result))