示例#1
0
文件: account.py 项目: wjcstp/mltshp
    def post(self):
        key_value = self.get_argument('key', '')

        voucher = is_valid_voucher_key(key_value)

        user = self.get_current_user_object()
        if voucher is not None:
            if not voucher.claimed_by_user_id:
                voucher.apply_to_user(user)
                return self.redirect("/account/settings")

        promotions = Promotion.active()

        self.add_error('key', 'Invalid discount code')
        return self.render("account/redeem.html",
            key=key_value, promotions=promotions)
示例#2
0
    def post(self):
        if options.disable_signups:
            return

        name_value = self.get_argument('name', '')
        email_value = self.get_argument('email', '')
        key_value = self.get_argument('key', '')
        has_errors = False
        voucher = None

        if key_value != '':
            voucher = is_valid_voucher_key(key_value)
            if voucher is None:
                has_errors = True
                self.add_error('key', 'Invalid discount code')

        new_user = User(name=name_value, email=email_value, email_confirmed=0)
        new_user.set_and_confirm_password(
            self.get_argument('password', ""),
            self.get_argument('password_again', ""))

        skip_recaptcha = self.get_argument('_skip_recaptcha_test_only', False)
        if not options.recaptcha_private_key:
            skip_recaptcha = True

        #recaptcha hotfix
        if not skip_recaptcha:
            response = captcha.submit(
                self.get_argument('recaptcha_challenge_field'),
                self.get_argument('recaptcha_response_field'),
                options.recaptcha_private_key, self.request.remote_ip)

            if not response.is_valid:
                has_errors = True
                self.add_error('recaptcha', 'Invalid captcha')

        if not has_errors:
            try:
                # create form asserts the user agress to terms of use
                new_user.tou_agreed = 1

                if new_user.save():
                    if options.postmark_api_key:
                        # i'd like to NOT invalidate_email in the
                        # case of using a voucher, but the person
                        # may use a different email for MLTSHP than
                        # they used for receiving their voucher, so...
                        new_user.invalidate_email()
                    else:
                        # we have no way to send a verification
                        # email, so we're gonna trust 'em
                        new_user.email_confirmed = 1
                        new_user.save()

                    query_str = ''
                    if voucher is not None:
                        voucher.apply_to_user(new_user)
                        query_str = '?vid=%s' % str(voucher.id)

                    self.log_user_in(new_user)
                    if new_user.email_confirmed:
                        return self.redirect('/')
                    else:
                        return self.redirect('/confirm-account%s' % query_str)
            except torndb.IntegrityError:
                #This is a rare edge case, so we handle it lazily -- IK.
                pass
            has_errors = True
            self.add_errors(new_user.errors)

        #recaptcha hot fix
        captcha_string = captcha.displayhtml(options.recaptcha_public_key)
        promotions = Promotion.active()
        return self.render("account/create.html",
                           name=name_value,
                           email=email_value,
                           key=key_value,
                           recaptcha=captcha_string,
                           promotions=promotions)
示例#3
0
    def post(self):
        if options.disable_signups:
            return

        name_value = self.get_argument('name', '')
        email_value = self.get_argument('email', '')
        key_value = self.get_argument('key', '')
        has_errors = False
        voucher = None

        if key_value != '':
            voucher = is_valid_voucher_key(key_value)
            if voucher is None:
                has_errors = True
                self.add_error('key', 'Invalid discount code')

        new_user = User(name=name_value, email=email_value, email_confirmed=0)
        new_user.set_and_confirm_password(
            self.get_argument('password', ""),
            self.get_argument('password_again', ""))

        skip_recaptcha = self.get_argument('_skip_recaptcha_test_only', False)
        if not options.recaptcha_secret_key:
            skip_recaptcha = True

        # recaptcha validation, when configured
        if not skip_recaptcha:
            response = requests.post(
                "https://www.google.com/recaptcha/api/siteverify",
                params={
                    "secret": options.recaptcha_secret_key,
                    "response": self.get_argument("recaptcha_token"),
                })
            try:
                result = response.json()
                if not result["success"] or result["score"] < 0.5:
                    has_errors = True
                    self.add_error("recaptcha", "Invalid captcha")
            except ValueError:
                has_errors = True
                self.add_error("recaptcha", "Invalid captcha")

        if not has_errors:
            try:
                # create form asserts the user agress to terms of use
                new_user.tou_agreed = 1

                if new_user.save():
                    if options.postmark_api_key:
                        # i'd like to NOT invalidate_email in the
                        # case of using a voucher, but the person
                        # may use a different email for MLTSHP than
                        # they used for receiving their voucher, so...
                        new_user.invalidate_email()
                    else:
                        # we have no way to send a verification
                        # email, so we're gonna trust 'em
                        new_user.email_confirmed = 1
                        new_user.save()

                    query_str = ''
                    if voucher is not None:
                        voucher.apply_to_user(new_user)
                        query_str = '?vid=%s' % str(voucher.id)

                    self.log_user_in(new_user)
                    if new_user.email_confirmed:
                        return self.redirect('/')
                    else:
                        return self.redirect('/confirm-account%s' % query_str)
            except torndb.IntegrityError:
                #This is a rare edge case, so we handle it lazily -- IK.
                pass
            has_errors = True
            self.add_errors(new_user.errors)

        promotions = Promotion.active()
        return self.render("account/create.html",
                           name=name_value,
                           email=email_value,
                           key=key_value,
                           recaptcha_site_key=options.recaptcha_site_key,
                           promotions=promotions)