Example #1
0
def register():
    """
    Take care of user registration by either sending them the form, or
    registering them and signing them in.
    :return:
    """
    form = UserRegistrationForm()
    if request.method == 'POST':
        if not form.validate():
            # If the form isn't properly validated, return a json saying why
            return jsonify(
                {
                    'success': False,
                    'errors': form.errors
                }
            )
        new_user = User(
            firstname=form.name.data,
            lastname=form.surname.data,
            email=form.email.data,
            username=form.username.data,
        )
        new_user.set_password(password=form.password.data)
        db.session.add(new_user)
        db.session.commit()
        return redirect(url_for('login'), code=307)

    return render_template('register-form.html', form=form)
Example #2
0
def register():
    # caricamento dati provenienti dal form
    if request.method == 'POST':
        if request.form['password'] != request.form['confirm_password']:
            flash('Le password inserite sono diverse!')
            return redirect(url_for('auth.register'))
        form = UserRegistrationForm(request.form)

        # per il check del form devo guardare anche le tariffe
        tariffe = Tariffa.query(Tariffa.visibilita == True).order(
            Tariffa.order).fetch()
        my_choices = []
        for i, tar in enumerate(tariffe):
            my_choices.append((str(i + 1), tar.tariffa))
        form.tariffa.choices = my_choices

        # check form
        if form.validate():
            if insert_user(form):
                return render_template('login/not_allowed.html')
            else:
                flash('Registration error!')
                return redirect(url_for('auth.register'))

    # caricamento vista registrazione
    elif request.method == 'GET':
        form = UserRegistrationForm()

        # aggiunta tariffe possibili
        tariffe = Tariffa.query(Tariffa.visibilita == True).order(
            Tariffa.order).fetch()
        my_choices = []
        for i, tar in enumerate(tariffe):
            my_choices.append((str(i + 1), tar.tariffa))
        form.tariffa.choices = my_choices

        if len(tariffe) > 0:
            return render_template(
                'login/registration.html',
                form=form,
                len=len(tariffe),
                nomi_tariffe=[tar.tariffa for tar in tariffe],
                prezzo_tariffe=[tar.prezzo for tar in tariffe],
                descr_tariffe=[tar.description for tar in tariffe])
        else:
            return render_template('login/registration.html',
                                   form=form,
                                   len=1,
                                   nomi_tariffe=["Non ci sono tariffe."],
                                   descr_tariffe=["Tutti gratis, paliazzu!!"])