def add_bot_v(request, form = 0): if form == 0: form = BotForm() if request.method == 'POST': form = BotForm(request.POST, request.FILES) if form.is_valid(): # get challenge challenge = Challenge.objects.get( id = form.data["target_challenge"] ) # create directory for a bot directory = challenge.directory + "bots/" + form.data["name"].replace(" ", "_") + "/" os.makedirs(directory) # upload source file upload_file(directory, request.FILES['source_file']) # gather data program = Program(compiler = Compiler.objects.get(id = form.data["compiler"]), source_file = directory + request.FILES['source_file'].name ) program.save() bot = Bot( name = form.data["name"], playing_program = program, directory = directory, owner = request.user, target_challenge = challenge) # delay compilation recent_action = RecentAction(owner = request.user, message = "Bot validation: " + bot.name + " (for challenge: " + challenge.title + ")", state = ActionState.objects.get(name = 'IN_QUEUE')) recent_action.save() chain(compile_bot.si(bot, recent_action), enqueue_bots_battles.si(bot)).apply_async() return HttpResponseRedirect('/successful/') return render_to_response('ChallengeManagement/add_bot.xhtml', { "form": form, "title" : "Add Bot" }, context_instance = RequestContext(request));
def add_challenge_v(request): form = ChallengeForm() if request.method == 'POST': form = ChallengeForm(request.POST, request.FILES) if form.is_valid(): # create directory for a challenge directory = CHALLENGES_ROOT + form.data["title"].replace(" ", "_") + "/" os.makedirs(directory) # upload files upload_file(directory, request.FILES['source_file']) upload_file(directory, request.FILES['description_file']) # gather data program = Program(compiler = Compiler.objects.get(id = form.data["compiler"]), source_file = directory + request.FILES['source_file'].name ) program.save() challenge = Challenge ( title = form.data["title"], directory = directory, short_description = form.data["short_description"], description_file = directory + request.FILES['description_file'].name, owner = request.user, bots_per_game = form.data["bots_per_game"], game_duration = form.data["game_duration"], memory_usage = form.data["memory_usage"], judging_program = program) # queue compilation recent_action = RecentAction(owner = request.user, message = "Challenge validation: " + challenge.title, state = ActionState.objects.get(name = 'IN_QUEUE')) recent_action.save() compile_challenge.delay(challenge, recent_action) return HttpResponseRedirect('/successful/') return render_to_response('ChallengeManagement/add_challenge.xhtml', { "form": form, "title" : "Add Challenge" }, context_instance = RequestContext(request));
def delete_challenge_v(request, challenge_id): challenge = Challenge.objects.get( id = challenge_id ) if request.user == challenge.owner: challenge.to_delete = True challenge.save() recent_action = RecentAction(owner = request.user, message = "Removing challenge: " + challenge.title, state = ActionState.objects.get(name = 'IN_QUEUE')) recent_action.save() delete_challenge.delay(challenge, recent_action) return HttpResponseRedirect('/successful/') else: return challenge_details_v(request, challenge_id)
def delete_bot_v(request, bot_id): bot = Bot.objects.get( id = bot_id ) if request.user == bot.owner: bot.to_delete = True bot.save() recent_action = RecentAction(owner = request.user, message = "Removing bot: " + bot.name + " (for challenge: " + bot.target_challenge.title + ")", state = ActionState.objects.get(name = 'IN_QUEUE')) recent_action.save() delete_bot.delay(bot, recent_action) return HttpResponseRedirect('/successful/') else: return bot_v(request, bot_id)