def queryArticleDetailed(request): try: json_req = json.loads(request.body) user_id = get_userid(json_req['token']) # 当前用户 article_id = json_req['article_id'] up_page_view(article_id) # 再redis中将阅读数加一 article = Article.objects.get(article_id=article_id) collection_count = Collection.objects.filter(article_id=article_id).count() # 该文章已被多少人收藏 write_count = getUserWriteCount(article.user_id.user_id) user_point_count = queryPointByUserId(article.user_id.user_id) json_dict = {"article_id": article.article_id, "title": article.title, "content": article.content, "page_view": query_page_view(article.article_id), "create_time": article.create_time.strftime("%Y-%m-%d %H:%M:%S"), "point_count": query_point_count(article.article_id), "type_id": article.type_id, "type_name": getTypeName(article.type_id), "point_flag": queryPoint(user_id, article.article_id), "collect_flag": queryCollect(user_id, article.article_id), # 登录用户是否收藏该文章 "user_point_count": user_point_count, # 获取该文章的作者共获得多少赞 "user": queryUserInfo(article.user_id.user_id), # 获取作者基本信息 "comment_count": getArticleCommentCount(article.article_id), # 该文章有多少条评论 "write_count": write_count, "collection_count": collection_count, "level_name": getLevelName(write_count, user_point_count) # 作者获得的称号 } result = {'code': 200, 'article': json_dict} return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json") except: return HttpResponse(json.dumps({'code': 405, 'information': '执行异常!'}, ensure_ascii=False), content_type="application/json")
def queryArticleByTypeId(request): try: json_req = json.loads(request.body) type_id = json_req['type_id'] user_id = get_userid(json_req['token']) order = check_order(json_req['order_type']) # 排序类型默认按时间,0 按时间,1 按阅读数,2 按点赞数 page = json_req['page'] # 分页查询文章信息 page_info = PageInfo(page, 10) article_count = Article.objects.filter(type_id=type_id, flag=1).count() articles = Article.objects.filter(type_id=type_id, flag=1).order_by(order)[page_info.start():page_info.end()] json_list = [] for article in articles: json_dict = {"article_id": article.article_id, "title": article.title, "simple_content": article.simple_content, "page_view": query_page_view(article.article_id), "create_time": article.create_time.strftime('%Y-%m-%d %H:%M:%S'), "point_count": query_point_count(article.article_id), "user": queryUserInfo(article.user_id.user_id), "point_flag": queryPoint(user_id, article.article_id), "photo": queryPhotoInfo(article.article_id), "comment_count": getArticleCommentCount(article.article_id), "level_name": getLevelName(getUserWriteCount(article.user_id.user_id), queryPointByUserId(article.user_id.user_id)) # 作者获得的称号 } json_list.append(json_dict) result = {'code': 200, 'article_count': article_count, 'article': json_list} return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json") except: return HttpResponse(json.dumps({'code': 405, 'information': '执行异常!'}, ensure_ascii=False), content_type="application/json")
def queryArticleByMyself(request): try: json_req = json.loads(request.body) user_id = json_req['user_id'] login_user_id = get_userid(json_req['token']) # 当前登录用户的user_id page = json_req['page'] # 分页查询文章信息 page_info = PageInfo(page, 10) article_count = Article.objects.filter(user_id=user_id, flag=1).count() articles = Article.objects.filter( user_id=user_id, flag=1).order_by("-create_time")[page_info.start():page_info.end()] json_list = [] for article in articles: json_dict = { "article_id": article.article_id, "title": article.title, "simple_content": article.simple_content, "page_view": query_page_view(article.article_id), "create_time": article.create_time.strftime("%Y-%m-%d %H:%M:%S"), "point_count": article.point_count, "type_id": article.type_id, "type_name": getTypeName(article.type_id), "point_flag": queryPoint(login_user_id, article.article_id), "collect_flag": queryCollect(login_user_id, article.article_id), "user": queryUserInfo(article.user_id.user_id), "photo": queryPhotoInfo(article.article_id), "comment_count": getArticleCommentCount(article.article_id), } json_list.append(json_dict) result = { 'code': 200, 'article_count': article_count, 'article': json_list } return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json") except: return HttpResponse(json.dumps({ 'code': 405, 'information': '执行异常!' }), content_type="application/json")
def create_response(self): context = super().get_context() #搜索引擎完成后的内容 keyword = self.request.GET.get('q', None) # 关键词为q try: if not keyword: return HttpResponse(json.dumps({ 'code': 200, 'message': '没有相关信息' }), content_type="application/json") else: conn = get_redis_connection('default') article_count = SearchQuerySet().using("default").filter( text=keyword).count() # 获取文章数量 if article_count != 0: conn.expire('keyword', 720 * 60) conn.zincrby('keyword', 1, keyword) # 搜索词存进redis #我搜索的历史记录的保存 user_id = self.request.GET.get('q_uid', None) ##如果user_id不为空 if user_id: history_key = "history_%d" % int(user_id) # 移除相同的记录 conn.lrem(history_key, 0, keyword) # 从左侧添加历史浏览记录 conn.lpush(history_key, keyword) # 只保留最新的五个历史浏览记录 conn.ltrim(history_key, 0, 4) #获取搜索文章信息 content_list = [] for i in context['page'].object_list: #对象列表 set_dict = { "article_id": i.object.article_id, "title": i.object.title, "simple_content": i.object.simple_content, "page_view": query_page_view(i.object.article_id), "create_time": i.object.create_time.strftime('%Y-%m-%d %H:%M:%S'), "type_id": i.object.type_id, "type_name": getTypeName(i.object.type_id), "point_count": i.object.point_count, "point_flag": queryPoint(user_id, i.object.article_id), "user": queryUserInfo(i.object.user_id.user_id), "photo": queryPhotoInfo(i.object.article_id), "comment_count": getArticleCommentCount(i.object.article_id), } # 要返回的字段 content_list.append(set_dict) result = { 'code': 200, 'article_count': article_count, 'article': content_list } return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json") except: return HttpResponse(json.dumps({ 'code': 405, 'information': '执行异常' }), content_type="application/json")