本文整理汇总了Python中forms.ItemForm.validate_on_submit方法的典型用法代码示例。如果您正苦于以下问题:Python ItemForm.validate_on_submit方法的具体用法?Python ItemForm.validate_on_submit怎么用?Python ItemForm.validate_on_submit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.ItemForm
的用法示例。
在下文中一共展示了ItemForm.validate_on_submit方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: item_store
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def item_store(category_name):
"""Store new item in DB"""
if 'username' not in login_session:
return redirect(url_for('login'))
category = Category.query.filter(Category.name == category_name).first()
if category.author_id != login_session['user_id']:
return render_template('401.html', name='item')
form = ItemForm()
form.category_id.choices = [(c.id, c.name)
for c in Category.query.order_by('name')]
form.category_id.data = category.id
if form.validate_on_submit():
try:
new_item = Item(form.name.data,
form.description.data,
category.id,
login_session['user_id'])
if 'image' in request.files and request.files['image']:
filename = images.save(request.files['image'])
new_item.image_name = filename
db_session.add(new_item)
db_session.commit()
return redirect(url_for('item_view',
category_name=category_name,
item_name=form.name.data))
except IntegrityError:
db_session.rollback()
flash("Item name must be unique value")
except Exception, e:
db_session.rollback()
print e
flash("Database error encountered")
示例2: newItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def newItem():
"""
newItem: form to add a new item
Args:
none
Returns:
return add a item and redirect to home or show form to add a new item
"""
if 'username' not in login_session:
return redirect('/login')
try:
form = ItemForm()
form.category_id.choices = [(0, 'Select')]
form.category_id.choices += [(category.id, category.name) for category
in session.query(Categories).
order_by('name')]
print form.errors
if form.validate_on_submit():
title = form.title.data
category_id = form.category_id.data
description = form.description.data
if (form.title.data is not None and
form.category_id.data is not None and
form.category_id.data is not '0'):
newItem = Items(title=title,
description=description,
category_id=category_id,
user_id=login_session['user_id'])
session.add(newItem)
session.commit()
flash("New Item Created")
else:
flash("Missing required information for Add")
else:
return render_template('newitem.html', form=form,
login_session=login_session)
except:
flash('Error')
return redirect(url_for('showHomepage'))
示例3: editItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def editItem(item_id):
itemForm = ItemForm()
item = Item.query.get(item_id)
slist = Slist.query.get(item.slist_id)
if itemForm.validate_on_submit():
item.shop = itemForm.shop.data
item.qnty=itemForm.quantity.data
item.price=itemForm.price.data
item.notes=itemForm.notes.data
coupon = itemForm.coupon.data
if (coupon == u'нет'):
coupon = None
item.coupon=coupon
db.session.commit()
return redirect(url_for('shopapp.show_list',slist=slist.name))
else:
return redirect(url_for('shopapp.shop_main'))
示例4: newItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def newItem():
itemForm = ItemForm()
if itemForm.validate_on_submit():
product=Product.query.filter_by(name=itemForm.product_id.data).first()
slist = Slist.query.filter_by(name=itemForm.slist_id.data).first()
shop=itemForm.shop.data
qnty=itemForm.quantity.data
price=itemForm.price.data
notes=itemForm.notes.data
coupon = itemForm.coupon.data
if (coupon == u'нет'):
coupon = None
item = Item(product=product, shop = shop, qnty=qnty, price = price, chk = False, note=notes, slist=slist, coupon=coupon)
db.session.add(item)
db.session.commit()
return redirect(url_for('shopapp.show_list',slist=slist.name))
else:
return redirect(url_for('shopapp.shop_main'))
示例5: items
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def items():
try:
log.info('Start reading from DB')
i_form = ItemForm()
s_form = SearchForm()
item_list = db.session.query(Item).all()
if i_form.validate_on_submit():
new_item = Item(i_form.sku.data, i_form.title.data)
db.session.add(new_item)
db.session.commit()
i_form.sku.data = ''
i_form.title.data = ''
item_list = db.session.query(Item).all()
return redirect(url_for('items'))
return render_template('items.html', i_form=i_form, s_form=s_form, item_list=item_list)
except:
_, ex, _ = sys.exc_info()
log.error(ex.message)
return render_template('items.html', i_form=i_form, s_form=s_form, item_list=item_list)
示例6: addItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def addItem(name):
"""Add an item to a named category or render a form.
Args: A category name
Returns: Add an item or render a form.
"""
# Store named category and the logged in user
category = Category.query.filter_by(name=name).first_or_404()
user = Users.query.filter_by(id=login_session["users_id"]).first_or_404()
# Verify that the logged in user is creator or admin
if category.users_id != login_session["users_id"] and not user.admin:
flash(" You are not authorized add items to that category.")
return redirect(url_for("category.showCategory", name=name))
# Initiate the form.
form = ItemForm()
# On POST of a valid form, add the new item.
if form.validate_on_submit():
item = Item(
form.name.data,
form.description.data,
form.amazon_asin.data,
form.picture.data,
category.id,
login_session["users_id"],
)
# Check if there is a amazon url, and if so extract asin
if form.amazon_url.data is not None:
asin = re.search("[A-Z0-9]{10}", form.amazon_url.data)
if asin:
item.amazon_asin = asin.group(0)
db.session.add(item)
db.session.commit()
flash("New Item %s Successfully Created" % item.name)
# Log new item
current_app.logger.info("New Item %s Created on %s" % (item.name, str(item.dateCreated)))
return redirect(url_for("category.showHome"))
else:
return render_template("newItem.html", form=form, category=category)
示例7: item_update
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def item_update(category_name, item_name):
"""Update item in DB"""
if 'username' not in login_session:
return redirect(url_for('login'))
item = Item.query.\
filter(Item.name == item_name).\
filter(Item.category.has(name=category_name)).\
first()
if item.author_id != login_session['user_id']:
return render_template('401.html', name='item')
form = ItemForm()
form.category_id.choices = [(c.id, c.name)
for c in Category.query.order_by('name')]
if form.validate_on_submit():
try:
category = Category.query.\
filter(Category.id == form.category_id.data).\
first()
item.name = form.name.data
item.description = form.description.data
item.category_id = form.category_id.data
if 'image' in request.files and request.files['image']:
filename = images.save(request.files['image'])
item.image_name = filename
db_session.add(item)
db_session.commit()
return redirect(url_for('item_view',
category_name=category.name,
item_name=form.name.data))
except IntegrityError:
db_session.rollback()
flash("Item name must be unique value")
except Exception:
db_session.rollback()
flash("Database error encountered")
flash_errors(form)
return redirect(url_for('item_edit',
category_name=category_name,
item_name=form.name.data))
示例8: DeleteItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def DeleteItem(list_id, item_id):
query_list = g.user.lists.filter_by(id = list_id).first()
query_list_shared = g.user.shared_lists.filter_by(id = list_id).first()
if query_list is not None or query_list_shared is not None:
item = Item.query.filter_by(id = item_id).first()
form = ItemForm()
# We don't need the item name to validate this request.
del form.item_name
# We don't need the items checked state to validate the form
del form.check
if item is not None and form.validate_on_submit():
db.session.delete(item)
db.session.commit()
return jsonify({"status": "success"})
return jsonify({"status": "failed", "errors": "Did not delete item."})
return jsonify({"status": "failed", "errors": "This is not a valid list."})
示例9: editItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def editItem(name):
"""Edit an item for a named item or render a form.
Args: An item name
Returns: Edit an item or render a form.
"""
# Store named item and the logged in user
item = Item.query.filter_by(name=name).first_or_404()
user = Users.query.filter_by(
id=login_session['users_id']).first_or_404()
# Verify that the logged in user is creator or admin
if item.users_id != login_session['users_id'] and not user.admin:
flash(' You are not authorized to make that edit.')
category = session.query(Category).filter_by(id=item.category_id)
return redirect(url_for('category.showCategory', name=category.name))
# Initiate the form.
form = ItemForm(obj=item)
# On POST of a valid form, edit the item.
if form.validate_on_submit():
item.name = form.name.data
item.description = form.description.data
item.picture = form.picture.data
# Check if there is a amazon url, and if so extract asin
if form.amazon_url.data is not None:
asin = re.search("[A-Z0-9]{10}", form.amazon_url.data)
if asin:
item.amazon_asin = asin.group(0)
else:
item.amazon_asin = form.amazon_asin.data
db.session.add(item)
db.session.commit()
flash(' Item %s Successfully Edited' % item.name)
category = Category.query.filter_by(id=item.category_id).first_or_404()
return redirect(url_for('category.showCategory', name=category.name))
else:
return render_template('editItem.html', form=form, item=item)
示例10: editItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate_on_submit [as 别名]
def editItem(category_name, item_title):
"""
editItem: form to edit a item
Args:
category_name (data type: str): category name to filter result
item_title (data type: str): item name to filter result
Returns:
return update a item and redirect to home or show form to edit a item
"""
if 'username' not in login_session:
return redirect(url_for('showLogin'))
try:
categories = session.query(Categories).order_by('name')
category = session.query(Categories)\
.filter_by(name=category_name).one()
if category is None:
flash('Error unable to retrive category')
return redirect(url_for('showHomepage'))
except Exception as error:
output = '<script>function myFunction(){'
output += 'alert("caught this error-cat: %s");}</script>'
output += '<body onload="myFunction()">' % (repr(error))
# return output
flash('Error cat')
return redirect(url_for('showHomepage'))
try:
item = session.query(Items).filter_by(title=item_title,
category_id=category.id).one()
if item is None:
flash('Error unable to retrive item')
return redirect(url_for('showHomepage'))
item = session.query(Items).filter_by(id=item.id).one()
except Exception as error:
output = '<script>function myFunction(){'
output += 'alert("caught this error-item: %s");}</script>'
output += '<body onload="myFunction()">' % (repr(error))
# return output
flash('Error item')
return redirect(url_for('showHomepage'))
try:
creator = getUserInfo(item.user_id)
form = ItemForm(obj=item)
form.category_id.choices = [(0, 'Select')]
form.category_id.choices += [(cat.id, cat.name) for
cat in categories]
print form.errors
"""
Validate form on submit
Gather user inputs and update the database
"""
if form.validate_on_submit():
form.populate_obj(item)
item.title = form.title.data
item.description = form.description.data
item.category_id = form.category_id.data
item.user_id = creator.id
session.add(item)
session.commit()
flash("Successfully updated item")
return redirect(url_for('showHomepage'))
return render_template('edititem.html',
form=form,
categories=categories,
category=category,
creator=creator,
login_session=login_session)
except Exception as error:
output = '<script>function myFunction(){'
output += 'alert("caught this error-2: %s");}</script>'
output += '<body onload="myFunction()">' % (repr(error))
# return output
flash('Error 2')
return redirect(url_for('showHomepage'))