Exemplo n.º 1
0
def register():
    if request.method == 'GET':
        return render_template('register.html')
    elif request.method == 'POST':
        form = forms.AccountForm(request.form)

        if not form.validate():
            return render_template('error.html',
                                   message='Invalid registration'), 400

        username, password = form.username.data.lower(), form.password.data

        if db.has(username):
            return render_template('error.html',
                                   message='User already exists!'), 400

        db.put(
            username, {
                'tasks': [],
                'password':
                bcrypt.hashpw(password.encode(),
                              bcrypt.gensalt()).decode('utf-8')
            })

        session['username'] = username

        return redirect('/tasks')
Exemplo n.º 2
0
def account():
    form = forms.AccountForm()
    # validate_on_submit() method is going to return False in case the function skips the if
    # statement and goes directly to render the template in the last line of the function
    if request.method == "POST" and form.validate_on_submit():
        ef.update_user(current_user.id, form.username.data, form.email.data,
                       form.cf.data)
        flash('I tuoi dati sono stati modificati correttamente')
        return redirect(url_for('account'))
        # render_template() takes a template filename and a variable list of template arguments and returns
        # the same template, but with all the placeholders in it replaced with actual values.
    return render_template('account.html',
                           title='I tuoi dati',
                           form=form,
                           email=current_user.email,
                           username=current_user.username,
                           cf=current_user.cf)
Exemplo n.º 3
0
def create_account(accountid=None):
    form = forms.AccountForm()
    form.owner.choices = [(str(user.id), user.fullname)
                          for user in models.User.select()]
    title = 'Create Account'
    if form.validate_on_submit():
        flash("Account created!", 'success')
        models.Account.create_account(name=form.name.data,
                                      owner=g.user._get_current_object(),
                                      created_by=g.user._get_current_object(),
                                      account_type=form.account_type.data,
                                      street=form.street.data,
                                      city=form.city.data,
                                      state=form.state.data,
                                      country=form.country.data,
                                      website=form.website.data,
                                      mrr=0,
                                      arr=0)
        return redirect('account')
    return render_template('create.html', form=form, title=title)
Exemplo n.º 4
0
def login():
    form = forms.AccountForm(request.form)

    if not form.validate():
        return render_template('error.html', message='Invalid login'), 400

    username = form.username.data.lower()

    if not db.has(username):
        return render_template('error.html', message='Invalid login'), 400

    user = db.get(username)

    if not bcrypt.checkpw(form.password.data.encode(),
                          user['password'].encode()):
        return render_template('error.html', message='Invalid login'), 400

    session['username'] = username

    return redirect('/tasks')
Exemplo n.º 5
0
def edit_account(accountid):
    form = forms.AccountForm()
    form.owner.choices = [(str(user.id), user.fullname)
                          for user in models.User.select()]
    record = models.Account.select().where(
        accountid == models.Account.id).dicts().get()
    title = 'Edit Account'
    if form.validate_on_submit():
        flash("Account updated!", 'success')
        edited_account = models.Account.update(
            name=form.name.data,
            owner=form.owner.data,
            account_type=form.account_type.data,
            street=form.street.data,
            city=form.city.data,
            state=form.state.data,
            country=form.country.data,
            website=form.website.data).where(accountid == models.Account.id)
        edited_account.execute()
        return redirect(url_for('account', accountid=accountid))
    return render_template('edit.html', form=form, record=record, title=title)
Exemplo n.º 6
0
def account():
    form = forms.AccountForm(request.form)
    form.country.choices = [(c.countryid, c.countryname)
                            for c in models.get_all_countries()]
    form.chefspec.choices = [(c.cuisineid, c.cuisine_name)
                             for c in models.get_all_cuisines()]
    form.chefspec.choices.append((-1, "Pick a Specialty..."))
    #form.custpref.choices = [(c.cuisineid, c.cuisine_name) for c in models.get_all_cuisines()]

    # populate form with existing info
    user = models.get_user_by_id(session['userid'])
    chef = models.get_chef_by_id(session['chefid'])
    cust = models.get_customer_by_id(session['custid'])

    print form.first_name.data, form.last_name.data
    print form.country.data, form.usertype.data
    print request.method, form.validate()

    if (request.method == 'POST'):
        print request.form

        # delete chef/cust only options from the form to get through validation
        if (not chef):
            del form.chefspec
        if (not cust):
            del form.custpref

        if (form.validate()):
            print "posting..."
            fname = form.first_name.data
            lname = form.last_name.data
            email = form.email_id.data
            passwd = sha256_crypt.encrypt(str(form.password.data))
            user_type = form.usertype.data
            aptno = form.apartment_no.data
            street = form.street.data
            city = form.city.data
            state = form.state.data
            zipcode = int(form.zipcode.data) if form.zipcode.data else None
            country = int(form.country.data) if form.country.data else None
            phoneno = int(
                form.phone_number.data) if form.phone_number.data else None
            chefspec = None
            reachouts = None
            custpref = None

            if (chef):
                chefspec = int(form.chefspec.data)
                reachouts = form.reachouts.data
            if (cust):
                custpref = form.custpref.data

            # update with new info if necessary
            r = user.update(fname, lname, email, passwd, user_type, aptno,
                            street, city, state, zipcode, country, phoneno,
                            chefspec, reachouts, custpref)

            update_session(user)

            if (r == 0):
                flash(
                    'User details updated. Visit account page again to see new fields',
                    'success')
            else:
                flash('Update failed, check the submitted data for errors',
                      'danger')

            return render_template('account.html',
                                   form=form,
                                   chef=chef,
                                   cust=cust)
        else:
            flash('Update failed, check the submitted data for errors',
                  'danger')
            return render_template('account.html',
                                   form=form,
                                   chef=chef,
                                   cust=cust)

    elif (request.method == 'GET'):
        usertype = ""
        aptno, street, city, state, zipcode, countryid, phoneno = (None, ) * 7
        chefspec = None
        custpref = None
        reachouts = None
        if (chef and cust):
            usertype = "both"
            aptno = chef.address
            street = chef.street
            city = chef.city
            state = chef.state
            zipcode = chef.zipcode
            countryid = chef.countryid
            phoneno = chef.phone_number
            chefspec = chef.get_specialty()
            reachouts = chef.get_reachouts_str()
            custpref = cust.preference
        elif (chef):
            usertype = "chef"
            aptno = chef.address
            street = chef.street
            city = chef.city
            state = chef.state
            zipcode = chef.zipcode
            countryid = chef.countryid
            phoneno = chef.phone_number
            chefspec = chef.get_specialty()
            reachouts = chef.get_reachouts_str()
        elif (cust):
            usertype = "customer"
            aptno = cust.address
            street = cust.street
            city = cust.city
            state = cust.state
            zipcode = cust.zipcode
            countryid = cust.countryid
            phoneno = cust.phone_number
            custpref = cust.preference

        form.first_name.data = user.fname
        form.last_name.data = user.lname
        form.email_id.data = user.email
        form.password.data = None
        form.usertype.data = usertype

        form.apartment_no.data = aptno
        form.street.data = street
        form.city.data = city
        form.state.data = state
        form.zipcode.data = str(zipcode)
        form.country.data = countryid
        form.phone_number.data = str(phoneno)

        form.chefspec.data = chefspec.cuisineid if chefspec else -1
        form.reachouts.data = reachouts
        form.custpref.data = custpref

        if (not chef):
            del form.chefspec
            del form.reachouts
        if (not cust):
            del form.custpref

        return render_template('account.html', form=form, chef=chef, cust=cust)
    flash('Update failed', 'danger')
    return render_template('account.html', form=form, chef=chef, cust=cust)