当前位置: 首页>>代码示例>>Python>>正文


Python City.create方法代码示例

本文整理汇总了Python中app.models.city.City.create方法的典型用法代码示例。如果您正苦于以下问题:Python City.create方法的具体用法?Python City.create怎么用?Python City.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在app.models.city.City的用法示例。


在下文中一共展示了City.create方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle_city

# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import create [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
开发者ID:Siphan,项目名称:airbnb_clone,代码行数:31,代码来源:city.py

示例2: create_new_city

# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import create [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
开发者ID:madejean,项目名称:airbnb_clone,代码行数:14,代码来源:city.py

示例3: cities

# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import create [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
开发者ID:papamuziko,项目名称:sample_airbnb_clone,代码行数:53,代码来源:city.py

示例4: create_city

# 需要导入模块: from app.models.city import City [as 别名]
# 或者: from app.models.city.City import create [as 别名]
def create_city(state_id):
    """
    Create a new city
    Create a new city in the given state.
    ---
    tags:
        - City
    parameters:
        -
            name: state_id
            in: path
            type: integer
            required: True
            description: ID of the state
        -
            name: name
            in: form
            type: string
            required: True
            description: Name of the city
    responses:
        201:
            description: City was created
            schema:
                $ref: '#/definitions/create_amenity_post_post_success'
        400:
            description: Issue with city request
        409:
            description: City 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 if state exists '''
        query = State.select().where(State.id == state_id)
        if not query.exists():
            raise LookupError('state_id')

        ''' Check if 'name' key in data '''
        if not 'name' in data:
            raise KeyError('name')

        ''' Check if 'name' value is a string '''
        if not type_test(data['name'], 'string'):
            raise TypeError("'name' value is not a string")

        ''' Check if city already exists '''
        query = City.select().where(City.name == data['name'])
        if query.exists():
            raise ValueError('City already exists')

        ''' Create new city in given state '''
        new = City.create(
            name = data['name'],
            state_id = state_id
        )
        res = {}
        res['code'] = 201
        res['id'] = int(new.id)
        res['msg'] = "City was created successfully"
        return res, 201
    except KeyError as e:
        res = {}
        res['code'] = 40000
        res['msg'] = 'Missing parameters'
        return res, 400
    except LookupError as e:
        abort(404)
    except TypeError as e:
        res = {}
        res['code'] = 400
        res['msg'] = e.message
        return res, 400
    except ValueError as e:
        res = {}
        res['code'] = 10002
        res['msg'] = e.message
        return res, 409
    except Exception as e:
        abort(500)
开发者ID:rickharris-dev,项目名称:airbnb_clone,代码行数:86,代码来源:city.py


注:本文中的app.models.city.City.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。