def add_promotion(product_id): form = PromotionForm() product = Product.query.filter_by(id=product_id).first() current_promotion = Promotion.query.filter_by(product_id=product.id).first() if product is None: abort(404) if form.validate_on_submit(): discount = float(form.discount.data) if discount > product.price: flash('Discount price cannot be greater than list price') elif discount <= 0: flash('Discount price must be at least $0.01') else: if current_promotion is None: current_promotion = Promotion() current_promotion.product_id = product_id current_promotion.discount = discount db.session.add(current_promotion) else: current_promotion.discount = discount db.session.commit() flash('Promotion updated') return redirect(url_for('promotions')) flash_form_errors(form) return render_template('add_promotion.html', title='Promotion for %s - %s' % (product.manufacturer, product.name), product=product, current_promotion=current_promotion, form=form)
def apply_payment(order_id): order = Order.query.filter_by(id=order_id).first() if order is None: abort(404) # Make sure we're applying a payment for one of the manager's accounts if order.sold_by.manager != current_user.employee: abort(404) # Make sure this order has not already been paid if order.balance <= 0: flash('Payment not applied. This order has a zero balance') return redirect(url_for('orders')) # Record payment form = PaymentForm() if form.validate_on_submit(): if form.amount.data > order.balance: flash('Payment amount exceeds outstanding balance') else: payment = Payment(order_id=order_id, amount=form.amount.data, timestamp=datetime.datetime.now()) db.session.add(payment) db.session.commit() flash('Payment added') return redirect(url_for('orders')) flash_form_errors(form) return render_template('add_payment.html', title='Make Payment', form=form)
def new_valid_signup(): form = NewValidSignupForm() if form.validate_on_submit(): if auth_manager.add_approved_signup(form): flash_success() else: flash_form_errors(form) return redirect("/")
def signup(): form = SignUpForm() if form.validate_on_submit(): if auth_manager.signup(form): flash_success() else: flash_error("Oops! Something went wrong.") else: flash_form_errors(form) return redirect("/")
def update_jumbotron(): form = NewPicForm() if form.validate_on_submit: jumbotron_pic = Picture(src=form.src.data, description=form.description.data, is_jumbotron=True) db.session.add(jumbotron_pic) db.session.commit() else: flash_form_errors(form) return redirect("/")
def add_picture(): form = NewPicForm() if form.validate_on_submit(): pic = Picture(src=form.src.data, description=form.description.data, is_jumbotron=False) db.session.add(pic) db.session.commit() else: flash_form_errors(form) flash("error!", "error") return redirect("/")
def add_product(): form = ProductForm() if form.validate_on_submit(): product = Product(manufacturer=form.manufacturer.data, name=form.name.data, price=form.price.data, quantity=form.quantity.data, active=True) db.session.add(product) db.session.commit() flash('Product added') return redirect(url_for('products')) flash_form_errors(form) return render_template('add_product.html', title='Add Product', form=form)
def reorder_product(product_id): product = Product.query.filter_by(id=product_id).filter_by(active=True).first() if product is None: abort(404) form = ReorderProductForm(obj=product, exclude='active') if form.validate_on_submit(): new_quantity = form.quantity.data if new_quantity < product.quantity: flash('New quantity must be greater than current quantity') else: product.quantity = new_quantity db.session.commit() flash('Product quantity updated') return redirect(url_for('products')) flash_form_errors(form) return render_template('reorder_product.html', title='Reorder %s' % (product.name), form=form, product=product)
def add_client(): form = AddClientForm() all_reports = flatten_hierarchy(current_user.employee, lambda e: [e]) salesperson_ids = [(s.employee_id, s.username) for s in all_reports if s.title == 'Salesperson'] form.salesperson_id.choices = salesperson_ids if form.validate_on_submit(): cli = Client() cli.username = form.username.data cli.password_hash = unicode(bcrypt.generate_password_hash(form.password1.data)) cli.active = True cli.is_employee = False cli.company = form.company.data cli.salesperson_id = form.salesperson_id.data db.session.add(cli) db.session.commit() flash('Client added successfully') return redirect(url_for('clients')) flash_form_errors(form) return render_template('add_client.html', title='Add Client', form=form)
def add_employee(): form = AddEmployeeForm() title = 'Add Employee' if current_user.employee.title == 'Manager': form.managed_by.choices = [(current_user.employee.employee_id, current_user.employee.username)] form.title.choices = [('Salesperson', 'Salesperson')] else: managedBy = [(e.employee_id, e.username) for e in Employee.query.filter(Employee.title != 'Salesperson').all()] form.managed_by.choices = managedBy # Send user back to previous page if form errors exist if form.validate_on_submit(): # Validate form data manager = Employee.query.filter_by(employee_id=form.managed_by.data).first() if form.password1.data != form.password2.data: flash('Passwords do not match') elif form.title.data == 'Director' and manager.title is not None: flash('Directors cannot have a manager') elif form.title.data == 'Manager' and manager.title != 'Director': flash('A Manager must be managed by a Director') elif form.title.data == 'Salesperson' and manager.title != 'Manager': flash('A Salesperson must be managed by a Manager') else: user = Employee() user.username = form.username.data user.password_hash = unicode(bcrypt.generate_password_hash(form.password1.data)) user.active = True user.is_employee = True user.managed_by = form.managed_by.data user.title = form.title.data user.commission = form.commission.data user.max_discount = form.max_discount.data db.session.add(user) db.session.commit() flash('Employee added successfully') return redirect('/employees/') flash_form_errors(form) return render_template('add_employee.html', title=title, form=form)
# Make sure we don't submit an empty order if not errors and item_count == 0: errors['Quantity'].append('At least one item must be selected') # Attempt to commit changes if errors: db.session.rollback() else: db.session.commit() flash('Order placed') except (Exception), err: errors['Database'].append(err) db.session.rollback() flash_errors(errors) flash_form_errors(form) return render_template('add_order.html', title='Place New Order', client=client, products=products, form=form) @login_required @app.route('/orders/export/<int:order_id>/') def export_order(order_id): if current_user.is_employee and current_user.employee.title != 'Salesperson': abort(404) elif current_user.is_employee: order = Order.query.filter(Order.id==order_id, Order.salesperson==current_user.employee.employee_id).first() else: order = Order.query.filter(Order.id==order_id, Order.client==current_user.client.client_id).first()