def customer_add():
    form = CustomerForm(request.form)
    if request.method == 'POST' and form.validate():
        new_user = Customer(name=form.name.data,
                            email=form.email.data,
                            tel=form.tel.data)
        db.session.add(new_user)
        db.session.commit()
        flash('Başarılı bir şekilde müşteri eklediniz', 'success')
        return redirect(url_for('customer_add'))
    else:
        return render_template('customer_add.html', form=form)
Example #2
0
def edit(id):
    qry = db.session.query(customer).filter(customer.id == id)
    customer_info = qry.first()

    if customer_info:
        form = CustomerForm(formdata=request.form, obj=customer_info)
        if request.method == 'POST' and form.validate():
            # save edits
            save_changes(customer_info, form)
            flash('Cliente registrado!')
            return redirect('/')
        return render_template('edit_customer.html', form=form)
    else:
        return 'Error loading #{id}'.format(id=id)
Example #3
0
def new_customer():
    """
    Add a new customer
    """
    form = CustomerForm(request.form)

    if request.method == 'POST' and form.validate():
        # save the album
        new_customer = customer()
        save_changes(new_customer, form, new=True)
        flash('Cliente Registrado!')
        return redirect('/')

    return render_template('new_customer.html', form=form)
Example #4
0
def customer(id):
    customer = Customer.query.get(id)
    if request.method == 'DELETE':
        db.session.delete(customer)
        db.session.commit()
        return redirect(url_for('customers'))
    else:
        form = CustomerForm(request.form)
        if form.validate():
            form.populate_obj(customer)
            db.session.add(customer)
            db.session.commit()
            return redirect(url_for('customers'))
        else:
            return render_template('edit_customer.html',
                                   form=form,
                                   customer=customer)
Example #5
0
def add_customers():
    form = CustomerForm(request.form)
    error = None
    if form.validate():
        customer = Customer(form.first_name.data, form.last_name.data,
                            form.email.data, form.age.data)
        flash('You sucessfully added a new customer!')
        session.add(customer)
        session.commit()
        return redirect(url_for('customers'))
    else:
        error = "Fields are not filled correctly!"
        return render_template('customer_form.html',
                               form=form,
                               method="POST",
                               action="/add",
                               error=error,
                               submit_text="Save")
Example #6
0
def delete(id):
    """
    Delete the item in the database that matches the specified
    id in the URL
    """
    qry = db.session.query(customer).filter(customer.id == id)
    customer_info = qry.first()

    if customer:
        form = CustomerForm(formdata=request.form, obj=customer_info)
        if request.method == 'POST' and form.validate():
            # delete the item from the database
            db.session.delete(customer_info)
            db.session.commit()

            flash('Cliente eliminado!')
            return redirect('/')
        return render_template('delete_customer.html', form=form)
    else:
        return 'Error deleting #{id}'.format(id=id)
Example #7
0
def edit_customer(c_id):
    form = CustomerForm(request.form)
    if form.validate():
        cust_edited = Customer(form.first_name.data, form.last_name.data,
                               form.email.data, form.age.data)
        flash('You sucessfully edited customer')
        customer_db = session.query(Customer).get(c_id)
        customer_db.first_name = cust_edited.first_name
        customer_db.last_name = cust_edited.last_name
        customer_db.age = cust_edited.age
        customer_db.email = cust_edited.email
        session.commit()
        return redirect(url_for('customers'))
    else:
        error = "Fields are not filled correctly!"
    return render_template('customer_form.html',
                           form=form,
                           method="POST",
                           action="/customer/%s/edit" % c_id,
                           submit_text="Save changes")
Example #8
0
def customers():
    if request.method == 'POST':
        form = CustomerForm(request.form)
        customer = Customer()

        if form.validate():
            form.populate_obj(customer)
            db.session.add(customer)
            db.session.commit()
            return redirect(url_for('customers'))
        else:
            return render_template('new_customer.html', form=form)
    else:
        page = int(request.args.get('page', 1))
        page_size = int(request.args.get('page_size', 10))
        filter = request.args.get('filter', None)
        order_by = request.args.get('order_by', None)
        order = request.args.get('order', None)
        order = order if order in ['ASC', 'DESC'] else 'ASC'

        q = Customer.query
        if filter:
            q = q.filter(getattr(Customer, 'name').like('%{}%'.format(filter)))
        count = q.count()

        if order_by:
            q = q.order_by('{} {}'.format(order_by, order))

        q = q.offset((page - 1) * page_size)
        customers = q.limit(page_size)
        total_pages = (count // page_size) + (0 if
                                              (count % page_size) == 0 else 1)
    return render_template('customers.html',
                           customers=customers,
                           page=page,
                           page_size=page_size,
                           count=count,
                           total_pages=total_pages)