def edit_vendor(id): """ Edit a vendor """ check_admin() add_vendor = False vendor = Vendor.query.get_or_404(id) form = VendorForm(obj=vendor) if form.validate_on_submit(): vendor.name = form.name.data vendor.description = form.description.data db.session.commit() flash('You have successfully edited the vendor.') # redirect to the vendors page return redirect(url_for('admin.list_vendors')) form.description.data = vendor.description form.name.data = vendor.name return render_template('admin/vendors/vendor.html', action="Edit", add_vendor=add_vendor, form=form, vendor=vendor, title="Edit Vendor")
def add_vendor(): """ Add a vendor to the database """ check_admin() add_vendor = True form = VendorForm() if form.validate_on_submit(): vendor = Vendor(name=form.name.data, description=form.description.data) try: # add vendor to the database db.session.add(vendor) db.session.commit() flash('You have successfully added a new vendor.') except: # in case vendor name already exists flash('Error: vendor name already exists.') # redirect to vendors page return redirect(url_for('admin.list_vendors')) # load vendor template return render_template('admin/vendors/vendor.html', action="Add", add_vendor=add_vendor, form=form, title="Add Vendor")
def addvendor(): form = VendorForm() if request.method == "GET": return render_template('addvendor.html', form=form) if request.method == "POST": if form.validate() == True: cursor = mysql.connection.cursor() query = "INSERT INTO vendor (vendor_name, vendor_phone_number, street, city, state, postal_code) VALUES (%s, %s, %s, %s, %s, %s)" variables = [form.vendor_name.data, form.vendor_phone_number.data, form.street.data, form.city.data, form.state.data, form.postal_code.data] cursor.execute(query, variables) mysql.connection.commit() return redirect(url_for("repairs", vendor_name=session["vendor"]["vendor_name"])) else: return render_template('addvendor.html', form=form)