Skip to content

Commit

Permalink
Modified rank.py
Browse files Browse the repository at this point in the history
Get rank of course by RankCache table.
Modified get_rank_by_course and get_all_rank api
  • Loading branch information
Kevinjyp committed May 7, 2019
1 parent 73b745b commit 132e16e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
4 changes: 1 addition & 3 deletions rateMyCourse/views/comments.py
Expand Up @@ -69,9 +69,7 @@ def get_comment_by_course(request):
'errMsg': 'cookies 错误',
}), content_type="application/json")'''
course_ID = request.GET['course_ID']
rawList = MakeComment.objects.filter(
course_id=Course.objects.get(
course_ID=course_ID).id)
rawList = MakeComment.objects.filter(course_id=Course.objects.get(course_ID=course_ID).id)

retList = []
for i in rawList:
Expand Down
112 changes: 64 additions & 48 deletions rateMyCourse/views/rank.py
Expand Up @@ -5,13 +5,15 @@
from rateMyCourse.models import *
from django.core.exceptions import ObjectDoesNotExist
import rateMyCourse.views.authentication as auth
from django.views.decorators.csrf import csrf_exempt,csrf_protect
from django.views.decorators.csrf import csrf_exempt, csrf_protect


def make_rank(request):
"""
发表评分,需要用户名,课程ID,以及分数
"""
try:
if not auth.auth_with_user(request,request.POST['username']):
if not auth.auth_with_user(request, request.POST['username']):
return HttpResponse(json.dumps({
'status': -100,
'errMsg': 'cookies 错误',
Expand All @@ -22,28 +24,30 @@ def make_rank(request):
funny_score = request.POST['funny_score']
gain_score = request.POST['gain_score']
recommend_score = request.POST['recommend_score']
if difficulty_score>5 or difficulty_score<0 or funny_score>5 or funny_score<0 or gain_score>5 or gain_score<0 or recommend_score>5 or recommend_score<0:
if difficulty_score > 5 or difficulty_score < 0 or funny_score > 5 or funny_score < 0 or gain_score > 5 or gain_score < 0 or recommend_score > 5 or recommend_score < 0:
return HttpResponse(json.dumps({
'status': -1,
'errMsg': '非法分数',
}), content_type="application/json")

except:
except BaseException:
return HttpResponse(json.dumps({
'status': -1,
'errMsg': '缺失信息',
}), content_type="application/json")
else:
try:
b = MakeRank.objects.get(user=User.objects.get(username=username),
course=Course.objects.get(course_ID=course_ID))
b = MakeRank.objects.get(
user=User.objects.get(
username=username), course=Course.objects.get(
course_ID=course_ID))
except ObjectDoesNotExist:
# create new
r = Rank(difficulty_score=difficulty_score,
funny_score=funny_score,
gain_score=gain_score,
recommend_score=recommend_score,
)
funny_score=funny_score,
gain_score=gain_score,
recommend_score=recommend_score,
)
r.save()
b1 = MakeRank(user=User.objects.get(username=username),
course=Course.objects.get(course_ID=course_ID),
Expand All @@ -56,19 +60,19 @@ def make_rank(request):
'message': "发表评分成功"
}
}), content_type="application/json")
except:
except BaseException:
return HttpResponse(json.dumps({
'status': -1,
'errMsg': '缺失信息',
}), content_type="application/json")
else:
# edit
r=b.rank
r = b.rank
r.difficulty_score = difficulty_score
r.funny_score = funny_score
r.gain_score = gain_score
r.recommend_score = recommend_score
#r.edit_time=datetime.datetime.now()
# r.edit_time=datetime.datetime.now()
r.save()
return HttpResponse(json.dumps({
'status': 1,
Expand All @@ -79,7 +83,7 @@ def make_rank(request):
}), content_type="application/json")


@cache_page(60*60)
@cache_page(60 * 60)
def get_rank_by_course(request):
"""
获取某节课的评分,需求课程号
Expand All @@ -92,69 +96,81 @@ def get_rank_by_course(request):
'errMsg': 'cookies 错误',
}), content_type="application/json")'''
course_ID = request.GET['course_ID']
rawList = MakeRank.objects.filter(course_id=Course.objects.get(course_ID=course_ID).id)
# rawList = MakeRank.objects.filter(course_id=Course.objects.get(course_ID=course_ID).id)
course = RankCache.objects.get(course_id=Course.objects.get(course_ID=course_ID).id)

num_rank = 0
rank_dict = {}
rank_dict['difficulty_score'] =0
rank_dict['funny_score'] =0
rank_dict['gain_score'] =0
rank_dict['recommend_score'] =0
for c in rawList:
num_rank = num_rank + 1
rank_dict['difficulty_score'] += c.rank.difficulty_score
rank_dict['funny_score'] += c.rank.funny_score
rank_dict['gain_score'] += c.rank.gain_score
rank_dict['recommend_score'] += c.rank.recommend_score
rank_dict['difficulty_score'] = 0
rank_dict['funny_score'] = 0
rank_dict['gain_score'] = 0
rank_dict['recommend_score'] = 0
# for c in rawList:
# num_rank = num_rank + 1
# rank_dict['difficulty_score'] += c.rank.difficulty_score
# rank_dict['funny_score'] += c.rank.funny_score
# rank_dict['gain_score'] += c.rank.gain_score
# rank_dict['recommend_score'] += c.rank.recommend_score

# rank_dict['difficulty_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['funny_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['gain_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['recommend_score'] /= (1 if num_rank == 0 else num_rank)

rank_dict['difficulty_score'] /= (1 if num_rank==0 else num_rank)
rank_dict['funny_score'] /= (1 if num_rank==0 else num_rank)
rank_dict['gain_score'] /= (1 if num_rank==0 else num_rank)
rank_dict['recommend_score'] /= (1 if num_rank==0 else num_rank)
except:
rank_dict['difficulty_score'] /= course.difficulty_score / course.people
rank_dict['funny_score'] /= course.funny_score / course.people
rank_dict['gain_score'] /= course.gain_score / course.people
rank_dict['recommend_score'] /= course.recommend_score / course.people
except BaseException:
return HttpResponse(json.dumps({
'status': -1,
'errMsg': '获取评分失败',
}), content_type="application/json")
else:
return HttpResponse(json.dumps({
'status': 1,
'length': num_rank,
'length': course.people,
'body': rank_dict
}), content_type="application/json")
finally:
pass


@cache_page(60*60)
@cache_page(60 * 60)
def get_all_rank(request):
all_course_ID=Course.objects.all()
retDist={}
all_course_ID = Course.objects.all()
retDist = {}
for course_ID in all_course_ID:
rawList = MakeRank.objects.filter(course_id=course_ID)
# rawList = MakeRank.objects.filter(course_id=course_ID)
course = RankCache.objects.get(course_id=Course.objects.get(course_ID=course_ID).id)

num_rank = 0
rank_dict = {}
rank_dict['difficulty_score'] = 0
rank_dict['funny_score'] = 0
rank_dict['gain_score'] = 0
rank_dict['recommend_score'] = 0
for c in rawList:
num_rank = num_rank + 1
rank_dict['difficulty_score'] += c.rank.difficulty_score
rank_dict['funny_score'] += c.rank.funny_score
rank_dict['gain_score'] += c.rank.gain_score
rank_dict['recommend_score'] += c.rank.recommend_score
# for c in rawList:
# num_rank = num_rank + 1
# rank_dict['difficulty_score'] += c.rank.difficulty_score
# rank_dict['funny_score'] += c.rank.funny_score
# rank_dict['gain_score'] += c.rank.gain_score
# rank_dict['recommend_score'] += c.rank.recommend_score

# rank_dict['difficulty_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['funny_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['gain_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['recommend_score'] /= (1 if num_rank == 0 else num_rank)
# rank_dict['rank_number'] = num_rank
rank_dict['difficulty_score'] /= course.difficulty_score / course.people
rank_dict['funny_score'] /= course.funny_score / course.people
rank_dict['gain_score'] /= course.gain_score / course.people
rank_dict['recommend_score'] /= course.recommend_score / course.people
rank_dict['rank_number'] = course.people
retDist[course_ID.course_ID] = rank_dict

rank_dict['difficulty_score'] /= (1 if num_rank == 0 else num_rank)
rank_dict['funny_score'] /= (1 if num_rank == 0 else num_rank)
rank_dict['gain_score'] /= (1 if num_rank == 0 else num_rank)
rank_dict['recommend_score'] /= (1 if num_rank == 0 else num_rank)
rank_dict['rank_number'] = num_rank
retDist[course_ID.course_ID]=rank_dict
return HttpResponse(json.dumps({
'status': 1,
'length': len(retDist),
'body': retDist
}), content_type="application/json")

0 comments on commit 132e16e

Please sign in to comment.