Exemple #1
0
def index(request):
    if request.user.is_authenticated():
        return redirect('/dash')
    else:
        return render_to_response('index.tpl', 
            { 'errors' : msghub.get_printable_errors() }, 
            context_instance=RequestContext(request))
Exemple #2
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() })
Exemple #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() })
Exemple #4
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')
Exemple #5
0
def dash(request):
    question_m = qm.QuestionManager()
    current_q = None
    
    # First try to get the current question if it's already been activated.
    try:
        current_q = question_m.get_current_question(request.user)
        # Get the time until next release in seconds
        next_release = {}
        next_release_s = question_m.time_until_next(request.user).total_seconds()
        
        # Release a question if they've passed their deadline.
        if next_release_s < 0:
            question_m.activate_next(request.user)
            next_release_s = question_m.time_until_next(request.user).seconds
    # If that doesn't work, try to activate a new question.  This should work
    # unless there are no questions left to activate.
    except exception.NoQuestionReadyException:
        try:
            question_m.activate_next(request.user)
            
            next_release = {}
            next_release_s = question_m.time_until_next(request.user).seconds
        # If there are no more questions to activate, let them know that.  There's
        # nothing more we can do!
        except exception.NoQuestionReadyException:
            next_release = None
    
    if next_release is not None:
        next_release['days'] = int(math.floor(next_release_s / 86400))
        next_release_s -= (next_release['days'] * 86400)
        next_release['hours'] = int(math.floor(next_release_s / 3600))
        next_release_s -= (next_release['hours'] * 3600)
        next_release['minutes'] = int(math.floor(next_release_s / 60))
        next_release_s -= (next_release['minutes'] * 60)
        
    return render_to_response('dashboard.tpl', 
        { 'user': request.user, 
          'question': current_q, 
          'ttl' : next_release,
          'messages' : msghub.get_messages(),
          'errors' : msghub.get_printable_errors(),
          'stats' : UserStats.UserStats(request.user),
        },
        context_instance=RequestContext(request)
    )