예제 #1
0
    def reviews_get(request):
        """
        Returns the business reviews


        errors
        ======

        * status 404 - if the business can't be found


        returns
        =======

        ::

            {
                "reviews": [
                    {
                        "rating": 5,
                        "modified": "2015-08-10T00:20:17.753000",
                        "id": "55c7ee41fad9b43993d71919",
                        "user_id": "55c7ee3dfad9b43993d7190e",
                        "reviewed_id": "55c7ee3efad9b43993d7190f",
                        "tags": [
                            "awesome"
                        ],
                        "text": "This is awesome.",
                        "created": "2015-08-10T00:20:17.753000",
                        "reviewed_collection": "businesses"
                    }
                ]
            }
        """
        business_id = request.matchdict['business_id']
        business = Business.get_by_id(business_id)

        if business:
            response_body = {
                'reviews': Review.reviews_for_reviewed(business.collection, business.id)
            }
            logger.debug('Retrieved business:{} reviews'.format(business.id))
        else:
            logger.debug('Failed to retrieve business:{} reviews'.format(business_id))
            request.response.status_int = 404
            response_body = {
                'status': 'error',
                'message': 'failed to find business'
            }

        request.response.body = json.dumps(response_body, cls=ComplexEncoder)
        request.response.content_type = 'application/json'
        return request.response
예제 #2
0
    def business_get(request):
        """
        Returns the business

        errors
        ======

        * status 404 - if the business can't be found


        returns
        =======

        /businesses/<id>?reviews=true

        ::


            {
                "rating": 5,
                "modified": "2015-08-10T00:20:14.370000",
                "address": {
                    "street1": "Time Square",
                    "postal_code": "",
                    "street2": "",
                    "city": "New York",
                    "state": "NY"
                },
                "id": "55c7ee3efad9b43993d7190f",
                "location": [
                    -73.985131,
                    40.758895
                ],
                "reviews": [
                    {
                    "rating": 5,
                    "modified": "2015-08-10T00:20:17.753000",
                    "id": "55c7ee41fad9b43993d71919",
                    "user_id": "55c7ee3dfad9b43993d7190e",
                    "reviewed_id": "55c7ee3efad9b43993d7190f",
                    "tags": [
                        "awesome"
                    ],
                    "text": "This is awesome.",
                    "created": "2015-08-10T00:20:17.753000",
                    "reviewed_collection": "businesses"
                    }
                ],
                "tags": [
                    "awesome"
                ],
                "name": "Time Square",
                "created": "2015-08-10T00:20:13.760000"
            }
        """
        business_id = request.matchdict['business_id']
        include_reviews = request.validated['reviews']
        business = Business.get_by_id(business_id)

        # a light redis check is an example of how queries can be further optimized
        if include_reviews:
            cache_key = 'business:{}:reviews-true'.format(business_id)
        else:
            cache_key = 'business:{}:reviews-false'.format(business_id)

        cached = redis_cache.get(cache_key)

        if cached and business:
            response_body = cached
        elif business:
            response_body = business.toJSON()

            if include_reviews:
                response_body['reviews'] = Review.reviews_for_reviewed(business.collection, business.id)

            response_body = json.dumps(response_body, cls=ComplexEncoder)
            redis_cache.set(cache_key, response_body)
            logger.debug('Retrieved business:{}'.format(business.id))
        else:
            logger.debug('Failed to retrieve business:{}'.format(business_id))
            request.response.status_int = 404
            response_body = json.dumps({
                'status': 'error',
                'message': 'failed to find business'
            })

        request.response.body = response_body
        request.response.content_type = 'application/json'
        return request.response