本文整理汇总了Python中app.models.city.City.select方法的典型用法代码示例。如果您正苦于以下问题:Python City.select方法的具体用法?Python City.select怎么用?Python City.select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.city.City
的用法示例。
在下文中一共展示了City.select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def handle_city(state_id):
'''Returns all the cities in the state with the id passed as `state_id`
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((City
.select()
.where(City.state == state_id)),
request)
return jsonify(list), 200
elif request.method == 'POST':
try:
City.select().where((City.name == request.form['name']) &
(City.state == state_id)
).get()
return jsonify(code=10002, msg="City already exists in this " +
"state"), 409
except City.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
city = City.create(name=request.form['name'], state=state_id)
return jsonify(city.to_dict()), 200
示例2: cities
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def cities(state_id=None, city_id=None):
state = None
try:
if state_id != None:
state = State.get(State.id == int(state_id))
except:
state = None
if state == None:
return { 'code': 404, 'msg': "not found" }, 404
if request.method == "GET":
if city_id != None:
try:
city = City.get(City.id == int(city_id), City.state == state)
return city.to_dict()
except:
pass
return { 'code': 404, 'msg': "not found" }, 404
return { 'data': [city.to_dict() for city in City.select().where(City.state == state)] }, 200
elif request.method == "POST":
name = request.form.get('name')
if City.select().where(City.state == state).where(City.name == name).count() > 0:
return { 'code': 10002, 'msg': "City already exists in this state" }, 409
try:
new_city = City.create(name=name, state=state)
except IntegrityError:
return { 'code': 10002, 'msg': "City already exists in this state" }, 409
except Exception as e:
raise e
return new_city.to_dict(), 201
elif request.method == "DELETE":
if city_id != None:
city = None
try:
city = City.get(City.id == int(city_id), City.state == state)
except:
city = None
if city != None:
city.delete_instance()
return {}, 200
return { 'code': 404, 'msg': "not found" }, 404
示例3: handle_city_id
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def handle_city_id(state_id, city_id):
'''Select the city with the id from the database and store as the variable
`city` with a GET request method. Remove the city with this id from the
database with a DELETE request method.
Keyword arguments:
state_id: The state of the city from the database.
city_id: The id of the city from the database.
'''
try:
city = City.select().where((City.id == city_id) &
(City.state == state_id)
).get()
except City.DoesNotExist:
raise jsonify(msg="There is no city with this id in this state."), 400
if request.method == 'GET':
return jsonify(city.to_dict())
elif request.method == 'DELETE':
try:
city = City.delete().where((City.id == city_id) &
(City.state == state_id))
except City.DoesNotExist:
raise Exception("There is no city with this id, in this state.")
city.execute()
return jsonify(msg="City deleted successfully."), 200
示例4: get_cities
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def get_cities(state_id):
if request.method == 'GET':
cities = []
query = (City.select(State, City).join(State).where(State.id == state_id))
for city in query:
cities.append(city.to_hash())
return jsonify(cities)
示例5: city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def city(state_id):
"""Handle GET and POST requests to /states/<state_id>/cities route.
Return a list of all cities in state (according to database) in the case of
a GET request.
Create a new city record in the given state in the database in the case of
a POST request.
"""
# handle GET requests:
# --------------------------------------------------------------------------
if request.method == 'GET':
list = ListStyle.list(City.select().where(City.state == state_id), request)
return jsonify(list)
# handle POST requests:
# --------------------------------------------------------------------------
elif request.method == 'POST':
try:
record = City(name=request.form["name"], state=state_id)
record.save()
return jsonify(record.to_hash())
# return 409 if city with given name already exists
except IntegrityError:
return json_response(
add_status_=False,
status_=409,
code=10002,
msg="City already exists in this state"
)
示例6: test_create
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def test_create(self):
"""
Test proper creation (or non-creation) of city records upon POST
requests to API.
"""
# test creation of city with all parameters provided in POST request
POST_request1 = self.app.post('/states/1/cities', data=dict(
name='namestring'
))
now = datetime.now().strftime('%d/%m/%Y %H:%M')
self.assertEqual(City.get(City.id == 1).name, 'namestring')
self.assertEqual(City.get(City.id == 1).state.id, 1)
self.assertEqual(City.get(City.id == 1).created_at.strftime('%d/%m/%Y %H:%M'), now)
self.assertEqual(City.get(City.id == 1).updated_at.strftime('%d/%m/%Y %H:%M'), now)
# test creation of city in all cases of a parameter missing in POST request
POST_request2 = self.app.post('/states/1/cities', data=dict())
self.assertEqual(POST_request2.status[:3], '400')
# test that city ID for sole record in database is correct
self.assertEqual(City.select().get().id, 1)
# test that a post request with a duplicate name value is rejected
POST_request3 = self.app.post('/states/1/cities', data=dict(
name='namestring'
))
self.assertEqual(POST_request3.status[:3], '409')
self.assertEqual(json.loads(POST_request3.data), {'code': 10002, 'msg': 'City already exists in this state'})
示例7: get_cities
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def get_cities(id):
cities = []
query = City.select().join(State).where(State.id == id)
for i in query:
cities.append(i.to_hash())
return jsonify(cities)
示例8: list_cities
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def list_cities(state_id):
id_state = state_id
# returns a json with the cities associated to a state
if request.method == 'GET':
list = ListStyle.list(City.select()
.where(City.state == id_state), request)
return jsonify(list)
# creates a new city
elif request.method == 'POST':
# checks if the city already exist in the state
for city in City.select():
if str(city.state_id) == id_state and city.name == request.form['name']:
return make_response(jsonify({'code': '10001',
'msg': 'City already exists in this state'}), 409)
city_name = request.form['name']
new_city = City(name=city_name, state_id=id_state)
new_city.save()
return "New city saved %s\n" % (new_city.name)
示例9: create_new_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City 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
示例10: create_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def create_city(id):
data = request.form
city_check = City.select().join(State).where(State.id == id, City.name == data['name'])
if city_check:
return {'code': 10002, 'msg': 'City already exists in this state'},409
city = City(
name = data['name'],
state = id
)
city.save()
return {'code': 201, 'msg': 'City created successfully'}, 201
示例11: delete_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def delete_city(state_id, city_id):
"""
Delete the given city
Deletes the given city in the database.
---
tags:
- City
parameters:
-
in: path
name: state_id
type: string
required: True
description: ID of the state
-
in: path
name: city_id
type: string
required: True
description: ID of the city
responses:
200:
description: City deleted successfully
schema:
$ref: '#/definitions/delete_amenity_delete_delete_200'
404:
description: City was not found
500:
description: Request could not be processed
"""
try:
''' Check if state exists '''
query = State.select().where(State.id == state_id)
if not query.exists():
raise LookupError('state_id')
''' Check if city exists '''
query = City.select().where(City.id == city_id)
if not query.exists():
raise LookupError('city_id')
''' Delete the city from the given state '''
delete_city = City.delete().where(City.id == city_id, City.state == state_id)
delete_city.execute()
response = {}
response['code'] = 200
response['msg'] = "City account was deleted"
return response, 200
except LookupError as e:
abort(404)
except Exception as e:
abort(500)
示例12: cities
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def cities(state_id):
if request.method == 'GET':
try:
query = City.select().where(City.state == state_id)
return ListStyle.list(query, request), 200
except City.DoesNotExist:
return json_response(status_=404, msg="not found")
elif request.method == 'POST':
if "name" not in request.form:
return json_response(status_=400, msg="missing parameters", code=40000)
city_test = City.select().where(City.name == str(request.form["name"]), City.state == state_id)
if city_test.wrapped_count() > 0:
return json_response(status_=409, code=10002, msg="city already exists in this state")
city = City(name=str(request.form["name"]), state=str(state_id))
city.save()
return jsonify(city.to_dict()), 201
示例13: get_places_by_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City 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)
示例14: delete_city_by_id
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def delete_city_by_id(state_id, city_id):
"""
Delete a city
Deletes a city based on id.
---
tags:
- city
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:
cities = City.select().where(City.id == int(city_id))
city = None
for u in cities:
city = u
if city == None:
return error_msg(400, 400, "Error")
city.delete_instance()
except:
return error_msg(400, 400, "Error")
return error_msg(200, 200, "Success")
示例15: modify_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import select [as 别名]
def modify_city(state_id, city_id):
id = city_id
try:
if request.method == 'GET':
list = ListStyle.list(City.select()
.where(City.id == city_id
and City.state == state_id), request)
return jsonify(list)
except:
return "City with id %d does not exist" % (int(id))
if request.method == "DELETE":
id = city_id
try:
get_city = City.get(City.id == id)
get_city.delete_instance()
return "City with id %d was deleted\n" % (int(id))
except:
return "City with id %d does not exist\n" % (int(id))