From f8205cf3aec384f1327177dbc000e41267fb6b74 Mon Sep 17 00:00:00 2001 From: wrmswindmill Date: Sat, 29 Sep 2018 22:35:13 +0800 Subject: [PATCH] add leadboard and add project url fix delete function --- apps/operations/views.py | 59 +++++++++++++++++++--- requirement.txt | 1 + static/js/source.js | 36 +++++++------ templates/projects/filesub/file-right.html | 10 ++++ 4 files changed, 84 insertions(+), 22 deletions(-) diff --git a/apps/operations/views.py b/apps/operations/views.py index 13c525c..59c5bda 100644 --- a/apps/operations/views.py +++ b/apps/operations/views.py @@ -19,7 +19,7 @@ from projects.models import File,Method from projects.models import Language, Project, FileSummary from django.template.loader import render_to_string import base64 - +import redis import logging @@ -29,13 +29,42 @@ logging.basicConfig(level=logging.DEBUG, filename='/test.log', filemode='w') +def set_init_rank(redis_db): + # 1.获取当前的比赛项目id + # competition_projects=Project.objects.filter(is_competition_project=1) + competition_files = File.objects.filter(is_competition_file=1) + # 2.获取所有的组id + symbol = "gcc-dev-2018" + private_token="hriEn3UwXfJs3PmyXnSG" + url="https://www.educoder.net/api/v1/sources/"+str(symbol)+"/get_team_ids?private_token="+private_token + response = requests.get(url) + team_ids = list(json.loads(response.text)['team_ids']) + for team in team_ids: + # 3.获取当前组对应的组成员 + url = "https://www.educoder.net/api/v1/sources/"+str(symbol)+"/get_team_user_ids?team_id="+str(team)+"&private_token="+private_token + response = requests.get(url) + user_ids = json.loads(response.text)['user_ids'] + users = User.objects.filter(educoder_userid__in=user_ids) + # add the user into user_team_map + for user in users: + user_team_map[user.id]=team + if len(users)>0: + # 4.获取每个组的分数 + score = len(Annotation.objects.filter(user__in=users,file__in=competition_files)) + redis_db.zadd("gcc",score,team) + print("init rank success") + +redis_db = redis.StrictRedis(host='localhost', port=6379, db=0) +user_team_map ={} +set_init_rank(redis_db) def get_educoder_group_userlist_by_educoder_userid(userid,file_id): # 根据file_id查询project再查询到对应的比赛 project = File.objects.get(id=file_id).project try: - symbol = Competition.objects.get(project=project).symbol + # symbol = Competition.objects.get(project=project).symbol + symbol = "gcc-dev-2018" private_token="hriEn3UwXfJs3PmyXnSG" url="https://www.educoder.net/api/v1/sources/"+str(symbol)+"/get_competition_members?user_id="+str(userid)+"&private_token="+private_token response = requests.get(url) @@ -389,7 +418,14 @@ class AddAnnotationView(View): # print(anno_content) if file.project.is_competition_project and not file.is_competition_file: return HttpResponse('{"status":"success_but_not_correct_file","msg":"注释成功,但请注意您标注的不是比赛模块","anno_content":"'+anno_content+'"}', content_type='application/json') - return HttpResponse('{"status":"success","msg":"注释成功","anno_content":"'+anno_content+'"}', content_type='application/json') + # update LeaderBoard + if request.user.educoder_userid is not None: + team_id = user_team_map[request.user.id] + redis_db.zincrby("gcc",team_id,1) + rank = int(redis_db.zrevrank("gcc",team_id))+1 + return HttpResponse(json.dumps({"status": "success", "msg":"注释成功","anno_content":anno_content,"rank":rank}), content_type='application/json') + else: + return HttpResponse(json.dumps({"status": "success", "msg":"注释成功","anno_content":anno_content,}), content_type='application/json') except Exception as e: return HttpResponse('{"status":"fail","msg":"参数传递错误,注释失败"}', content_type='application/json') else: @@ -455,11 +491,19 @@ def get_currentfile_language(file): class DeleteView(View): def post(self,request): try: + remove_anno_icon = False object_id = request.POST.get("object_id","") object_type = request.POST.get("object_type","") if object_type=="Annotation": annotation = Annotation.objects.get(id=object_id,is_latest=1) - Annotation.objects.filter(linenum=annotation.linenum,file=annotation.file).delete() + linenum = annotation.linenum + file = annotation.file + Annotation.objects.filter(user=request.user,linenum=linenum,file=file).delete() + if request.user.educoder_userid is not None: + team_id = user_team_map[request.user.id] + redis_db.zincrby("gcc",team_id,-1) + if len(Annotation.objects.filter(linenum=linenum,file=file))==0: + remove_anno_icon = True elif object_type == "Question": Question.objects.filter(id=object_id).delete() elif object_type == "QuestionAnswer": @@ -470,8 +514,9 @@ class DeleteView(View): IssueComment.objects.filter(id=object_id).delete() elif object_type == 'QuestionAnswerComment': QuestionAnswerComment.objects.filter(id=object_id).delete() - return HttpResponse('{"status":"success","msg":"删除成功"}', content_type='application/json') - except: + return HttpResponse(json.dumps({"status": "success", "msg":"删除成功","remove_anno_icon":remove_anno_icon}), content_type='application/json') + except Exception as e: + # print(str(e)) return HttpResponse('{"status":"fail","msg":"删除失败"}', content_type='application/json') @@ -1384,6 +1429,7 @@ class RightView(View): filelinenum = current_file_summary.line_num create_time = file.create_time file_num = current_file_summary.file_num + project_url = file.project.github html_str = render_to_string('projects/filesub/file-right.html', locals()) return html_str @@ -1420,6 +1466,7 @@ class RightView(View): path = file.path # 观看数应该放在File_id中 + project_url = file.project.github html_str = render_to_string('projects/filesub/file-right.html', locals()) return html_str diff --git a/requirement.txt b/requirement.txt index fe2d1b3..ddac3eb 100644 --- a/requirement.txt +++ b/requirement.txt @@ -1,3 +1,4 @@ +redis alabaster==0.7.10 amqp==1.4.9 anyjson==0.3.3 diff --git a/static/js/source.js b/static/js/source.js index 58fd3b8..c2b5416 100644 --- a/static/js/source.js +++ b/static/js/source.js @@ -248,7 +248,7 @@ function modify_anno(file_id,line_num,anno_id){ if (data.status === "success") { alert(data.msg) $("#ItemcommentPanel").remove() - $("."+file_id+line_num).text(data.anno_content) + $("."+file_id+"_"+line_num).text(data.anno_content) show_annotation(file_id,line_num); deletelocalStorage("modifyAnno_id"); @@ -485,8 +485,10 @@ function delete_anno(file_id,line_num,anno_id){ if (data.status == 'success') { $("#ItemcommentPanel").remove() $("."+file_id+line_num).remove() - $("#codeopration_anno_"+file_id+"_"+line_num).remove(); - show_annotation(file_id,line_num); + if(data.remove_anno_icon){ + $("#codeopration_anno_"+file_id+"_"+line_num).remove(); + } + // show_annotation(file_id,line_num); layer.msg(data.msg); }else{ layer.msg(data.msg); @@ -537,18 +539,6 @@ function add_annotation(item,file_id, line_num) { function submit_annotation(file_id, line_num, content) { // 因为可能注释或问题没有值的时候,不会为该代码块添加html代码,所以首先判断 // 然后将注释数+1 - var codeopration_anno_div = $("#codeopration_anno_"+file_id+"_" + line_num).html() - var str = ''; - str +=''; - $("#codeopration_anno_" + +file_id + "_" + line_num).html(str); - $(".addno-panel").hide(); - $(".source-addno-panel").remove(); - // show_annotation(file_id,line_num); - //将注释添加到当前行的上一行 - var contenthtml ='
'+'
'+'
'+"//"+ content+'
'+'
'+'
'; - var html = '
'+contenthtml+'
'; - var id=file_id+'_'+"L"+line_num; - $("#"+id).before(html); $.ajax({ cache: false, type: "POST", @@ -561,7 +551,21 @@ function submit_annotation(file_id, line_num, content) { }, success: function (data) { if (data.status == 'success') { - layer.msg('提交成功'); + var codeopration_anno_div = $("#codeopration_anno_"+file_id+"_" + line_num).html() + var str = ''; + str +=''; + $("#codeopration_anno_" + +file_id + "_" + line_num).html(str); + $(".addno-panel").hide(); + $(".source-addno-panel").remove(); + // show_annotation(file_id,line_num); + //将注释添加到当前行的上一行 + var contenthtml ='
'+'
'+'
'+"//"+ content+'
'+'
'+'
'; + var html = '
'+contenthtml+'
'; + var id=file_id+'_'+"L"+line_num; + $("#"+id).before(html); + if(data.rank){ + layer.msg('标注提交成功,当前小组排名为:'+data.rank); + } }else{ layer.msg(data.msg); } diff --git a/templates/projects/filesub/file-right.html b/templates/projects/filesub/file-right.html index 2cd7aa5..76067f6 100644 --- a/templates/projects/filesub/file-right.html +++ b/templates/projects/filesub/file-right.html @@ -2,12 +2,22 @@
{% if isdir %}

Module Summary

+ +

+ Project Url: + {{project_url}} +

Module Path: {{path}}

{% else %}

File Summary

+ +

+ Project Url: + {{project_url}} +

File Path: {{path}}