Repository navigation
Expand file tree
/
Copy pathviews.py
More file actions
831 lines (754 loc) · 32.5 KB
/
Copy pathviews.py
File metadata and controls
831 lines (754 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .models import Question, Answer, UserData, Team, TimeTable
from django.db.models import Q, Sum
from django.core.paginator import Paginator
from django.core import serializers
import django.utils.timezone as timezone
import time
import json
import base64
import random
import requests
# from bs4 import BeautifulSoup
session = requests.Session()
url_login = 'https://www.shuhelper.cn/api/users/login/'
headers = {'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'Keep-Alive',
'Content-Type': 'application/json;charset=UTF-8',
'Host': 'www.shuhelper.cn',
'Origin': 'https://www.shuhelper.cn',
'Pragma': 'no-cache',
'Referer': 'https://www.shuhelper.cn/login',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/69.0.3497.100 Safari/537.36'}
# 报名系统api
def index(request, string=''):
print(string)
# if string == 'Beta':
userid = request.session.get('userid')
msg = {}
if userid:
user = UserData.objects.filter(id=userid)
if user.exists():
user = user.first()
# todo 通知的处理逻辑太过于智障
# 团体赛通知
# if user.isPromoted:
# msg = {'msg': '恭喜你成功晋级!/恭喜你成功晋级,请到1106休息等待最终的决赛!'}
# else:
# msg = {'msg': '还有复活赛!/很遗憾你没有成功晋级,但是还有复活赛,还有机会进入决赛!'}
# 复活赛通知
# if user.isPromoted:
# msg = {'msg': '恭喜你成功晋级!/恭喜你成功晋级,请到指定机房参加最终的决赛!'}
# else:
# msg = {'msg': '很遗憾!/你与决赛失之交臂……'}
# 个人赛通知
# if user.isPromoted:
# msg = {'msg': '等待结果!/请等待工作人员对比赛情况进行最终审核!'}
# else:
# msg = {'msg': '等待结果!/请等待工作人员对比赛情况进行最终审核……'}
return render(request, 'JustSearch/index.html', msg)
# return render(request, 'JustSearch/index.html')
# return HttpResponse('不在比赛时间')
@csrf_exempt
def login_view(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST' and request.is_ajax():
if not request.body.decode():
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
data = json.loads(request.body.decode())
if data.get('isAgree'):
# 必须同意比赛准则
username = data.get('username')
psword = data.get('password')
if username and psword:
# 登陆
# 检查数据库有没有该用户
user = UserData.objects.filter(
stdid=username,
)
if user.exists():
# 找到该用户 验证密码
user = user.first()
print('查到', user)
res = login_std(username, psword)
if not res:
# 密码错误
res_data['errmsg'] = '用户名或密码错误'
else:
# 密码验证成功
# 绑定到session
request.session['userid'] = user.id
res_data['isOk'] = True
res_data['userInfo'] = getUserInfo(user.id)
else:
# 没有找到该用户 验证用户名和密码创建用户
res = login_std(username, psword)
if res:
res_data['errmsg'] = 'ok'
res_data['isOk'] = True
res_data['name'] = res.get('name')
print('登陆成功', res)
# 写入数据库
user_now = UserData.objects.create(
stdid=username,
name=res.get('name')
)
request.session['userid'] = user_now.id
else:
print('登陆失败')
res_data['errmsg'] = '用户名或密码错误'
else:
res_data['errmsg'] = '用户名或密码为空'
else:
res_data['errmsg'] = '必须同意比赛准则'
return JsonResponse(res_data)
@csrf_exempt
def new_team(request):
# todo 这边截止报名是一次性的……待更正……
res_data = {'isOk': False, 'errmsg': '报名已经截止!'}
if request.method == 'POST':
# user = isAuthed(request)
# if not user:
# res_data['errmsg'] = '获取授权信息失败'
# JsonResponse(res_data)
# data = json.loads(request.body.decode())
# teamname = data.get('teamname')
# if Team.objects.filter(name=teamname).exists():
# res_data['errmsg'] = '队伍名称已存在'
# else:
# print(user, '创建队伍', data)
# if user.team:
# res_data['errmsg'] = '你已经有队伍了哟'
# else:
# user.team = Team.objects.create(
# name=data.get('teamname'),
# leader=user.name
# )
# user.save()
# request.session['teamid'] = user.team.id
#
# res_data['isOk'] = True
# res_data['userInfo'] = getUserInfo(user.id)
return JsonResponse(res_data)
@csrf_exempt
def get_team(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
if UserData.objects.filter(id=userid).exists():
res_data['isOk'] = True
res_data['userInfo'] = getUserInfo(userid)
print(res_data['userInfo'])
# 已经获得登陆授权~
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
@csrf_exempt
def register(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST' and request.is_ajax():
userid = request.session.get('userid')
user = UserData.objects.filter(id=userid)
if not user.exists():
print('用户不存在')
res_data['errmsg'] = 'User Not Found'
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
user = user.first()
if not request.body.decode():
print('没有收到信息')
res_data['errmsg'] = 'Msg Not Found'
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
data = json.loads(request.body.decode())
qq = data.get('qq')
tel = data.get('tel')
if qq and tel and user:
user.qq = qq
user.tel = tel
user.save()
res_data['isOk'] = True
res_data['userInfo'] = getUserInfo(userid)
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
@csrf_exempt
def get_rank(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
data = json.loads(request.body.decode())
if data.get('who') == 'team':
res_data['isOk'] = True
res_data['teams'] = list(
Team.objects.all().order_by('-score', 'finish_time','-id')[:25].values_list('id', 'name', 'score'))
res_data['users'] = list(
UserData.objects.all().order_by('-score', 'finish_time','-id')[:15].values_list('id', 'name', 'score'))
# 已经获得登陆授权~
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
@csrf_exempt
def find_team(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
data = json.loads(request.body.decode())
team_find = data.get('teamfind')
print(team_find)
# if(team_find)
if team_find.isdigit():
team1 = Team.objects.filter(id=team_find)
if team1.exists():
team1 = team1.first()
# if len(team1.mems.all()) < 3:
res_data['team'] = {'name': team1.name, 'id': team1.id, 'leader': team1.leader}
res_data['isOk'] = True
team2 = Team.objects.filter(Q(name__contains=team_find) | Q(leader__contains=team_find))
if team2.exists() and not res_data['isOk']:
team2 = team2.first()
# if len(team2.mems.all()) < 3:
res_data['team'] = {'name': team2.name, 'id': team2.id, 'leader': team2.leader}
res_data['isOk'] = True
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
@csrf_exempt
def join_team(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
user = UserData.objects.filter(id=userid)
if user.exists():
user = user.first()
data = json.loads(request.body.decode())
teamid = data.get('id')
team = Team.objects.filter(id=teamid).first()
if len(team.mems.all()) >= 3:
res_data['errmsg'] = '队伍人数已满'
elif isScored(team):
res_data['errmsg'] = '队伍已经开始答题'
else:
user.team = team
user.save()
res_data['isOk'] = True
res_data['userInfo'] = getUserInfo(userid)
# 已经获得登陆授权~
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
@csrf_exempt
def quit_team(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
user = UserData.objects.filter(id=userid)
data = json.loads(request.body.decode())
if user.exists() and data.get('id'):
user = UserData.objects.filter(id=data.get('id')).first()
teamid = user.team.id
print(teamid)
if isScored(Team.objects.get(id=teamid)):
res_data['errmsg'] = '队伍已经开始答题'
else:
user.team = None
user.save()
res_data['isOk'] = True
res_data['userInfo'] = getUserInfo(userid)
# 已经获得登陆授权~
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
def logout(request):
request.session['userid'] = ''
res_data = {'isOk': True}
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
@csrf_exempt
def del_team(request):
res_data = {'isOk': False, 'errmsg': '无法完成操作'}
team = Team()
user = UserData()
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
user = UserData.objects.get(id=int(userid))
team = Team.objects.filter(leader=user.name)
if team.exists():
team = team.first()
res_data['isOk'] = True
else:
res_data['errmsg'] = '你好像不是队伍的队长~'
else:
res_data['errmsg'] = '未找到用户'
# 找到预备被删除的队伍~
if res_data['isOk']:
if isScored(team):
res_data['errmsg'] = '队伍已经开始答题'
res_data['isOk'] = False
else:
request.session['team'] = ''
team_del = team
team_del.delete()
res_data['userInfo'] = getUserInfo(userid)
json_data = json.dumps(res_data)
return HttpResponse(json_data, content_type="application/json")
# 答题系统apis
@csrf_exempt
def get_questions(request, page_num=0):
res_data = {'isOk': False, 'errmsg': '未知错误'}
finished = False
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
user = UserData.objects.select_related('team').filter(id=userid)
if user.exists():
user = user.first()
stage = get_stage()
seed = 0
if stage:
if stage.name == '预选赛':
finished = user.team.isFinished
seed = user.team.id
pass
elif stage.name == '团队赛':
finished = user.team.isFinished
seed = user.team.id
pass
elif stage.name == '复活赛':
finished = user.isFinished
seed = user.id
pass
elif stage.name == '个人赛':
finished = user.isFinished
seed = user.id
pass
else:
print('不在比赛时间')
res_data['errmsg'] = '不在比赛时间'
return HttpResponse(json.dumps(res_data).encode(), content_type="application/json")
page_now = []
res_data = {'isOk': False}
# 遍历所有难度,每个难度分别随机出来指定数目的题目
# 12 12 6
# 0 10 0
# 9 12 9
diffs = [0, 1, 2]
questions = []
for diff in diffs:
questions_fliter = Question.objects.filter(stage__name=stage.name, difficulty=diff).order_by('id')
print('难度%d,筛选之前共有题目%d-> ' % (diff, questions_fliter.count()), end='')
for i in questions_fliter:
print(i.id, end=' ')
print()
random.seed(seed)
questions_fliter = list(questions_fliter)
questions_fliter = random.sample(questions_fliter, len(questions_fliter) // 2)
print('难度%d,筛选完了还有题目%d-> ' % (diff, len(questions_fliter)), end='')
for i in questions_fliter:
print(i.id, end=' ')
print()
questions += questions_fliter
print('筛选完了总共还有题目%d-> ' % len(questions), end='')
for i in questions:
print(i.id, end=' ')
print()
# questions = Question.objects.filter(stage__name=stage.name).order_by('id')
# random.seed(user.team.id)
# questions = list(questions)
# questions = random.sample(questions, 30)
pages = Paginator(questions, 10)
print('一共有', pages.count, '题')
print('一共有', pages.num_pages, '页')
res_data['page_count'] = pages.count
res_data['page_num_pages'] = pages.num_pages
if page_num == 0:
res_data['isOk'] = True
page_num = 1
page_now = pages.page(page_num).object_list
answered_questions = 0
if stage.name in ['团队赛', '预选赛']:
answered_questions = Question.objects.filter(answer__user__team=user.team,
stage=stage).distinct()
elif stage.name in ['复活赛', '个人赛']:
answered_questions = Question.objects.filter(answer__user=user, stage=stage).distinct()
answered_num = len(answered_questions)
res_data['answered_num_all'] = answered_num
res_data['userInfo'] = getUserInfo(user.id)
elif page_num in pages.page_range:
page_now = pages.page(page_num).object_list
res_data['isOk'] = True
else:
res_data['isOk'] = False
res_data['errmsg'] = '页码超出范围'
if res_data['isOk']:
res_data['page_num'] = page_num
res_data['isFinished'] = finished
questions = json.loads(
serializers.serialize('json', page_now, fields=('question_text', 'choices', 'difficulty')))
# res_data['answered_choices'] = json.loads(
# serializers.serialize('json',
# Answer.objects.filter(user__team=user.team,
# question__in=page_now).order_by('pub_time'),
# fields=('question_text', 'choices', 'difficulty')))
for i in range(len(questions)):
# 查看最近选项功能
if stage.name in ['团队赛', '预选赛']:
answers = Answer.objects.filter(user__team=user.team, question_id=questions[i]['pk'],
stage=stage)
elif stage.name in ['复活赛', '个人赛']:
print('个人赛')
answers = Answer.objects.filter(user=user, question_id=questions[i]['pk'],
stage=stage)
if answers.exists():
questions[i]['answered_num'] = answers.aggregate(Sum('pub_num')).get('pub_num__sum', 0)
questions[i]['answered_choices'] = answers.order_by('pub_time')[0].choose
else:
questions[i]['answered_num'] = 0
questions[i]['answered_choices'] = -1
res_data['questions'] = questions
else:
res_data['errmsg'] = '请登陆后再试'
return JsonResponse(res_data)
@csrf_exempt
def get_stages(request):
time_now = int(time.time())
data = {'isOk': True, 'time_now': time_now}
stages = TimeTable.objects.all().order_by('id')
stages_data = json.loads(serializers.serialize('json', stages))
stages_fields = []
for i in range(len(stages)):
data_temp = {
'name': stages[i].name,
'timeStart': get_time(stages[i].timeStart),
'timeEnd': get_time(stages[i].timeEnd),
}
stages_fields.append(data_temp)
stages_data[i]['fields'] = data_temp
data['stages'] = stages_data
if get_stage():
data['stage'] = get_stage().id
else:
data['stage'] = 0
return HttpResponse(json.dumps(data).encode(), content_type="application/json")
def add_time(user, stage, diff):
if stage.name == '团队赛':
user.team.add_time += 2
user.team.save()
pass
elif stage.name == '复活赛':
if diff >= 1:
user.add_time += 2
user.save()
elif stage.name == '个人赛':
user.add_time += pow(2, diff)
user.save()
def add_score(user, stage, diff):
if stage.name == '预选赛':
user.team.score += 2
user.team.save()
elif stage.name == '团队赛':
user.team.score += 2 * diff + 1
print('加分' + str(2 * diff + 1))
user.team.save()
elif stage.name == '复活赛':
user.score += 2
user.save()
elif stage.name == '个人赛':
user.score += 3 * diff + 2
user.save()
@csrf_exempt
def submit_answer(request):
res_data = {'isOk': False, 'errmsg': '未知错误'}
user = isAuthed(request)
stage_type = ''
if user:
print(user, '身份验证成功')
if not isTeamed(user):
print('没有加入队伍')
res_data['errmsg'] = '没有加入队伍'
return JsonResponse(res_data)
stage = get_stage()
if not stage:
print('不在比赛时间')
res_data['errmsg'] = '不在比赛时间'
return JsonResponse(res_data)
if stage.name in ['团队赛', '预选赛']:
stage_type = 'team'
elif stage.name in ['复活赛', '个人赛']:
stage_type = 'single'
if user.team.isFinished and stage_type == 'team':
print('已经完成比赛,禁止答题')
res_data['errmsg'] = '禁止答题'
return JsonResponse(res_data)
if user.isFinished and stage_type == 'single':
print('已经完成比赛,禁止答题')
res_data['errmsg'] = '禁止答题'
return JsonResponse(res_data)
data = json.loads(request.body.decode())
if data.get('allOk'):
# 都答完了,计算罚时
answered_question_num = 0
if stage_type == 'team':
answered_question_num = Question.objects.filter(answer__user__team=user.team, stage=stage).count()
elif stage_type == 'single':
answered_question_num = Question.objects.filter(answer__user=user, stage=stage).count()
print(Question.objects.filter(answer__user=user, stage=stage), stage)
# fixme 根据比赛场次计算已答题(team/singe
question_num = Question.objects.filter(stage=stage).count() // 2
print('answered_question_num', answered_question_num)
print('question_num', question_num)
if question_num <= answered_question_num:
dt = timezone.now() - stage.timeStart
def add(obj):
# 罚时计算函数
obj.finish_time = dt.total_seconds() / 60 + obj.add_time
obj.isFinished = True
obj.save()
if stage_type == 'team':
add(user.team)
elif stage_type == 'single':
add(user)
# 已经完成比赛
res_data['isOk'] = True
else:
res_data['errmsg'] = "请完成所有题目后再提交哟"
return JsonResponse(res_data)
pk = data.get('question_pk', -1)
choice = data.get('choice', -1)
print(data)
if pk != -1 and choice != -1:
# 传入答案合法
choice_now = Answer.objects.filter(user=user, question_id=int(pk), stage=stage)
answered_num = 0
if stage_type == 'team':
print('stage_type == "team"')
team_answered = Answer.objects.filter(user__team=user.team, question_id=int(pk), stage=stage)
answered_num = team_answered.aggregate(Sum('pub_num')).get('pub_num__sum', 0)
if not answered_num:
answered_num = 0
elif stage_type == 'single':
print('stage_type == "single"')
answered_num = choice_now.aggregate(Sum('pub_num')).get('pub_num__sum', 0)
if not answered_num:
answered_num = 0
question_answered = Question.objects.get(pk=pk)
diff = question_answered.difficulty
# 进入判题逻辑
if choice_now.exists():
# user已经答过一次了
print('user已经答过一次了')
choice_now = choice_now.first()
# print(choice_now.pub_num)
if answered_num <= 1:
# 队没答过
if question_answered.key == choice:
# 答对了
if not choice_now.isRight:
print('上次答错了这次答对了,加分')
choice_now.isRight = True
choice_now.save()
add_score(user, stage, diff)
else:
print('上次答对了这次答对了,不变(是谁两次提交一样的答案?)')
else:
# 答错了 先罚时再说
add_time(user, stage, diff)
# 看看上次怎么样呢
if not choice_now.isRight:
print('上次答错了这次答错了,只罚时')
else:
if stage.name == '预选赛':
user.team.score -= 2
user.team.save()
elif stage.name == '团队赛':
user.team.score -= 2 * diff + 1
user.team.save()
elif stage.name == '复活赛':
user.score -= 2
user.save()
elif stage.name == '个人赛':
user.score -= 3 * diff + 2
user.save()
choice_now.isRight = False
choice_now.save()
print('上次答对了这次答错了减分儿,罚时')
choice_now.pub_num = choice_now.pub_num + 1
choice_now.choose = choice
choice_now.save()
res_data['isOk'] = True
else:
res_data['errmsg'] = '答题次数达到上限'
else:
# user没有答过
print('user没有答过')
if answered_num == 0:
if question_answered.key == choice:
# 答对了
add_score(user, stage, diff)
print('答对了,加分儿')
else:
# 答错了
add_time(user, stage, diff)
print('答错了,罚时')
res_data['isOk'] = True
elif answered_num == 1:
team_answered = team_answered.first()
if question_answered.key == choice:
# 答对了
if not team_answered.isRight:
print('上次答错了这次答对了,加分')
add_score(user, stage, diff)
else:
print('上次答对了这次答对了,不变(是谁两次提交一样的答案?)')
else:
# 答错了 先罚时再说
add_time(user, stage, diff)
# 看看上次怎么样呢
if not team_answered.isRight:
print('上次答错了这次答错了,不做')
else:
if stage.name == '预选赛':
user.team.score -= 2
user.team.save()
elif stage.name == '团队赛':
user.team.score -= 2 * diff + 1
user.team.save()
elif stage.name == '复活赛':
user.score -= 2
user.save()
elif stage.name == '个人赛':
user.score -= 3 * diff + 2
user.save()
print('上次答对了这次答错了减分儿')
# choice_now.pub_num = choice_now.pub_num + 1
# choice_now.choose = choice
# choice_now.save()
elif answered_num >= 2:
res_data['errmsg'] = '答题次数达到上限'
return JsonResponse(res_data)
res_data['isOk'] = True
Answer.objects.create(
user=user,
question=question_answered,
isRight=question_answered.key == choice,
choose=choice,
pub_num=1,
stage=stage
)
res_data['isOk'] = True
print('首次答题成功')
else:
res_data['errmsg'] = '传入数据不合法'
else:
res_data['errmsg'] = '获取授权信息失败'
return JsonResponse(res_data)
# return HttpResponse(json.dumps(res_data).encode(), content_type="application/json")
# tools
def get_team_questions(question_id):
print(question_id)
def get_stage():
stages = TimeTable.objects.all().order_by('id')
time_now = timezone.now()
for stage in stages:
if stage.timeStart <= time_now <= stage.timeEnd:
return stage
return None
def get_time(datetime):
return int(time.mktime(timezone.localtime(datetime).timetuple()))
def isAuthed(request):
if request.method == 'POST':
userid = request.session.get('userid')
if userid:
user = UserData.objects.filter(id=userid)
if user.exists():
user = user.first()
return user
return None
def isTeamed(user):
if user.team:
return True
else:
return False
def isScored(team):
if team.score > 0:
return True
else:
return False
def getUserInfo(userid):
user = UserData.objects.get(pk=userid)
stage = get_stage()
finished = False
team = user.team
team_data = {}
if team:
if stage:
if stage.name == '预选赛':
finished = user.team.isFinished
pass
elif stage.name == '团队赛':
finished = user.team.isFinished
pass
elif stage.name == '复活赛':
finished = user.isFinished
pass
elif stage.name == '个人赛':
finished = user.isFinished
pass
mems_data = []
mems = team.mems.all()
# print(list(mems.values_list('id', 'name')))
for mem in mems:
mems_data.append({'id': mem.id, 'name': mem.name})
team_data = {
'id': team.id,
'name': team.name,
'score': team.score,
'leader': team.leader,
'mems': mems_data
}
if stage:
print('1')
return {
'id': user.id,
'name': user.name,
'score': user.score,
'tel': user.tel,
'qq': user.qq,
'stage': stage.pk,
'team': team_data,
'isFinished': finished
}
else:
print('2')
return {
'id': user.id,
'name': user.name,
'score': user.score,
'tel': user.tel,
'qq': user.qq,
'stage': 0,
'team': team_data,
'isFinished': finished
}
def login_std(username='16122364', psword='123'):
# res_pre = session.get(url_login)
data = {
'card_id': username,
'password': psword,
}
res = session.post(url_login,
data=json.dumps(data), headers=headers)
if res.status_code == 200:
return json.loads(res.text)
else:
return False
def yxwh(request, txt):
content = {'txt': txt}
return render(request, 'JustSearch/yxwh.html', content)