Beispiel #1
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))

    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Registration Succesful!')
        return redirect(url_for('login'))

    return render_template('register.html', form=form, title='Register')
Beispiel #2
0
def handle_sign_up(request):
    post_result_dic = request.POST
    global temp_user_information_dic

    if post_result_dic.__contains__("username"):
        new_user = User()
        new_user.set_nickname(post_result_dic["username"])

        if post_result_dic.__contains__("email_address"):
            new_user.set_email_address(post_result_dic["email_address"])

            # send verification code by email
            verification_code = random.randint(0, 99999)
            verification_code_str = str(verification_code).zfill(5)
            correct_verification_code_str = verification_code_str
            verification_email = EmailMessage(
                "verification code", verification_code_str,
                "*****@*****.**",
                [post_result_dic["email_address"]])
            verification_email.send(fail_silently=False)

            if post_result_dic.__contains__("password"):
                new_user.set_password(post_result_dic["password_confirm"])

                # when a user finish the first step of signing up,
                # store a new user and his/her verification code in temp_user_information_dic
                temp_user_information_dic[post_result_dic["email_address"]] = [
                    new_user, correct_verification_code_str
                ]
                return HttpResponse()

    elif post_result_dic.__contains__("verification_code"):
        email_address = post_result_dic["email_address"]
        this_user_information = temp_user_information_dic[email_address]
        if post_result_dic["verification_code"] == this_user_information[1]:
            this_user_information[0].save()

            # when a user finish signing up and leave the sign_up_page,
            # delete his/her temp information stored in temp_user_information_dic
            del temp_user_information_dic[email_address]
            return HttpResponse()
        else:
            return HttpResponse("wrong")

    # when a user leave the sign_up_page,
    # delete his/her temporary information stored in temp_user_information_dic
    elif post_result_dic.__contains__("leave"):
        email_address = post_result_dic["email_address"]
        del temp_user_information_dic[email_address]
Beispiel #3
0
def registr(request):
    if request.method == 'GET':
        form = RegistrForm()
        return render(request, 'regis.html', {'form': form})
    elif request.method == 'POST':
        form = RegistrForm(request.POST)
        if form.is_valid() and (form.cleaned_data['password']
                                == request.POST['password2']):
            user = User(username=form.cleaned_data['username'],
                        first_name=form.cleaned_data['first_name'],
                        last_name=form.cleaned_data['last_name'])
            user.set_password(form.cleaned_data['password'])
            user.save()
            login(request, user)
        else:
            print(form.errors.as_data())
            return render(request, 'regis.html', {'form': form})
        return render(request, 'home.html')
    else:
        HttpResponseNotAllowed(['GET', 'POST'])