本文整理汇总了Python中app.models.city.City.get方法的典型用法代码示例。如果您正苦于以下问题:Python City.get方法的具体用法?Python City.get怎么用?Python City.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.city.City
的用法示例。
在下文中一共展示了City.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [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'})
示例2: test_get
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def test_get(self):
"""
Test proper representation of a city record upon GET requests via city
ID to API
"""
# set-up for tests
# ----------------------------------------------------------------------
# create city record in city table; should have ID 1
city_record = self.createCityViaPeewee()
# test for handling of GET request for user record by user id which
# exists
# ----------------------------------------------------------------------
# make GET request for record in table
GET_request1 = self.app.get('/states/1/cities/1')
GET_data = json.loads(GET_request1.data)
# test that status of response is 200
self.assertEqual(GET_request1.status[:3], '200')
# test that values of response correctly reflect record in database
self.assertEqual(city_record.id, GET_data['id'])
self.assertEqual(city_record.created_at.strftime('%d/%m/%Y %H:%M'), GET_data['created_at'][:-3])
self.assertEqual(city_record.updated_at.strftime('%d/%m/%Y %H:%M'), GET_data['updated_at'][:-3])
self.assertEqual(City.get(City.id == 1).name, GET_data['name'])
self.assertEqual(City.get(City.id == 1).state.id, GET_data['state_id'])
# test for handling of GET request for city record by city id which
# does not exist
# ----------------------------------------------------------------------
GET_request2 = self.app.get('/states/1/cities/1000')
self.assertEqual(GET_request2.status[:3], '404')
示例3: cities
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [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
示例4: city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def city(state_id, city_id):
if request.method == 'GET':
try:
query = City.get(City.id == city_id, City.state == state_id)
except:
return {"code":404, "msg":"city not found"}, 404
return query.to_hash()
else:
try:
query = City.get(City.id == city_id, City.state == state_id)
except:
return {"code":404, "msg":"city not found"}, 404
out_dict = query.to_hash()
query.delete_instance()
return out_dict
示例5: city_id
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def city_id(state_id, city_id):
"""Handle GET and DELETE requests to /states/<state_id>/cities/<city_id>.
Return a hash of the appropriate record in the case of a GET request.
Delete appropriate record in case of DELETE request.
"""
# check whether resource exists:
# --------------------------------------------------------------------------
try:
record = City.get(City.id == city_id)
# return 404 not found if it does not
except City.DoesNotExist:
return json_response(
add_status_=False,
status_=404,
code=404,
msg="not found"
)
# if exception does not arise:
# --------------------------------------------------------------------------
# handle GET requests
if request.method == 'GET':
return jsonify(record.to_hash())
# handle DELETE requests
elif request.method == "DELETE":
record.delete_instance()
record.save()
return 'deleted city\n'
示例6: create_place_by_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def create_place_by_city(state_id, city_id):
post_data = request.values
keys=["name", "description", "latitude", "longitude", "owner_id"]
for key in keys:
if key not in post_data:
return {"code":400, "msg":"bad request, incorrect parameters"}
try:
city = City.get(City.id == city_id, City.state == state_id)
except City.DoesNotExist:
return {"code":400, "msg":"bad request, city or state does not exist"}, 400
place_dictionary = place_dict(
post_data['name'],
post_data['description'],
post_data.get('number_rooms'),
post_data.get('number_bathrooms'),
post_data.get('max_guest'),
post_data.get('price_by_night'),
post_data['latitude'],
post_data['longitude'],
post_data['owner_id'],
city.id,
)
try:
new_place = Place.create(**place_dictionary)
except:
return {"code":400, "msg":"Bad Request"}, 400
return new_place.to_dict()
示例7: get_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def get_city(sid, id):
try:
city = City.get(City.id == id, City.state == sid)
except Exception:
return {'code': 404, 'msg': 'City not found'}, 404
return city.to_hash(), 200
示例8: create_place_by_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def create_place_by_city(state_id, city_id):
post_data = request.values
keys=["name", "description", "latitude", "longitude", "owner_id"]
for key in keys:
if key not in post_data:
return {"code":400, "msg":"bad request, incorrect parameters"}
try:
city = City.get(City.id == city_id, City.state == state_id)
except:
return {"code":400, "msg":"bad request, city or state does not exist"}, 400
new_place = Place.create(
owner=int(post_data['owner_id']),
name=post_data['name'],
city=city.id,
description=post_data['description'],
latitude=float(post_data['latitude']),
longitude=float(post_data['longitude'])
)
if 'number_rooms' in post_data:
new_place.number_rooms=int(post_data['number_rooms'])
if 'number_bathrooms' in post_data:
new_place.number_bathrooms=int(post_data['number_bathrooms'])
if 'max_guest' in post_data:
new_place.max_guest=int(post_data['max_guest'])
if 'price_by_night' in post_data:
new_place.price_by_night=int(post_data['price_by_night'])
new_place.save()
return new_place.to_hash()
示例9: delete_single_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def delete_single_city(state_id, city_id):
try:
query = City.get(City.id == city_id, City.state == state_id)
except City.DoesNotExist:
return {"code":404, "msg":"city not found"}, 404
out_dict = query.to_dict()
query.delete_instance()
return out_dict
示例10: delete_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def delete_city(sid, id):
try:
city = City.get(City.id == id, City.state == sid)
except Exception:
return {'code': 404, 'msg': 'City not found'}, 404
city.delete_instance()
return {'code': 200, 'msg': 'Deleted successfully'}, 200
示例11: places_by_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def places_by_city(state_id, city_id):
try:
city_query = City.get(City.id == city_id, City.state == state_id)
except:
return {"code":404, "msg":"not found"}, 404
places = []
query = Place.select().where(Place.city == city_query.id)
for place in query:
places.append(place.to_hash())
return jsonify(places)
示例12: cities_states
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def cities_states(state_id, city_id):
if request.method == "GET":
try:
query = City.get(City.id == city_id)
return jsonify(query.to_dict()), 200
except City.DoesNotExist:
return json_response(status_=404, msg="not found")
elif request.method == "DELETE":
try:
city = City.get(City.id == city_id)
except City.DoesNotExist:
return json_response(status_=404, msg="Not found")
city.delete_instance()
city.save()
return json_response(status_=200, msg="City deleted")
示例13: get_places_by_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [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: places_within_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [as 别名]
def places_within_city(state_id, city_id):
response = jsonify({'code': 404, 'msg': 'not found'})
response.status_code = 404
# Getting the information for the place
if request.method == 'GET':
try:
list = ListStyle.list(Place.select()
.join(City)
.where(City.id == city_id)
.where(Place.city == city_id,
City.state == state_id), request)
return jsonify(list)
except:
return response
# Creating a new place
elif request.method == 'POST':
try:
# Getting the city to check if it exist
City.get(City.id == city_id, City.state == state_id)
# adding city by Using Post
add_place = Place.create(owner=request.form['owner'],
city=city_id,
name=request.form['name'],
description=request.form['description'],
number_rooms=request.form['number_rooms'],
number_bathrooms=request.form['number_bathrooms'],
max_guest=request.form['max_guest'],
price_by_night=request.form['price_by_night'],
latitude=request.form['latitude'],
longitude=request.form['longitude'])
add_place.save()
return jsonify(add_place.to_dict())
print("You've just added a place!")
except:
return response
示例15: modify_city
# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import get [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))