Example #1
0
def question_status(request, gid):
    try:
        guess = users.Guess.objects.get(id=gid)
        question = guess.question
        
        # Get the information for the next question
        question_m = qm.QuestionManager()
        try:
            next_q = question_m.get_current_question(request.user)
        except exception.NoQuestionReadyException:
            next_q = None

        answers = users.Answer.objects.filter(question=question, value=guess.value)
        
        answer = None
        if len(answers) > 0:
            answer = answers[0]
        
        return render_to_response('question_status.tpl', 
            { 'guess' : guess, 
              'question' : question, 
              'next_q' : next_q,
              'user': request.user,
              'stats' : UserStats.UserStats(request.user),
              'answer': answer },
            context_instance=RequestContext(request))
    except users.Guess.DoesNotExist:
        msghub.register_error(9, gid)
        return render_to_response('error.tpl', { 'errors' : msghub.get_printable_errors() })
Example #2
0
def build_acct(request):
    ruser = None
    try:
        ruser = users.RegisUser.objects.get(user=request.user)
    except users.RegisUser.DoesNotExist:
        # Update the user's username.
        u = request.user
        u.username = '******' % (u.first_name, u.last_name)

        # Create their RegisUser record.  
        # TODO: id=1 shouldn't be hard-coded here.      
        league = users.RegisLeague.objects.get(id=1)
        
        ruser = users.RegisUser(user=u, league=league)
        ruser.save()
        
        # Activate a question set for this user.
        concierge = question_link.Concierge()
        try:
            # Allocates a question set and activates the first
            # question.
            concierge.activate_question_set(u)
        except exception.NoQuestionSetReadyException:
            msghub.register_error(10, u)
            return render_to_response('error.tpl', { 'errors' : msghub.get_printable_errors() })
                
        u.save()
    # Save an event recording that the user just logged in. 
    users.RegisEvent(who=request.user, event_type="login").save()
                
    # If the user hasn't had a question released in 48 hours, release
    # a new one.
    question_m = qm.QuestionManager()
    try:
        # Raises a NoQuestionReadyException if user hasn't ever had a
        # question released.  This should never happen because a new
        # question is released when the question set is assigned to
        # a user.
        currentq = question_m.get_current_question(request.user)
        last_unlock = (datetime.datetime.now() - currentq.time_released)

        # If it's been more than 2 days, release a new question.
        if last_unlock > datetime.timedelta(days=2):
            # Raises a NoQuestionReadyException if there are no
            # questions left to unlock.
            question_m.activate_next(request.user)
    except exception.NoQuestionReadyException:
        pass

    # Correct, let's proceed.
    return redirect('/dash')
Example #3
0
def get_hint_details(request, tid, hinthash):
    try:
        template = users.QuestionTemplate.objects.get(id=tid)
        try:
            hints = users.QuestionHint.objects.filter(template=template)
        
            # TODO: order the hints somehow.
        
            # Get the specific hint that we want to return.
            chosen = None
            for hint in hints:
                if hint.get_hash() == hinthash:
                    chosen = hint
                
            # Tally the votes
            votes = users.QuestionHintRating.objects.filter(hint=chosen)
        
            upvotes = 0
            downvotes = 0
            for vote in votes:
                if vote.rating:
                    upvotes += 1
                else:
                    downvotes += 1
        
            # Show all of this info.
            chosen_data = {
                'hint_id' : hinthash,
                'hint_body' : chosen.text,
                'upvotes' : upvotes,
                'downvotes' : downvotes 
            }

            # Register an event saying that the user viewed the hint.
            users.RegisEvent(event_type='gethint', who=request.user, target=chosen.id).save()
            return HttpResponse(json.dumps(chosen_data), mimetype='application/json')

        except users.Question.DoesNotExist:
            msghub.register_error(9, tid)
            return render_to_response('error.tpl', {'errors' : msghub.get_printable_errors() })
    except users.QuestionTemplate.DoesNotExist:
        msghub.register_error(9, tid)
        return render_to_response('error.tpl', {'errors' : msghub.get_printable_errors() })
Example #4
0
def submit_hint(request, tid):
    template = users.QuestionTemplate.objects.get(id=tid)
    # Save the hint!
    try:
        user_q = users.Question.objects.exclude(status='retired').get(template=template, user=request.user)
        prev_hints = users.QuestionHint.objects.filter(template=template, src=request.user)

        # Check that the problem has been solved and that the user hasn't provided
        # any hints for this question already.
        if user_q.status == 'solved':# and len(prev_hints) is 0:
            users.QuestionHint(template=template, src=request.user, text=request.POST['hinttext']).save()
            msghub.register_message('Thanks for providing a hint!', template, True)
        # Error: the user has already provided a hint.
        elif len(prev_hints) > 0:
            msghub.register_error(8, template)
        # Error: the user hasn't answered the question yet.
        else:
            msghub.register_error(7, template)
    except users.Question.DoesNotExist:
        msghub.register_error(7, template)
        
    return redirect('/dash')