def _edit(request, challenge):
    nav_selected = "challenges"
    form = ChallengeForm(instance=challenge, data=(request.POST or None))
    if form.is_valid():
        form.save()
        return redirect(challenge)
    type_label = challenge.id and 'Edit' or 'Create'
    return render_to_response('challenges/edit.html', locals(), context_instance=RequestContext(request))
def _edit(request, challenge):
    nav_selected = "challenges"
    form = ChallengeForm(instance=challenge, data=(request.POST or None))
    if form.is_valid():
        form.save()
        return redirect(challenge)
    type_label = challenge.id and 'Edit' or 'Create'
    return render_to_response('challenges/edit.html',
                              locals(),
                              context_instance=RequestContext(request))
Exemplo n.º 3
0
def admin():
	form = ChallengeForm()
	if request.method == 'POST':
		if form.validate_on_submit():
			challenge = Challenge(form.ChallengeName.data, g.user.PersonID, datetime.datetime.now())
			commit_to_db(challenge)
			flash("Successfullly created new challenge")
			return redirect(url_for('discussions_all'))
		else:
			flash("Couldn't initiate your new challenge")
			return redirect(url_for('admin'))
	if request.method == 'GET':
		new_users = get_all_persons(Confirmed = False)
		return render_template('admin.html', form = form, new_users = new_users)
Exemplo n.º 4
0
def challenge(request, id):
    """ Displays a challenge, only if status = accepted
    This is play """
    chall_user = request.user.get_profile().get_extension(ChallengeUser)

    chall = get_object_or_404(Challenge, pk=id)

    # check to see if challenge was already submitted
    try:
        participant = chall.participant_for_player(chall_user)
    except:
        raise Http404

    if participant.played:
        return do_result(request, _('You have already submitted this challenge'\
                                   ' and scored %.2f points') % participant.score)

    # this is caught by chall_user
    #if not chall_user.can_play(chall):
    #    return do_result(request, _('You cannot play this challenge.'))

    if request.method == 'GET' and not chall.is_started_for_user(chall_user):
        chall.set_start(chall_user)

    if request.method == "POST":
        form = ChallengeForm(chall, request.POST)
        results = chall.set_played(chall_user, form.get_response())
        form.check_self_boxes()
        if results.get('results', False):
            results['results'] = form.get_results_in_order(results['results'])
            questions_and_answers = zip(form.visible_fields(),
                                        results['results'])
        else:
            questions_and_answers = None
        return render_to_response(
            'challenge/result.html', {
                'challenge': chall,
                'challenge_user': chall_user,
                'points': results['points'],
                'form': form,
                'questions_and_answers': questions_and_answers
            },
            context_instance=RequestContext(request))
    else:
        form = ChallengeForm(chall)

    seconds_left = chall.time_for_user(chall_user)
    return render_to_response('challenge/challenge.html', {
        'challenge': chall,
        'form': form,
        'challenge_user': chall_user,
        'seconds_left': seconds_left
    },
                              context_instance=RequestContext(request))
Exemplo n.º 5
0
 def get(self, request, *args, **kwargs):
     if not self.chall.is_started_for_user(self.chall_user):
         self.chall.set_start(self.chall_user)
     form = ChallengeForm(self.chall)
     seconds_left = self.chall.time_for_user(self.chall_user)
     return render_to_response('challenge/challenge.html',
             {'challenge': self.chall, 'form': form, 'challenge_user': self.chall_user,
             'seconds_left': seconds_left},
             context_instance=RequestContext(request))
Exemplo n.º 6
0
def _edit(request, challenge):
    nav_selected = "challenges"
    title = challenge.title or ''
    title = title.split(":")
    if len(title) == 1:
        target = ''
        demand = title[0].strip()
    else:
        target = title[0].strip()
        demand = title[1].strip()

    form = ChallengeForm(request.user, instance=challenge, 
                         data=(request.POST or None),
                         initial={'groups': request.GET.getlist("groups"),
                                  "target": target,
                                  "demand": demand})
    if form.is_valid():
        form.save()
        return redirect(challenge)
    type_label = challenge.id and 'Edit' or 'Create'
    return render_to_response('challenges/edit.html', locals(), context_instance=RequestContext(request))
Exemplo n.º 7
0
def challenge(request, id):
    """ Displays a challenge, only if status = accepted
    This is play """
    chall_user = request.user.get_profile().get_extension(ChallengeUser)

    chall = get_object_or_404(Challenge, pk=id)

    # check to see if challenge was already submitted
    try:
        participant = chall.participant_for_player(chall_user)
    except:
        raise Http404

    if participant.played:
        return do_result(
            request, _("You have already submitted this challenge" " and scored %.2f points") % participant.score
        )

    # this is caught by chall_user
    # if not chall_user.can_play(chall):
    #    return do_result(request, _('You cannot play this challenge.'))

    if request.method == "GET" and not chall.is_started_for_user(chall_user):
        chall.set_start(chall_user)

    if request.method == "POST":
        form = ChallengeForm(chall, request.POST)
        results = chall.set_played(chall_user, form.get_response())
        form.check_self_boxes()
        if results.get("results", False):
            results["results"] = form.get_results_in_order(results["results"])
            questions_and_answers = zip(form.visible_fields(), results["results"])
        else:
            questions_and_answers = None
        return render_to_response(
            "challenge/result.html",
            {
                "challenge": chall,
                "challenge_user": chall_user,
                "points": results["points"],
                "form": form,
                "questions_and_answers": questions_and_answers,
            },
            context_instance=RequestContext(request),
        )
    else:
        form = ChallengeForm(chall)

    seconds_left = chall.time_for_user(chall_user)
    return render_to_response(
        "challenge/challenge.html",
        {"challenge": chall, "form": form, "challenge_user": chall_user, "seconds_left": seconds_left},
        context_instance=RequestContext(request),
    )
Exemplo n.º 8
0
def challenge(request, id):
    """ Displays a challenge, only if status = accepted
    This is play """
    chall_user = request.user.get_profile().get_extension(ChallengeUser)

    chall = get_object_or_404(Challenge, pk=id)

    # check to see if challenge was already submitted
    try:
        participant = chall.participant_for_player(chall_user)
    except:
        raise Http404

    if participant.played:
        return do_result(request, _('You have already submitted this challenge'\
                                   ' and scored %.2f points') % participant.score)

    # this is caught by chall_user
    #if not chall_user.can_play(chall):
    #    return do_result(request, _('You cannot play this challenge.'))

    if request.method == 'GET' and not chall.is_started_for_user(chall_user):
        chall.set_start(chall_user)

    if request.method == "POST":
        form = ChallengeForm(chall, request.POST)

        results = chall.set_played(chall_user, form.get_response())

        return render_to_response('challenge/result.html',
            {'challenge': chall, 'results': results},
            context_instance=RequestContext(request))
    else:
        form = ChallengeForm(chall)

    seconds_left = chall.time_for_user(chall_user)
    return render_to_response('challenge/challenge.html',
            {'challenge': chall, 'form': form, 'challenge_user': chall_user, 'seconds_left': seconds_left},
            context_instance=RequestContext(request))
Exemplo n.º 9
0
def challenge(challenge_id):
	valid = db.session.query(Challenge).filter_by(active=True).filter_by(id=challenge_id).first()
	if not valid:
		flash(('Not an active challenge, sorry!'))
		return redirect(url_for('home'))

	timer = db.session.query(Timer).filter_by(user_id=g.user.id).filter_by(challenge_id=challenge_id).first()
	if not timer:
		newTimer = Timer(time.time(), challenge_id, g.user.id)
		db.session.add(newTimer)
		db.session.commit()
	else:
		if timer.end_time:
			flash(("You've already completed that challenge!"))
			return redirect(url_for('home'))

	form = ChallengeForm() if request.method == 'POST' else ChallengeForm(request.args)
	
	if form.validate_on_submit():
		if form.answer.data == valid.answer:
			end = time.time()
			user = db.session.query(User).filter_by(id=g.user.id).first()

			rank = db.session.query(Timer).filter(Timer.end_time > 0).filter_by(challenge_id=challenge_id).count()
			timer.end_time = end

			timeDelta = end - timer.start_time
			score = math.floor(10000 + max((((86400 - timeDelta)/86400.00) * 1000) + ((15 - (rank + 1))/15.0 * 2000), -2500))
			user.score = valid.weight * score + user.score
			timer.rank = rank + 1

			db.session.commit()
			flash(('Your answer was correct! You received a score of ' + str(score) ))
			return redirect(url_for('home'))
		else:
			flash(('Ooh sorry...wrong answer, try again!'))
			return redirect(url_for('challenge', challenge_id=challenge_id))
	return render_template('challenge.html', form=form, challenge=valid)
Exemplo n.º 10
0
def new(request):
    if request.method == 'POST':
        form = ChallengeForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/challenges')
    else:
        form = ChallengeForm()
    return render_to_response('new_challenge.html',
        {
            "form": form
        },
        context_instance=RequestContext(request))
Exemplo n.º 11
0
    def post(self, request, **kwargs):
        form = ChallengeForm(self.chall, request.POST)
        results = self.chall.set_played(self.chall_user, form.get_response())
        form.check_self_boxes()

        if results.get('results', False):
            results['results'] = form.get_results_in_order(results['results'])
            questions_and_answers = zip(form.visible_fields(), results['results'])
        else:
            questions_and_answers = None
        return render_to_response('challenge/result.html',
            {'challenge': self.chall, 'challenge_user': self.chall_user,
            'points': results['points'], 'form' : form,  'questions_and_answers' : questions_and_answers},
            context_instance=RequestContext(request))
Exemplo n.º 12
0
def post_challenge():

    form = ChallengeForm()
    runIds = form.data['runs']
    if runIds is None or len(runIds) != 1:
        return redirect(
            '/?challengeError=Please select exactly one run to challenge')

    if current_user is not None and hasattr(current_user, 'id'):
        prev_challenged_run = db.session.query(Challenge).filter(
            Challenge.runner_id == current_user.id).first()
        #found a previosly challenged run, gotta make it unchallenged and then challenge the next one
        if prev_challenged_run:
            db.session.delete(prev_challenged_run)
            if prev_challenged_run.run_id != int(runIds[0]):
                # I'm not unchallenging a run, but challenging a new one
                new_challenge = db.session.query(Run).filter(
                    Run.runner_id == current_user.id,
                    Run.id == runIds[0]).first()
                if new_challenge is not None:
                    challenge = Challenge()
                    challenge.run_id = runIds[0]
                    challenge.runner = current_user
                    challenge.runner_id = current_user.id
                    challenge.run = new_challenge
                    latest_id = db.session.query(func.max(Run.id)).scalar()
                    challenge.latest_run_id = latest_id
                    db.session.add(challenge)
            db.session.commit()
        else:  # just challenge the new one
            new_challenge = db.session.query(Run).filter(
                Run.runner_id == current_user.id, Run.id == runIds[0]).first()
            if new_challenge is not None:
                challenge = Challenge()
                challenge.run_id = runIds[0]
                challenge.runner = current_user
                challenge.runner_id = current_user.id
                challenge.run = new_challenge
                latest_id = db.session.query(func.max(Run.id)).scalar()
                challenge.latest_run_id = latest_id
                db.session.add(challenge)
            db.session.commit()
    else:
        return redirect("/login")

    return redirect("/")
Exemplo n.º 13
0
    def post(self, request, **kwargs):
        form = ChallengeForm(self.chall, request.POST)
        results = self.chall.set_played(self.chall_user, form.get_response())
        form.check_self_boxes()

        if results.get("results", False):
            results["results"] = form.get_results_in_order(results["results"])
            questions_and_answers = zip(form.visible_fields(), results["results"])
        else:
            questions_and_answers = None
        return render_to_response(
            "challenge/result.html",
            {
                "challenge": self.chall,
                "challenge_user": self.chall_user,
                "points": results["points"],
                "form": form,
                "questions_and_answers": questions_and_answers,
            },
            context_instance=RequestContext(request),
        )