Пример #1
0
    def clean(self):
        self.cleaned_data = super(RegistrationForm, self).clean()
        self.cleaned_data['solved_challenge'] = self.challenge
        self.cleaned_data['has_solved_challenge'] = False

        if 'challenge_do_attempt' in self.cleaned_data and \
            self.cleaned_data["challenge_do_attempt"]:

            # clean strings to minimise errors due to weird characters, spacing, capitalization etc.
            user_solution = self.cleaned_data["challenge_question"].lower().strip()
            user_solution = Challenge.clean_message(user_solution)
            valid_solution = self.challenge.decrypted_message.lower().strip()
            valid_solution = Challenge.clean_message(valid_solution)
            
            if user_solution == valid_solution:
                self.cleaned_data['has_solved_challenge'] = True
                # Make sure there are challenges left in this category
                if 'is_student' in self.cleaned_data:
                    spots_left = Challenge.unsolved_puzzles_left(student=self.cleaned_data["is_student"])
                    if spots_left == 0:
                        self.add_error('challenge_do_attempt', 
                            _("There are no spots left for your category. "
                              "Please uncheck this box to proceed."))
                # Avoid the same person solving challenges
                if 'email' in self.cleaned_data and self.cleaned_data["email"]:
                    n = Registration.objects.filter(email=self.cleaned_data["email"]).count()
                    if n > 0:
                        self.add_error('challenge_do_attempt', 
                            _("Someone who solved the puzzle already registered with your email. "
                              "Please uncheck this box to proceed."))
            else:
                self.add_error('challenge_question', _("No luck :( Try again?"))
                self.add_error('challenge_do_attempt', _("Uncheck this if you'd like to proceed without a solution."))
        
        self.data['has_solved_challenge'] = self.cleaned_data['has_solved_challenge']        
        if 'discount_code_code' in self.cleaned_data and \
            self.cleaned_data["discount_code_code"]:
            # check that discount code exists
            code = self.cleaned_data["discount_code_code"]
            self.has_discount_code = False
            self.discount_code = None
            discount_code_exists = DiscountCode.objects.filter(code=code).exists()
            if discount_code_exists:
                discount_code = DiscountCode.objects.get(code=code)
                if not discount_code.is_active:
                    self.add_error('discount_code_code', _("Sorry, this discount code is inactive."))
                elif Registration.objects.filter(discount_code=discount_code).count() >= discount_code.max_coupons:
                    self.add_error('discount_code_code', _("Sorry, we ran out of coupons for this discount code."))
                else:
                    self.has_discount_code = True
                    self.discount_code = discount_code
            else:
                self.add_error('discount_code_code', _("Sorry, this discount code doesn't exist."))

        return self.cleaned_data
Пример #2
0
    def clean(self):
        self.cleaned_data = super(RegistrationForm, self).clean()
        self.cleaned_data['solved_challenge'] = self.challenge
        self.cleaned_data['has_solved_challenge'] = False

        if 'challenge_do_attempt' in self.cleaned_data and \
            self.cleaned_data["challenge_do_attempt"]:

            # clean strings to minimise errors due to weird characters, spacing, capitalization etc.
            user_solution = self.cleaned_data["challenge_question"].lower().strip()
            user_solution = Challenge.clean_message(user_solution)
            valid_solution = self.challenge.decrypted_message.lower().strip()
            valid_solution = Challenge.clean_message(valid_solution)
            
            if user_solution == valid_solution:
                self.cleaned_data['has_solved_challenge'] = True
                # Make sure there are challenges left in this category
                if 'is_student' in self.cleaned_data:
                    spots_left = Challenge.unsolved_puzzles_left(student=self.cleaned_data["is_student"])
                    if spots_left == 0:
                        self.add_error('challenge_do_attempt', 
                            _("There are no spots left for your category. "
                              "Please uncheck this box to proceed."))
                # Avoid the same person solving challenges
                if 'email' in self.cleaned_data and self.cleaned_data["email"]:
                    n = Registration.objects.filter(email=self.cleaned_data["email"]).count()
                    if n > 0:
                        self.add_error('challenge_do_attempt', 
                            _("Someone who solved the puzzle already registered with your email. "
                              "Please uncheck this box to proceed."))
            else:
                self.add_error('challenge_question', _("No luck :( Try again?"))
                self.add_error('challenge_do_attempt', _("Uncheck this if you'd like to proceed without a solution."))
        
        self.data['has_solved_challenge'] = self.cleaned_data['has_solved_challenge']        
        if 'discount_code_code' in self.cleaned_data and \
            self.cleaned_data["discount_code_code"]:
            # check that discount code exists
            code = self.cleaned_data["discount_code_code"]
            self.has_discount_code = False
            self.discount_code = None
            discount_code_exists = DiscountCode.objects.filter(code=code).exists()
            if discount_code_exists:
                discount_code = DiscountCode.objects.get(code=code)
                if not discount_code.is_active:
                    self.add_error('discount_code_code', _("Sorry, this discount code is inactive."))
                elif Registration.objects.filter(discount_code=discount_code).count() >= discount_code.max_coupons:
                    self.add_error('discount_code_code', _("Sorry, we ran out of coupons for this discount code."))
                else:
                    self.has_discount_code = True
                    self.discount_code = discount_code
            else:
                self.add_error('discount_code_code', _("Sorry, this discount code doesn't exist."))

        return self.cleaned_data
Пример #3
0
 def clean_message(self, m):
     return Challenge.clean_message(m)
Пример #4
0
 def clean_message(self, m):
     return Challenge.clean_message(m)