本文整理汇总了Python中forms.ItemForm.validate方法的典型用法代码示例。如果您正苦于以下问题:Python ItemForm.validate方法的具体用法?Python ItemForm.validate怎么用?Python ItemForm.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.ItemForm
的用法示例。
在下文中一共展示了ItemForm.validate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: items_edit
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def items_edit(item_id):
form = ItemForm(request.form)
if request.method == 'POST' and form.validate():
if Item.query.filter(Item.name == form.name.data, Item.id != form.id.data).first():
data = {
'nav_urls': get_urls(),
'active_url': url_for('items_new')
}
duplicate = True
return render_template('items_edit.html', form=form, data=data, duplicate=duplicate)
else:
item = Item.query.filter(Item.id == item_id).first()
item.name = form.name.data
item.price = form.price.data
item.active = form.active.data
dbs.add(item)
dbs.commit()
return redirect(url_for('items'))
else:
data = {
'nav_urls': get_urls(),
'active_url': url_for('items_edit', item_id=item_id)
}
item = Item.query.filter(Item.id == item_id).first()
form.id.data = item.id
form.name.data = item.name
form.price.data = item.price
form.active.data = item.active
return render_template('items_edit.html', data=data, form=form)
示例2: update_item
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def update_item(id):
"""Updates the requested item."""
if needs_login():
return redirect(url_for('oauth2callback'))
form = ItemForm(request.form)
item = models.Item.query.filter_by(id=id).first()
if not form.validate():
return render_template('edit_item.html', form=form, item=item)
# retain the old image path before populate_obj overwrites it with form
# data
old_image = item.image
form.populate_obj(item)
item.image = old_image
if form.image is not None:
image_data = request.files['image'].read()
if len(image_data) > 0:
# generate a random file name and save it to the image folder
filename = ''.join(random.choice(
string.ascii_uppercase + string.digits) for x in xrange(16))
ext = os.path.splitext(request.files['image'].filename)[1]
open(app.config['IMAGE_DIR'] +
filename + ext, 'w').write(image_data)
item.image = filename + ext
# if the user updates the image, delete the old one
if os.path.isfile(app.config['IMAGE_DIR'] + old_image):
os.remove(app.config['IMAGE_DIR'] + old_image)
db.session.add(item)
db.session.commit()
return render_template('generic.html',
title="Update Completed",
description="%s has been updated." % item.name,
redirect_to_index=True)
示例3: add_item
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def add_item():
"""Saves the new item and redirects to confirmation page."""
if needs_login():
return redirect(url_for('oauth2callback'))
form = ItemForm(request.form)
if not form.validate():
return render_template('new_item.html', form=form)
image = ""
if form.image.data is not None:
image_data = request.files['image'].read()
if len(image_data) > 0:
# generate a random file name and save it to the image folder
filename = ''.join(random.choice(
string.ascii_uppercase + string.digits) for x in xrange(16))
ext = os.path.splitext(request.files['image'].filename)[1]
open(app.config['IMAGE_DIR'] +
filename + ext, 'w').write(image_data)
image = filename + ext
item = models.Item(name=form.name.data,
user=get_current_user(),
description=form.description.data,
category=form.category.data,
image=image)
db.session.add(item)
db.session.commit()
return render_template('generic.html',
title="Item Added",
description="%s has been added." % item.name,
redirect_to_index=True)
示例4: add_item
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def add_item(user_id):
user = db.session.query(User).filter_by(id=user_id).first()
tokens = map(lambda x: x.token , user.tokens)
check = check_auth_header(request.headers)
if not check[0]:
return check[1]
if not authenticate_user(tokens , request.headers['AuthToken']):
return unauthorized_message()
data = MultiDict(mapping=request.json)
inputs = ItemForm(data , csrf_enabled=False)
if not inputs.validate():
return bad_request_error(inputs.errors)
data = request.get_json()
name = data['name']
description = data['description']
thumbnail_url = data['thumbnail_url']
item_url = data['item_url']
item = Item(name, description, thumbnail_url, user_id, item_url)
db.session.add(item)
db.session.commit()
response = jsonify({'name':item.name , 'description':item.description, 'thumbnail_url':item.thumbnail_url})
response.status_code = 201
return response
示例5: UpdateItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def UpdateItem(message):
Before_request()
list_id = message["list_id"]
item_id = message["item_id"]
# Before checking for validation, we assume the item will not be
# added and that the list is not valid.
sendMessage = {"errors": "This is not a valid list."}
customMessage = 'update item fail'
room = None
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(item_name = message["item_name"], csrf_token = message["csrf_token"])
# We don't need the items checked state to validate the form
del form.check
if item is not None and form.validate():
item.item_name = form.item_name.data
db.session.commit()
sendMessage = {"itemName": item.item_name, "itemID": item.id}
customMessage = 'update item success'
room = list_id
else:
sendMessage = {"errors": form.item_name.errors}
emit(customMessage, sendMessage, room = room)
示例6: add
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def add():
global db
form = ItemForm(request.form)
if request.method == 'POST' and form.validate():
document = {}
for field, value in request.form.items():
document[field] = value
if 'title' in document and 'media_type' in document:
slug = common.slugify(document['title'])
document['slug'] = slug
db.save(document)
print "Document saved."
flash('Changes saved.')
return redirect(url_for('edit', slug=slug))
types = [item[0] for item in MEDIA_TYPES]
return render_template('form.html', title="Add item", form=form, types=types, document={})
示例7: CheckItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def CheckItem(message):
Before_request()
list_id = message["list_id"]
item_id = message["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()
# Before checking for validation, we assume the item will not be
# added and that the list is not valid.
sendMessage = {"errors": "This is not a valid list."}
customMessage = 'check item fail'
room = None
if query_list is not None or query_list_shared:
item = Item.query.filter_by(id = item_id).first();
check = True if message["check"] else False
form = ItemForm(check = check, csrf_token = message["csrf_token"])
# We don't need the item name to validate this request.
del form.item_name
if item is not None and form.validate():
item.check = form.check.data
db.session.commit()
sendMessage = { "itemID": item.id }
customMessage = 'check item success'
room = list_id
else:
sendMessage = { "errors" : item.check.errors }
emit(customMessage, sendMessage, room = room)
示例8: AddItem
# 需要导入模块: from forms import ItemForm [as 别名]
# 或者: from forms.ItemForm import validate [as 别名]
def AddItem(message):
Before_request()
# Before checking for validation, we assume the item will not be
# added and that the list is not valid.
sendMessage = {"errors": "This is not a valid list."}
customMessage = 'add item fail'
room = None
list_id = message["list_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:
form = ItemForm(item_name = message["item_name"], csrf_token = message["csrf_token"])
# We don't need the items checked state to validate the form
del form.check
if form.validate():
item_name = form.item_name.data
item = Item(item_name = item_name, list_id = list_id)
# add item entry
db.session.add(item)
db.session.commit()
# Set everything to a success state.
sendMessage = { "itemID": item.id, "itemName": item.item_name }
customMessage = 'add item success'
room = list_id
else:
sendMessage = { "errors": [form.item_name.errors, form.csrf_token.errors]}
emit(customMessage, sendMessage, room = room)