本文整理汇总了Python中app.models.state.State.select方法的典型用法代码示例。如果您正苦于以下问题:Python State.select方法的具体用法?Python State.select怎么用?Python State.select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.state.State
的用法示例。
在下文中一共展示了State.select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [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: get_places_by_city
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def get_places_by_city(state_id, city_id):
"""
Get all places
List all places in the given city in the database.
---
tags:
- Place
parameters:
-
name: state_id
in: path
type: integer
required: True
description: ID of the state
-
name: city_id
in: path
type: integer
required: True
description: ID of the city
responses:
200:
description: List of all places
schema:
$ref: '#/definitions/get_places_get_Places'
"""
try:
''' Check if the state_id exists '''
query = State.select().where(State.id == state_id)
if not query.exists():
raise LookupError('state_id')
''' Check if the city_id exists '''
query = City.select().where(City.id == city_id)
if not query.exists():
raise LookupError('city_id')
''' Check if the city_id is associated to the state_id '''
city = City.get(City.id == city_id)
query = State.select().where(State.id == city.state, State.id == state_id)
if not query.exists():
raise LookupError('city_id, state_id')
''' Return all places in the given city '''
data = Place.select().where(Place.city == city.id)
return ListStyle.list(data, request), 200
except LookupError as e:
abort(404)
except Exception as error:
abort(500)
示例3: states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def states():
"""Handle GET and POST requests to /states route.
Return a list of all states in the database in the case of a GET request.
Create a new state in the database in the case of a POST request
"""
# handle GET requests:
# --------------------------------------------------------------------------
if request.method == 'GET':
list = ListStyle.list(State.select(), request)
return jsonify(list)
# handle POST requests:
# --------------------------------------------------------------------------
elif request.method == 'POST':
try:
record = State(name=request.form["name"])
record.save()
return jsonify(record.to_hash())
# return 409 state with given name already exists
except IntegrityError:
return json_response(
add_status_=False,
status_=409,
code=10001,
msg="State already exists"
)
示例4: handle_state_id
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def handle_state_id(state_id):
'''Select the state with the id from the database and store as the variable
`state` with a GET request method. Update the data of the particular state
with a PUT request method. This will take the parameters passed and update
only those values. Remove the state with this id from the database with
a DELETE request method.
Keyword arguments:
state_id: The id of the state from the database.
'''
try:
state = State.select().where(State.id == state_id).get()
except State.DoesNotExist:
return jsonify(msg="There is no state with this id."), 404
if request.method == 'GET':
return jsonify(state.to_dict()), 200
elif request.method == 'DELETE':
try:
state = State.delete().where(State.id == state_id)
except State.DoesNotExist:
raise Exception("There is no state with this id.")
state.execute()
return jsonify(msg="State deleted successfully."), 200
示例5: get_states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def get_states():
if request.method == 'GET':
states = []
query = State.select()
for state in query:
states.append(state.to_hash())
return jsonify(states)
示例6: get_states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def get_states():
"""
Get all states
List all states in the database.
---
tags:
- State
responses:
200:
description: List of all states
schema:
id: States
required:
- data
- paging
properties:
data:
type: array
description: states array
items:
$ref: '#/definitions/get_state_get_State'
paging:
description: pagination
schema:
$ref: '#/definitions/get_amenities_get_Paging'
"""
try:
''' Returns a list of states in list named result '''
data = State.select()
return ListStyle.list(data, request), 200
except Exception as e:
abort(500)
示例7: del_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def del_state(id):
id_check = State.select().where(State.id == id)
if not id_check:
return {'code': 404, 'msg': 'State not found'}, 404
item = State.delete().where(State.id == id)
item.execute()
return {'code': 200, 'msg': 'Deleted successfully'}, 200
示例8: get_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def get_state(state_id):
"""
Get the given state
Returns the given state in the database.
---
tags:
- State
parameters:
-
in: path
name: state_id
type: integer
required: True
description: ID of the state
responses:
200:
description: State returned successfully
schema:
id: State
required:
- name
- id
- created_at
- updated_at
properties:
name:
type: string
description: name of the given state
default: None
id:
type: integer
description: id of the state
default: 1
created_at:
type: datetime string
description: date and time the state was created in the database
default: '2016-08-11 20:30:38'
updated_at:
type: datetime string
description: date and time the state was updated in the database
default: '2016-08-11 20:30:38'
404:
description: State was not found
500:
description: Request could not be processed
"""
try:
''' Check that state_id exists '''
query = State.select().where(State.id == state_id)
if not query.exists():
raise LookupError('state_id')
state = State.get(State.id == state_id)
return state.to_dict(), 200
except LookupError as e:
abort(404)
except Exception as e:
abort(500)
示例9: state_places
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def state_places(state_id):
state = State.select().where(State.id == state_id)
if state.wrapped_count() < 1:
return json_response(status_=404, code=10002, msg="state not found")
query = Place.select().join(City).join(State).where(State.id == state_id)
return ListStyle.list(query, request)
示例10: create_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [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
示例11: state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def state(number):
if request.method == 'GET':
query = State.get(State.id == number)
return query.to_hash()
else:
try:
query = State.select().where(State.id == number).get()
except:
return {'code':404, 'msg':'state not found'}, 404
out_json = query.to_hash()
query.delete_instance()
return out_json
示例12: create_new_city
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def create_new_city(state_id):
post_data = request.values
if 'name' in post_data:
city_query = City.select().where(City.name == post_data['name'])
state_query = State.select().where(State.id == state_id).get()
if city_query.exists():
out = {'code': 10002, 'msg': 'City already exists in this state'}
return out, 409
city_row = City.create(state=state_query, name=post_data['name'])
return city_row.to_hash()
else:
return {"code":404, "msg":"not found"}, 404
示例13: create_new_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [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
示例14: list_of_states
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def list_of_states():
if request.method == 'GET':
list = ListStyle.list(State.select(), request)
return jsonify(list)
if request.method == 'POST':
# name_state = request.form['name']
try:
new_state = State(name=request.form['name'])
# saving the changes
new_state.save()
# returning the new information in hash form
return "New State entered! -> %s\n" % (new_state.name)
except:
return make_response(jsonify({'code': 10000,
'msg': 'State already exist'}), 409)
示例15: delete_one_state
# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import select [as 别名]
def delete_one_state(state_id):
"""
Delete an state
Deletes an state based on id.
---
tags:
- state
responses:
200:
description: Success message
schema:
id: success_message
properties:
status:
type: number
description: status code
default: 200
msg:
type: string
description: Status message
default: 'Success'
400:
description: Error message
schema:
id: error_message
properties:
status:
type: number
description: status code
default: 400
msg:
type: string
description: Status message
default: 'Error'
"""
try:
states = State.select().where(State.id == int(state_id))
state = None
for u in states:
state = u
if state == None:
return error_msg(400, 400, "Error")
state.delete_instance()
except:
return error_msg(400, 400, "Error")
return error_msg(200, 200, "Success")