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


Python Amenity.create方法代码示例

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


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

示例1: create_amenities

# 需要导入模块: from app.models.amenity import Amenity [as 别名]
# 或者: from app.models.amenity.Amenity import create [as 别名]
def create_amenities():
    data = request.form
    check_amenity =  Amenity.select(). where(Amenity.name == data['name'])
    if check_amenity:
        return {'code': 10003, 'msg': 'Name already exists'}, 409

    amenity = Amenity.create(
        name = data['name']
    )
    return {'code': 201, 'msg': 'Amenity created successfully'}, 201
开发者ID:havk64,项目名称:airbnb_clone,代码行数:12,代码来源:amenity.py

示例2: handle_amenity

# 需要导入模块: from app.models.amenity import Amenity [as 别名]
# 或者: from app.models.amenity.Amenity import create [as 别名]
def handle_amenity():
    '''Returns all amenities as JSON objects in an array with a GET request.
    Adds an amenity with a POST request.
    '''
    if request.method == 'GET':
        list = ListStyle().list(Amenity.select(), request)
        return jsonify(list), 200

    elif request.method == 'POST':
        try:
            Amenity.select().where(Amenity.name == request.form['name']).get()
            return jsonify(code=10003, msg="Name already exists"), 409
        except Amenity.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

            amenity = Amenity.create(name=request.form['name'])
            return jsonify(amenity.to_dict()), 200
开发者ID:Siphan,项目名称:airbnb_clone,代码行数:22,代码来源:amenity.py

示例3: create_amenity

# 需要导入模块: from app.models.amenity import Amenity [as 别名]
# 或者: from app.models.amenity.Amenity import create [as 别名]
def create_amenity():
    """
    Create a new amenity
    Create a new amenity in the database.
    ---
    tags:
        - Amenity
    parameters:
        -
            name: name
            in: form
            type: string
            required: True
            description: Name of the amenity
    responses:
        201:
            description: Amenity was created
            schema:
                id: post_success
                required:
                    - code
                    - id
                    - msg
                properties:
                    code:
                        type: integer
                        description: Response code from the API
                        default: 201
                    id:
                        type: integer
                        description: ID of the newly created record
                        default: 1
                    msg:
                        type: string
                        description: Message about record creation
                        default: "created successfully"
        400:
            description: Issue with amenity request
        409:
            description: Amenity already exists
        500:
            description: The request was not able to be processed
    """
    data = {}
    if request.json:
        data = request.json
    else:
        for key in request.form.keys():
        	for value in request.form.getlist(key):
        		data[key] = value
    try:
        ''' Check if name for amenity was given '''
        if not 'name' in data:
            raise KeyError("'name'")

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

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

        ''' Create new amenity '''
        new = Amenity.create(
            name = data['name']
        )
        res = {}
        res['code'] = 201
        res['id'] = new.id
        res['msg'] = "amenity was created successfully"
        return res, 201
    except KeyError as e:
        res = {}
        res['code'] = 40000
        res['msg'] = 'missing parameters'
        return res, 400
    except TypeError as e:
        res = {}
        res['code'] = 400
        res['msg'] = e.message
        return res, 400
    except ValueError as e:
        res = {}
        res['code'] = 10003
        res['msg'] = e.message
        return res, 409
    except Exception as e:
        abort(500)
开发者ID:rickharris-dev,项目名称:airbnb_clone,代码行数:92,代码来源:amenity.py


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