當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。