本文整理汇总了Python中app.models.state.State.create方法的典型用法代码示例。如果您正苦于以下问题:Python State.create方法的具体用法?Python State.create怎么用?Python State.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.state.State
的用法示例。
在下文中一共展示了State.create方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import create [as 别名]
def handle_states():
'''Returns all the states from the database as JSON objects with a GET
request, or adds a new state to the database with a POST request. Refer to
exception rules of peewee `get()` method for additional explanation of
how the POST request is handled:
http://docs.peewee-orm.com/en/latest/peewee/api.html#SelectQuery.get
'''
if request.method == 'GET':
list = ListStyle().list(State.select(), request)
return jsonify(list), 200
elif request.method == 'POST':
params = request.values
'''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
try:
State.select().where(State.name == request.form['name']).get()
return jsonify(code=10001, msg="State already exists"), 409
except State.DoesNotExist:
state = State.create(name=request.form['name'])
return jsonify(state.to_dict()), 201
示例2: create_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import create [as 别名]
def create_state():
data = request.form
name_check = State.select().where(State.name == data['name'])
if name_check:
return {'code': 10001, 'msg': 'State already exists'}, 409
state = State.create(
name = data['name']
)
return {'code': 201,'msg': 'State was created successfully'}, 201
示例3: create_new_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import create [as 别名]
def create_new_state():
post_data = request.values
if 'name' not in post_data:
return {'code':400, 'msg':'bad request'}, 400
state_query = State.select().where(State.name == post_data['name'])
if state_query.exists():
out = {'code': 10001, 'msg': 'State already exists'}
return out, 409
try:
state_row = State.create(name=post_data['name'])
return state_row.to_hash()
except:
return {'code':500, 'msg':'database connection error'}, 500
示例4: states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import create [as 别名]
def states(state_id=None):
if request.method == "GET":
if state_id != None:
try:
state = State.get(State.id == int(state_id))
return state.to_dict()
except:
pass
return { 'code': 404, 'msg': "not found" }, 404
return { 'data': [state.to_dict() for state in State.select()] }, 200
elif request.method == "POST":
name = request.form.get('name')
try:
new_state = State.create(name=name)
except IntegrityError:
return { 'code': 10001, 'msg': "State already exists" }, 409
except Exception as e:
raise e
return new_state.to_dict(), 201
elif request.method == "DELETE":
if state_id != None:
state = None
try:
state = State.get(State.id == int(state_id))
except:
state = None
if state != None:
state.delete_instance()
return {}, 200
return { 'code': 404, 'msg': "not found" }, 404
示例5: create_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import create [as 别名]
def create_state():
"""
Create a new state
Create a new state in the database
---
tags:
- State
parameters:
-
name: name
in: form
type: string
required: True
description: Name of the state
responses:
201:
description: State was created
schema:
$ref: '#/definitions/create_amenity_post_post_success'
400:
description: Issue with state request
409:
description: State already exists
500:
description: The request was not able to be processed
"""
data = {}
for key in request.form.keys():
for value in request.form.getlist(key):
data[key] = value
try:
''' Check that name key is in data '''
if not 'name' in data:
raise KeyError('name')
''' Check that name key is not null '''
if not data['name']:
raise TypeError("'name' cannot be NULL")
''' Check that name key value is a string '''
if not type_test(data['name'], 'string'):
raise TypeError("'name' must be a string")
''' Check if state already exists '''
query = State.select().where(State.name == data['name'])
if query.exists():
raise ValueError('State already exists')
new = State.create(
name = data['name']
)
res = {}
res['code'] = 201
res['id'] = new.id
res['msg'] = "State was created successfully"
return res, 201
except TypeError as e:
response = {}
response['code'] = 400
response['msg'] = e.message
return response, 400
except ValueError as e:
response = {}
response['code'] = 10001
response['msg'] = e.message
return response, 409
except KeyError as e:
response = {}
response['code'] = 40000
response['msg'] = 'Missing parameters'
return response, 400
except Exception as e:
print e.message
abort(500)