add new files

This commit is contained in:
robin 2021-10-03 20:47:35 +08:00
parent 1b46b0dc9a
commit 3bfdd85bf8
9 changed files with 153 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

124
home/api.py Normal file
View File

@ -0,0 +1,124 @@
# -*- coding:utf-8 -*-
'''
@AuthorRobin.lau
@Email: 329080237@qq.com
@Wechat: 15618110227
@File: api.py
@Date: 2021/2/27 13:45
@Description:本模块Api接口主文件如接口比较复杂可以拆分多个文件
'''
from web.search.api import project
from django.db.models import Q
from django.views.generic import View
from utils.public import *
from web.models import *
import json
import random
class myQuestion(View):
@try_except
def get(self, request):
user_id = request.session.get("user_id")
items = Question.objects.filter(userKey_id=user_id).values("id","name").order_by("-id")[:5]
for i in items:
i['reply'] = ReplyAnswer.objects.filter(answerKey__questionKey_id=i['id']).count()
i['answer'] = Answer.objects.filter(questionKey=i['id']).count()
resData = {
"items": list(items)
}
return jsonData(data=resData)
class todoQuestion(View):
@try_except
def get(self, request):
data = json.loads(request.GET.get("data"))
pageNo = data.get("pageNo")
pageSize = data.get("pageSize")
user_id = request.session.get("user_id")
items = Question.objects.order_by("-infoDateTime").values("id","content","name")
items = items.filter(state=0) #待解决问题
items = items.exclude(userKey_id=user_id) #排除我的提问
items = items.exclude(answer__userKey_id=user_id) #排除我回答过的问题
#测试时宽松条件
#items = Question.objects.exclude(userKey_id=user_id).order_by("?").values("id", "content", "name")[:1]
return_items = getPageRecord(items, pageNo, pageSize)
resData = {
"items": return_items,
"total": items.count()
}
return jsonData(data=resData)
class getMyinfo(View):
@try_except
def get(self, request):
user_id = request.session.get("user_id")
print(user_id)
args = ("id", "nickName", "avatar")
item = User.objects.filter(id=user_id).values(*args)[0]
item['myFans'] = FollowMember.objects.filter(userKey=user_id).count()
item['myFocus'] = FollowMember.objects.filter(followerKey=user_id).count()
resData = {
"items": item
}
console(resData)
return jsonData(data=resData)
class hotQuestion(View):
@staticmethod
@try_except
def sortItems(data, sortKey, reverse=True):
#倒序处理 sorted() 默认从小到大reverse=TRUE 调整为 从大到小
newData = sorted(data, key=lambda x: x[sortKey], reverse=reverse)
return newData
@try_except
def get(self, request):
data = json.loads(request.GET.get("data"))
user_id = request.session.get("user_id")
pageNo = data.get("pageNo")
pageSize = data.get('pageSize')
ip = visitor_ip_address(request)
items = Question.objects.filter().order_by("-id").values()
for i in items:
i['good'] = QuestionTriple.objects.filter(type=1, questionKey_id=i['id']).count()
i['totalAnswer'] = Answer.objects.filter(questionKey_id=i['id']).count()
i['myClick'] = QuestionTriple.objects.filter(type=1, questionKey_id=i['id']).filter(
Q(userKey_id=user_id) | Q(ip=ip)).count()
i["randomKey"] = random.random()
return_items = hotQuestion.sortItems(items, "randomKey")
resData = {
"items": return_items[:5],
"total": items.count()
}
res = jsonData(data=resData)
return res
class hotProject(View):
@staticmethod
@try_except
def sortItems(data, sortKey, reverse=True):
#倒序处理 sorted() 默认从小到大reverse=TRUE 调整为 从大到小
newData = sorted(data, key=lambda x: x[sortKey], reverse=reverse)
return newData
@try_except
def get(self, request):
user_id = request.session.get("user_id")
ip = visitor_ip_address(request)
items = Project.objects.filter().order_by("-id").values()
args = ("id", "nickName", "avatar")
for i in items:
i['randomKey'] = random.random()
i['user'] = User.objects.filter(id=i['userKey_id']).values(*args)[0]
i['like'] = ProjectTriple.objects.filter(projectKey_id= i['id'], type=1).count()
items = hotProject.sortItems(items, "randomKey", reverse=False)[:3]
resData = {
"items": list(items),
"total": 3
}
res = jsonData(data=resData)
return res

29
home/urls.py Normal file
View File

@ -0,0 +1,29 @@
# -*- coding:utf-8 -*-
'''
@AuthorRobin.lau
@Email: 329080237@qq.com
@Wechat: 15618110227
@File: urls.py
@Date: 2021/2/27 13:45
@Description: Api地址配置文件
'''
from django.urls import path
from web.models import *
from .api import *
urlpatterns = [
#首页--我的提问
path('myQuestion/', myQuestion.as_view()),
#首页--每日一答
path('todoQuestion/', todoQuestion.as_view()),
#首页--获取我的信息
path('getMyinfo/', getMyinfo.as_view()),
#首页-- 热门问答
path('hotQuestion/', hotQuestion.as_view()),
#首页-- 热门项目
path('hotProject/', hotProject.as_view())
]