diff --git a/apps/operations/views.py b/apps/operations/views.py index f0d0497..f2668bf 100644 --- a/apps/operations/views.py +++ b/apps/operations/views.py @@ -20,6 +20,7 @@ from projects.models import Language, Project, FileSummary from django.template.loader import render_to_string import base64 import redis +import json import logging @@ -29,9 +30,16 @@ 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) +def get_educoder_teamdid_by_educoder_userid(educoder_userid): + url = "https://www.educoder.net/api/v1/sources/get_user_competition_id?educoder_userid="+educoder_userid+"&private_token=hriEn3UwXfJs3PmyXnSG" + response = requests.get(url) + team_id = list(json.loads(response.text)['team_ids']) + if len(team_id)>0: + return team_id[0] + else: + return None + +def update_rank_latest(): competition_files = File.objects.filter(is_competition_file=1) # 2.获取所有的组id symbol = "gcc-dev-2018" @@ -52,11 +60,18 @@ def set_init_rank(redis_db): # 4.获取每个组的分数 score = len(Annotation.objects.filter(user__in=users,file__in=competition_files)) redis_db.zadd("gcc",score,team) + with open('user_team_data.json', 'w') as outfile: + json.dump(user_team_map, outfile) + +def set_init_rank(): + # 1.获取当前的比赛项目id + # competition_projects=Project.objects.filter(is_competition_project=1) + user_team_map = json.load(open('user_team_data.json','r')) print("init rank success") redis_db = redis.StrictRedis(host='localhost', port=6379, db=0) user_team_map ={} -set_init_rank(redis_db) +set_init_rank() def get_educoder_group_userlist_by_educoder_userid(userid,file_id): @@ -420,10 +435,21 @@ class AddAnnotationView(View): return HttpResponse('{"status":"success_but_not_correct_file","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') + try: + team_id = user_team_map[request.user.id] + except: + team_id = get_educoder_teamdid_by_educoder_userid(request.user.educoder_userid) + finally: + if team_id is None: + return HttpResponse(json.dumps({"status": "success", "msg":"注释成功","anno_content":anno_content,}), content_type='application/json') + elif team_id in user_team_map: + user_team_map[request.user.id]=team_id + # save to file + with open('user_team_data.json', 'w') as outfile: + json.dump(user_team_map, outfile) + 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: diff --git a/user_team_data.json b/user_team_data.json new file mode 100644 index 0000000..d570a52 --- /dev/null +++ b/user_team_data.json @@ -0,0 +1 @@ +{"234": 280, "143": 281, "146": 281, "72": 285, "189": 285, "283": 288, "184": 288, "534": 288, "133": 290, "165": 290, "196": 290, "197": 290, "264": 290, "71": 292, "169": 292, "88": 293, "68": 295, "104": 295, "150": 295, "57": 298, "107": 298, "61": 337, "66": 302, "161": 305, "162": 305, "87": 319, "173": 310, "514": 310, "90": 317, "89": 323, "235": 327, "121": 328, "137": 373, "99": 331, "24": 337, "77": 341, "100": 357, "233": 357, "338": 363, "297": 366, "183": 373, "198": 374, "156": 379, "435": 382, "269": 390, "154": 400, "166": 400, "152": 402, "163": 404, "187": 413, "115": 414, "167": 414, "172": 414, "174": 414, "364": 418, "204": 422, "208": 430, "221": 436, "94": 439, "95": 439, "96": 439, "127": 440, "129": 440, "131": 440, "447": 442, "230": 444, "268": 450, "246": 451, "223": 452, "430": 462, "275": 464, "267": 468, "255": 469, "226": 470, "265": 477, "245": 480, "253": 480, "254": 480, "285": 480, "311": 486, "298": 488, "210": 490, "175": 496, "200": 496, "314": 499, "333": 503, "344": 510, "428": 510, "81": 516, "33": 562, "433": 518, "408": 535, "415": 539, "407": 545, "420": 546, "437": 557, "439": 557, "444": 560, "83": 562, "84": 562, "374": 569, "429": 570, "222": 572, "357": 573, "342": 576, "359": 576, "438": 576, "300": 579, "352": 581, "353": 581, "441": 583, "467": 584, "360": 588, "482": 593, "485": 594, "487": 595, "251": 600, "306": 600, "274": 602, "279": 602, "280": 602, "522": 610, "483": 611, "186": 616, "506": 619, "517": 631, "355": 633, "403": 633, "397": 634, "527": 637} \ No newline at end of file