Example #1
0
   def get_context_data(self, **kwargs):
      context = super(HomepageView, self).get_context_data(**kwargs)
      context['is_contest_started'] = is_contest_started()
      context['contest_begin_date'] = begin_date().strftime('%Y %B %d %H:%M:%S')
      context['contest_end_date']   = end_date().strftime('%Y %B %d %H:%M:%S')

      return context
Example #2
0
def validate(request, pk):
   errors = []
   _messages = {
      "invalid_key": {
         "level": messages.ERROR,
         "text": _("Sorry, wrong key. Try again !")
         },
      "valid_key": {
         "level": messages.SUCCESS,
         "text": _("Congratulation, you found the key !")
         },
      "breakthrough": {
         "level": messages.WARNING,
         "text": _("Great, you just made a breakthrough !")
         },
      "already_done": {
         "level": messages.INFO,
         "text": _("This challenge was already done by your team !")
         },
      "before_start": {
         "level": messages.INFO,
         "text": _("The contest is not yet started !")
         },
      "after_end": {
         "level": messages.INFO,
         "text": _("The contest is finished !")
         },
      }

   # Check if the contest is open
   now = timezone.now()

   if (now <= begin_date()):
      messages.add_message(request,
                           _messages['before_start']['level'],
                           _messages['before_start']['text'])

      return HttpResponseRedirect('/challenges/')

   if (now >= end_date()):
      messages.add_message(request,
                           _messages['after_end']['level'],
                           _messages['after_end']['text'])
      return HttpResponseRedirect('/challenges/')

   if request.method == 'POST':
      form = ChallengeValidationForm(request.POST)

      if form.is_valid():
         key = form.cleaned_data['key']

         if key == Challenge.objects.get(pk=pk).key:
            # Key is valid
            messages.add_message(request,
                                 _messages['valid_key']['level'],
                                 _messages['valid_key']['text'])

            team = request.user.team_set.filter()[:1].get()
            challenge = Challenge.objects.get(pk=pk)
            try:
               Validation.objects.filter(challenge=challenge)[:1].get()
               # Not a breakthrough
               try:
                  Validation.objects.get(challenge=challenge, team=team)
                  # Team has already validated this challenge
                  messages.add_message(request,
                                       _messages['already_done']['level'],
                                       _messages['already_done']['text'])

                  return HttpResponseRedirect('/challenges/')

               except Validation.DoesNotExist:
                  # Validation was not already registered, creating it
                  validation = Validation(date=now,
                                          team=team,
                                          user=request.user,
                                          challenge=challenge)
                  validation.save()
                  team.is_active = True
                  team.save()

            except Validation.DoesNotExist:
               # Breakthrough !
               messages.add_message(request,
                                    _messages['breakthrough']['level'],
                                    _messages['breakthrough']['text'])
               validation = Validation(date=now,
                                       team=team,
                                       user=request.user,
                                       challenge=challenge)
               validation.save()
               team.is_active = True
               team.save()

            # Recompute score for everybody #
            # ############################# #
            teams =  Team.objects.filter(is_active=True).all()
            teams_count = teams.count()

            # Compute the score for each challenge and the first team
            challenge_scores = {}
            for challenge in Challenge.objects.all():
               validations = Validation.objects.filter(challenge=challenge)

               first_validation = validations.first()
               if (first_validation is not None):
                  first_team = first_validation.team
               else:
                  first_team = None

               count = validations.count()

               # challenge_score contains the score for a challenge and the
               # team which make de the breakthrough (None otherwise).
               challenge_scores[challenge.pk] = \
                           (teams_count * (teams_count - count + 1), first_team)

            # Compute the score for each team
            for team  in teams:
               score = 0
               breakthroughs = 0
               validations = Validation.objects.filter(team=team)
               for validation in validations:
                  score_challenge, first_team = challenge_scores[validation.challenge.pk]
                  score += score_challenge
                  # Get the bonus if team has a breakthrough
                  if (team == first_team):
                     score += teams_count
                     breakthroughs += 1

               team.score = score
               team.breakthroughs = breakthroughs
               team.save()

         else:
            # Key is not valid
            messages.add_message(request,
                                 _messages['invalid_key']['level'],
                                 _messages['invalid_key']['text'])

   else:
      form = ChallengeValidationForm()
      messages.add_message(request,
                           _messages['invalid_key']['level'],
                           _messages['invalid_key']['text'])

   return HttpResponseRedirect('/challenges/')