def apply(request): #申请入队,做一些可行性判断就可以 if ((not request.user) or request.user.is_authenticated == False): return HttpResponse("Please log in.", status=400) if (not (request.POST and request.POST.get('id'))): return HttpResponse("ID missing.", status=400) try: team = Team.objects.get(id=int(request.POST.get('id'))) except: return HttpResponse("Team not found.", status=400) contest = team.contest if tools.getTeamByUserContest(request.user, contest) != None: return HttpResponse("Already in a team now.", status=400) if datetime.datetime.now() > contest.registerTimeUp: return HttpResponse("Time for registration is up.", status=400) if team.members.count() >= contest.limitOfMember: return HttpResponse("The team is full.", status=400) membership = team.candidates.all().filter(id=request.user.id) if (request.POST and request.POST.get('cancel') == 'true'): if (len(membership) == 0): return HttpResponse("Application not found.", status=400) team.candidates.remove(request.user) return HttpResponse("Application is canceled.") else: if (len(membership) > 0): return HttpResponse("Application already exists.", status=400) team.candidates.add(request.user) return HttpResponse("Apply successfully.")
def apply(request): # 登录了才能申请 if (request.user and request.user.is_authenticated): user = request.user else: return HttpResponse("Please log in.", status = 400) # 用availableTime的id找到该开放时间对象 try: avaiTime = RsrvTimeAvailable.objects.get(id = int(request.POST.get('id'))) except: return HttpResponse("Available time not found.", status = 400) project = avaiTime.project # 用timestamp算出startTime、endTime try: startTime = tools.timestamp2datetime(int(request.POST.get('startTime'))) endTime = tools.timestamp2datetime(int(request.POST.get('endTime'))) assert startTime < endTime; except: return HttpResponse("Wrong start time or end time.", status = 400) #检查是否为过去时间 if (startTime.date() - datetime.timedelta(days=1) <= datetime.datetime.now().date()): return HttpResponse("Can't reserve a past time.", status = 400) #检查是否在开放时间内 if (startTime < avaiTime.startTime or endTime > avaiTime.endTime): return HttpResponse("The time is not available.") # 检查是否有未使用的申请时间 if (project.contest): team = tools.getTeamByUserContest(user, project.contest) # 跟队伍挂钩的项目必须组了队才能申请 if (len(team) == 0): return HttpResponse("Not in a team now.", status = 400) else: team = team[0] timeUsedList = RsrvTimeUsed.objects.filter(availableTime__project = project, endTime__gt = datetime.datetime.now(), user__in = team.members.all()) if (len(timeUsedList) >= 1): return HttpResponse("Already have a reservation.", status = 400) else: timeUsedList = RsrvTimeUsed.objects.filter(project = project, endTime__gt = datetime.datetime.now(), user = user) if (len(timeUsedList) >= 1): return HttpResponse("Already have a reservation.", status = 400) # 检查跟已预约时间是否冲突 timeUsedList = avaiTime.rsrvtimeused_set.all() for timeUsed in timeUsedList: if timeUsed.startTime < endTime and timeUsed.endTime > startTime: return HttpResponse("Conflict with existing reservation.", status = 400) # 建立新的已预约时间 newTimeUsed = RsrvTimeUsed(availableTime = avaiTime, startTime = startTime, endTime = endTime, user = request.user) newTimeUsed.save() return HttpResponse('Apply successfully')
def submit(request): try: assert (request.user.is_authenticated) except: return HttpResponse("Please log in.", status=400) user = request.user if (not request.POST): return HttpResponse("Only POST is allowed.", status=400) if (request.POST.get('id')): try: problem = Problem.objects.get(id=int(request.POST.get('id'))) except: return HttpResponse("Problem not found.", status=400) else: return HttpResponse("ID missing.", status=400) if (request.FILES.get('code')): code = request.FILES['code'] else: return HttpResponse("Code missing.", status=400) if (code.size > 100 * 1024): return HttpResponse("Code can't be larger than 100KB.", status=400) submission = ProblemSubmission(user=user, problem=problem, status='Waiting') result = {} if (request.POST.get('contest')): try: contest = Contest.objects.get(id=int(request.POST.get('contest'))) except: return HttpResponse("Contest error.", status=400) if (datetime.datetime.now() < contest.timeUp): team = tools.getTeamByUserContest(user, contest) if (team): submission.team = team result['inContest'] = True submission.save() submission.code = code submission.save() problemJudge.delay(submission) result['message'] = 'Submitted successfully' return HttpResponse(json.dumps(result))
def aiModify(request): if (request.POST and request.POST.get('id')): if (not request.user.is_authenticated): return HttpResponse("Please log in.", status=400) try: ai = GameAi.objects.get(id=int(request.POST.get('id'))) except: return HttpResponse("AI not found.", status=400) if not tools.userHasAiPermission(request.user, ai): return HttpResponse("Permission denied.", status=400) if request.POST.get('name'): ai.name = request.POST.get('name') if request.POST.get('introduction'): ai.introduction = request.POST.get('introduction') ai.save() return HttpResponse("Modified successfully.", status=200) else: if (request.POST == None or request.POST.get('game') == None): return HttpResponse("Game ID missing.", status=400) if (request.POST == None or request.POST.get('name') == None): return HttpResponse("AI name missing.", status=400) try: game = Game.objects.get(id=int(request.POST.get('game'))) except: return HttpResponse("Game not found.", status=400) name = request.POST.get('name') introduction = '' if request.POST.get('introduction'): introduction = request.POST.get('introduction') if game.contest: team = tools.getTeamByUserContest(request.user, game.contest) if team == None: return HttpResponse("Not in a team now.", status=400) GameAi(game=game, name=name, introduction=introduction, team=team).save() else: GameAi(game=game, name=name, introduction=introduction, user=request.user).save() return HttpResponse("Created successfully.", status=200)
def admin(request): #管理队伍,内容比较多 if ((not request.user.is_authenticated) or request.user.is_authenticated == False): return HttpResponse("Please log in.", status=400) user = request.user contest = None if (request.POST and request.POST.get('contest')): try: contest = Contest.objects.get(id=int(request.POST.get('contest'))) except: return HttpResponse("Contest not found.", status=400) team = None if (request.POST and request.POST.get('team')): try: team = Team.objects.get(id=int(request.POST.get('team'))) except: return HttpResponse("Team not found.", status=400) if (request.POST and request.POST.get('name')): if (team == None): #如果提供了名字,没有给队伍定位,视为新建一个队伍 if (contest == None): return HttpResponse("Contest missing.", status=400) if datetime.datetime.now() > contest.registerTimeUp: return HttpResponse("Time for registration is up.", status=400) if tools.getTeamByUserContest(request.user, contest) != None: return HttpResponse("Already in a team now.", status=400) team = Team(captain=user, contest=contest, name=request.POST.get('name')) team.save() team.members.add(user) else: if (request.user.is_authenticated and team.captain.username == request.user.username): team.name = request.POST.get('name') if (request.POST and request.POST.get('introduction')): team.introduction = request.POST.get('introduction') else: team.introduction = '' team.save() if (team == None): return HttpResponse("Team not found.", status=400) #判断是不是队长,如果不是队长就只能做退出操作 if (request.user.is_authenticated and team.captain.username != request.user.username): if (request.POST and request.POST.get('quit')): team.members.remove(request.user) return HttpResponse("Quit successfully.", status=200) else: return HttpResponse("You are not the captain.", status=400) #通过入队申请 if (request.POST and request.POST.get('accept')): try: targetUser = User.objects.get(id=int(request.POST.get('accept'))) except: return HttpResponse("User not found.", status=400) if datetime.datetime.now() > team.contest.registerTimeUp: return HttpResponse("Time for registration is up.", status=400) if team.members.count() >= team.contest.limitOfMember: return HttpResponse("The team is full.", status=400) team.members.add(targetUser) targetUser.apply.clear() #拒绝入队申请 if (request.POST and request.POST.get('refuse')): try: targetUser = User.objects.get(id=int(request.POST.get('refuse'))) except: return HttpResponse("User not found.", status=400) team.candidates.remove(targetUser) #踢出队伍 if (request.POST and request.POST.get('dismiss')): if datetime.datetime.now() > team.contest.registerTimeUp: return HttpResponse("Time for registration is up.", status=400) try: targetUser = User.objects.get(id=int(request.POST.get('dismiss'))) except: return HttpResponse("User not found.", status=400) team.members.remove(targetUser) #解散队伍 if (request.POST and request.POST.get('disband')): if datetime.datetime.now() > team.contest.registerTimeUp: return HttpResponse("Time for registration is up.", status=400) team.delete() return HttpResponse('Change successfully')