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


Python State.get方法代码示例

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


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

示例1: state_id

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def state_id(state_id):
    """Handle GET and DELETE requests to /states/<state_id> route.

     Return a hash of the appropriate record in the database in the case of a
     GET request.
     Delete the appropriate record in the case of a DELETE request.
     """
    # check whether resource exists:
    # --------------------------------------------------------------------------
    try:
        record = State.get(State.id == state_id)

    # return 404 not found if it does not
    except State.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 state\n'
开发者ID:ronachong,项目名称:airbnb_clone,代码行数:34,代码来源:state.py

示例2: get_state

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def get_state(id):
    try:
        state = State.get(State.id == id)
    except Exception as e:
        return {'code': 404, 'msg': "State not found"}, 404

    return state.to_hash(), 200
开发者ID:havk64,项目名称:airbnb_clone,代码行数:9,代码来源:state.py

示例3: delete_state

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def delete_state(number):
    try:
        query = State.get(State.id == number)
    except State.DoesNotExist:
        return {'code':404, 'msg':'state not found'}, 404
    out_json = query.to_dict()
    query.delete_instance()
    return out_json
开发者ID:bilalbarki,项目名称:airbnb_clone,代码行数:10,代码来源:state.py

示例4: modify_state

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def modify_state(state_id):
    try:
        # displaying state by id
        if request.method == 'GET':
            return jsonify(State.get(State.id == state_id).to_dict())
    except:
        return "No State was found with id %d\n" % (int(id))
    if request.method == 'DELETE':
        id = state_id
        try:
            # creating and oobject calling the State
            get_state = State.get(State.id == id)
            # deleting state based on the id
            get_state.delete_instance()
            return "State with id %d was deleted\n" % (int(id))
        except:
            return "No State was found with id %d\n" % (int(id))
开发者ID:frakentoaster,项目名称:airbnb_clone,代码行数:19,代码来源:state.py

示例5: to_hash

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
 def to_hash(self):
     """Method to_hash returns City object model to hash"""
     state = State.get(State.id == self.state)
     city = {
         'name'      : self.name,
         'state_id'  : state.id,
     }
     return super(City, self).to_hash(self, city)
开发者ID:havk64,项目名称:airbnb_clone,代码行数:10,代码来源:city.py

示例6: get_state

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def get_state(state_id):
    """
    Get the given state
    Returns the given state in the database.
    ---
    tags:
    	- State
    parameters:
    	-
    		in: path
    		name: state_id
    		type: integer
    		required: True
    		description: ID of the state
    responses:
        200:
            description: State returned successfully
            schema:
                id: State
                required:
                    - name
                    - id
                    - created_at
                    - updated_at
                properties:
                    name:
                        type: string
                        description: name of the given state
                        default: None
                    id:
                        type: integer
                        description: id of the state
                        default: 1
                    created_at:
                        type: datetime string
                        description: date and time the state was created in the database
                        default: '2016-08-11 20:30:38'
                    updated_at:
                        type: datetime string
                        description: date and time the state was updated in the database
                        default: '2016-08-11 20:30:38'
        404:
            description: State was not found
        500:
            description: Request could not be processed
    """
    try:
        ''' Check that state_id exists '''
        query = State.select().where(State.id == state_id)
        if not query.exists():
            raise LookupError('state_id')

        state = State.get(State.id == state_id)
        return state.to_dict(), 200
    except LookupError as e:
        abort(404)
    except Exception as e:
        abort(500)
开发者ID:rickharris-dev,项目名称:airbnb_clone,代码行数:60,代码来源:state.py

示例7: states_id

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def states_id(state_id):
	if request.method == 'GET':
		try:
			state = State.get(State.id == state_id)
			return jsonify(state.to_dict()), 200

		except State.DoesNotExist:
			return json_response(status_=404, msg="not found")

	elif request.method == 'DELETE':
		try:
			state = State.get(State.id == state_id)
			state.delete_instance()
			state.save()
			return json_response(status_=200, msg="State succesfully deleted")

		except State.DoesNotExist:
			return json_response(status_=404, msg="state does not exist")
开发者ID:jdeepee,项目名称:airbnb_clone,代码行数:20,代码来源:state.py

示例8: get_list_places

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def get_list_places(state_id):
    try:
        # Checking if a state exist
        State.get(State.id == state_id)
    except State.DoesNotExist:
            return make_response(jsonify({'code': '10001',
                                          'msg': 'Place not found'}), 404)
    if request.method == 'GET':
        try:
            # getting a place that is in a specific state
            list = ListStyle.list(Place.select()
                                  .join(City)
                                  .where(Place.city == City.id)
                                  .join(State)
                                  .where(State.id == City.state), request)
            return jsonify(list)
        except:
            return make_response(jsonify({'code': '10001',
                                          'msg': 'Place not found'}), 404)
开发者ID:frakentoaster,项目名称:airbnb_clone,代码行数:21,代码来源:place.py

示例9: state

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def state(number):
    if request.method == 'GET':
        query = State.get(State.id == number)
        return query.to_hash()
    else:
        try:
            query = State.select().where(State.id == number).get()
        except:
            return {'code':404, 'msg':'state not found'}, 404
        out_json = query.to_hash()
        query.delete_instance()
        return out_json
开发者ID:madejean,项目名称:airbnb_clone,代码行数:14,代码来源:state.py

示例10: cities

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

示例11: states

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def states(state_id=None):
    
    if request.method == "GET":
        if state_id != None:
            try:
                state = State.get(State.id == int(state_id))
                return state.to_dict()
            except:
                pass
            return { 'code': 404, 'msg': "not found" }, 404

        return { 'data': [state.to_dict() for state in State.select()] }, 200
    
    elif request.method == "POST":
        name = request.form.get('name')
        
        try:
            new_state = State.create(name=name)
        except IntegrityError:
           return { 'code': 10001, 'msg': "State already exists" }, 409
        except Exception as e:
            raise e 
        
        return new_state.to_dict(), 201

    elif request.method == "DELETE":
        if state_id != None:
            state = None
            try:
                state = State.get(State.id == int(state_id))
            except:
                state = None
            
            if state != None:
                state.delete_instance()
                return {}, 200

        return { 'code': 404, 'msg': "not found" }, 404
开发者ID:papamuziko,项目名称:sample_airbnb_clone,代码行数:40,代码来源:state.py

示例12: test_create

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
    def test_create(self):
        """
        Test proper creation (or non-creation) of state records upon POST
        requests to API.
        """
        # test creation of state with all parameters provided in POST request

        State.drop_table()
        database.create_tables([State], safe=True)

        POST_request1 = self.app.post('/states', data=dict(
            name='namestring'
        ))

        self.assertEqual(POST_request1.status[:3], '200')

        now = datetime.now().strftime('%d/%m/%Y %H:%M')

        self.assertEqual(State.get(State.id == 1).name, 'namestring')
        self.assertEqual(State.get(State.id == 1).created_at.strftime('%d/%m/%Y %H:%M'), now)
        self.assertEqual(State.get(State.id == 1).updated_at.strftime('%d/%m/%Y %H:%M'), now)

        # test creation of state in all cases of a parameter missing in POST request
        POST_request2 = self.app.post('/states', data=dict())
        self.assertEqual(POST_request2.status[:3], '400')

        # test that state ID for sole record in database is correct
        self.assertEqual(State.select().get().id, 1)

        # test that a post request with a duplicate name value is rejected
        POST_request3 = self.app.post('/states', data=dict(
            name='namestring'
        ))

        self.assertEqual(POST_request3.status[:3], '409')
        self.assertEqual(json.loads(POST_request3.data), {'code': 10001, 'msg': 'State already exists'})
开发者ID:WilliamMcCann,项目名称:airbnb_clone,代码行数:38,代码来源:state.py

示例13: get_single_state

# 需要导入模块: from app.models.state import State [as 别名]
# 或者: from app.models.state.State import get [as 别名]
def get_single_state(number):
    try:
        query = State.get(State.id == number)
    except State.DoesNotExist:
        return {'code':404, 'msg':'state not found'}, 404
    return query.to_dict()
开发者ID:bilalbarki,项目名称:airbnb_clone,代码行数:8,代码来源:state.py


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