本文整理汇总了Python中app.models.amenity.Amenity.create方法的典型用法代码示例。如果您正苦于以下问题:Python Amenity.create方法的具体用法?Python Amenity.create怎么用?Python Amenity.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.amenity.Amenity
的用法示例。
在下文中一共展示了Amenity.create方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_amenities
# 需要导入模块: from app.models.amenity import Amenity [as 别名]
# 或者: from app.models.amenity.Amenity import create [as 别名]
def create_amenities():
data = request.form
check_amenity = Amenity.select(). where(Amenity.name == data['name'])
if check_amenity:
return {'code': 10003, 'msg': 'Name already exists'}, 409
amenity = Amenity.create(
name = data['name']
)
return {'code': 201, 'msg': 'Amenity created successfully'}, 201
示例2: handle_amenity
# 需要导入模块: from app.models.amenity import Amenity [as 别名]
# 或者: from app.models.amenity.Amenity import create [as 别名]
def handle_amenity():
'''Returns all amenities as JSON objects in an array with a GET request.
Adds an amenity with a POST request.
'''
if request.method == 'GET':
list = ListStyle().list(Amenity.select(), request)
return jsonify(list), 200
elif request.method == 'POST':
try:
Amenity.select().where(Amenity.name == request.form['name']).get()
return jsonify(code=10003, msg="Name already exists"), 409
except Amenity.DoesNotExist:
'''Check that all the required parameters are made in request.'''
required = set(["name"]) <= set(request.values.keys())
if required is False:
return jsonify(msg="Missing parameter."), 400
amenity = Amenity.create(name=request.form['name'])
return jsonify(amenity.to_dict()), 200
示例3: create_amenity
# 需要导入模块: from app.models.amenity import Amenity [as 别名]
# 或者: from app.models.amenity.Amenity import create [as 别名]
def create_amenity():
"""
Create a new amenity
Create a new amenity in the database.
---
tags:
- Amenity
parameters:
-
name: name
in: form
type: string
required: True
description: Name of the amenity
responses:
201:
description: Amenity was created
schema:
id: post_success
required:
- code
- id
- msg
properties:
code:
type: integer
description: Response code from the API
default: 201
id:
type: integer
description: ID of the newly created record
default: 1
msg:
type: string
description: Message about record creation
default: "created successfully"
400:
description: Issue with amenity request
409:
description: Amenity already exists
500:
description: The request was not able to be processed
"""
data = {}
if request.json:
data = request.json
else:
for key in request.form.keys():
for value in request.form.getlist(key):
data[key] = value
try:
''' Check if name for amenity was given '''
if not 'name' in data:
raise KeyError("'name'")
''' Check if name is a string '''
if not type_test(data['name'], 'string'):
raise TypeError("amenity 'name' must be a string value")
''' Check if amenity already exists '''
query = Amenity.select().where(Amenity.name == data['name'])
if query.exists():
raise ValueError('amenity already exists')
''' Create new amenity '''
new = Amenity.create(
name = data['name']
)
res = {}
res['code'] = 201
res['id'] = new.id
res['msg'] = "amenity was created successfully"
return res, 201
except KeyError as e:
res = {}
res['code'] = 40000
res['msg'] = 'missing parameters'
return res, 400
except TypeError as e:
res = {}
res['code'] = 400
res['msg'] = e.message
return res, 400
except ValueError as e:
res = {}
res['code'] = 10003
res['msg'] = e.message
return res, 409
except Exception as e:
abort(500)