def get_challenge_question_header(encrypted_message): help_text = _( _('Try to decrypt this message to win a free ticket.\n' '%(num_tickets_student)i tickets left out of %(total_num_tickets_student)i ' 'for students and %(num_tickets_non_student)i out of %(total_num_tickets_non_student)i' ' tickets left for non-students.') % { 'num_tickets_student': Challenge.unsolved_puzzles_left(student=True), 'total_num_tickets_student': Challenge.MAX_NUM_STUDENT, 'num_tickets_non_student': Challenge.unsolved_puzzles_left(student=False), 'total_num_tickets_non_student': Challenge.MAX_NUM_NON_STUDENT, }) template = """ <div id="div_id_challenge_question" class="form-group"> <label for="id_challenge_question_encrypted" class="control-label col-lg-3"> %(challenge_question_label)s </label> <div class="controls col-lg-7"> <p class="help-block">%(help_text)s</p> <p>%(encrypted_message)s</p> </div> </div> """ return template % { 'encrypted_message': encrypted_message, 'help_text': help_text, 'challenge_question_label': _('Challenge Question') }
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
def get(self, request, *args, **kwargs): language = request.LANGUAGE_CODE challenge = Challenge.get_unsolved_challenge(language=language) challenge_id = challenge.id if challenge else None order_id = Registration.generate_order_id() context = { 'order_id': order_id, 'form': RegistrationForm(challenge=challenge), 'stripe_public_key': self.get_stripe_public_key(), 'challenge_id': challenge_id } print 'GET ====== ' return render(request, self.template_name, context)
def clean_message(self, m): return Challenge.clean_message(m)