Exemple #1
0
def get(video_id):
    """Get a video identified by `video_id`.

    **Example request:**

    .. sourcecode:: http

       GET /videos/1/ HTTP/1.1
       Accept: application/json

    **Example response:**

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

       {
        "name": "Joe Schmoe",
        "first_name": "Joe",
        "second_name": "Schmoe",
        "image_url": "abc"
       }

    :statuscode 200: success
    :statuscode 404: video does not exist
    """
    columns = ['Video.name', 'Video.image_url',
        'Video.video_sources', 'VideoSource.name', 'VideoSource.url', 
        'VideoSource.source_id',
        'Video.products', 'Product.model', 'Product.uri', 'Product.brand',
        'Product.product_images', 'ProductImage.url',
        'Product.product_style', 'RefProductStyle.name']
    try:
        message = 'success'
        data = Video.query.outerjoin(Video.video_sources, Video.products,
            Product.product_style, Product.product_images,
            ).options(
                contains_eager(Video.video_sources),
                contains_eager(Video.products),
            ).filter(Video.id==video_id
            ).order_by(RefProductStyle.name).limit(100).all()[0]
    except IndexError as error:
        message = "'%s' record does not exist." % video_id
        return jsonify(message=message, success=False, error=404), 404
    except Exception as error:
        message = '%s: %s' % (error.__class__.__name__, error)
        return jsonify(message=message, success=False, error=500), 500

    if data is None:
        message = "'%s' record does not exist." % video_id
        return jsonify(message=message, success=False, error=404), 404
    else:
        ## need to use the JSONEncoder class for datetime objects
        response = make_response(json.dumps(dict(data=data, message=message,
            success=True), cls=json_encoder(False, columns)))
        response.headers['Content-Type'] = 'application/json'
        response.headers['mimetype'] = 'application/json'
        return response
Exemple #2
0
def get(artist_id):
    """Get a artist identified by `artist_id`.

    **Example request:**

    .. sourcecode:: http

       GET /artists/123 HTTP/1.1
       Accept: application/json

    **Example response:**

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

       {
        "name": "Joe Schmoe",
        "first_name": "Joe",
        "second_name": "Schmoe",
        "image_url": "abc"
       }

    :statuscode 200: success
    :statuscode 404: artist does not exist
    """
    columns = ['Artist.name', 'Artist.first_name', 'Artist.last_name', 
        'Artist.image_url',
        'Artist.videos', 'Video.name', 'Video.image_url', 'Video.uri',
        'Video.video_sources', 'VideoSource.url']
    try:
        message = 'success'
        data = Artist.query.outerjoin(Artist.videos, Video.video_sources
            ).options(
                contains_eager(Artist.videos, Video.video_sources),
            ).filter(Artist.id==artist_id
            ).first()
    except NoResultFound as error:
        message = '%s: %s' % (error.__class__.__name__, error)
        return jsonify(message=message, success=False), 404
    except Exception as error:
        message = '%s: %s' % (error.__class__.__name__, error)
        return jsonify(message=message, success=False), 500

    if data is None:
        message = "'%s' record does not exist." % artist_id
        return jsonify(error=404, message=message, success=False), 404
    else:
        ## need to use the JSONEncoder class for datetime objects
        response = make_response(json.dumps(dict(data=data, message=message,
            success=True), cls=json_encoder(False, columns)))
        response.headers['Content-Type'] = 'application/json'
        response.headers['mimetype'] = 'application/json'
        return response
Exemple #3
0
def get_all():
    """Get all the videos.

    **Example request:**

    .. sourcecode:: http

       GET /videos HTTP/1.1
       Accept: application/json

    **Example response:**

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

       {
         "data": [
           {
            "name": "abc",
           },
           {
            "name": "xyz",
           }
         ]
       }

    :statuscode 200: success
    :statuscode 404: videos do not exist
    """
    columns = ['Video.name', 'Video.image_url',
        #Video.products, Product.upc]
	    'Video.video_sources', 'VideoSource.name', 'VideoSource.url', 
        'VideoSource.source_id']
    try:
        message = 'success'
        data = Video.query.outerjoin(Video.video_sources
            ).options(
                contains_eager(Video.video_sources),
            ).limit(10).all()
    except Exception as error:
        message = '%s: %s' % (error.__class__.__name__, error)
        return jsonify(message=message, success=False), 500
    if data is None or not data:
        message = "No video data exists."
        return jsonify(error=404, message=message, success=False), 404
    else:
        ## need to use the JSONEncoder class for datetime objects
        response = make_response(json.dumps(dict(data=data, message=message,
            success=True), cls=json_encoder(False, columns)))
        response.headers['Content-Type'] = 'application/json'
        response.headers['mimetype'] = 'application/json'
        return response
Exemple #4
0
def get(product_id):
    """Get a product identified by `product_id`.

    **Example request:**

    .. sourcecode:: http

       GET /products/123 HTTP/1.1
       Accept: application/json

    **Example response:**

    .. sourcecode:: http

       HTTP/1.1 200 OK
       Content-Type: application/json

        data = {
            'brand': 'abc',
            'upc': '123'
            ...
        }

    :statuscode 200: success
    :statuscode 404: product does not exist
    """
    ## columns can either be str or class Attributes, but class Attributes are 
    ## required to specify columns
    columns = [Product.upc, Product.brand, Product.model,
        Product.product_type, RefProductType.name, 
        Product.product_style, RefProductStyle.name,
        Product.product_images, ProductImage.url,
        Product.product_retailers, ProductRetailer.url, ProductRetailer.price,
        ProductRetailer.retailer, Retailer.name, Retailer.image_url]
    try:
        message = 'success'
        ## INNER JOIN any relationship in 'join()' and LEFT OUTER JOIN any other
        ## relationship in 'outerjoin()'. contains_eager will eager load the 
        ## columns and you need to include indirect relationships in one ().
        data = Product.query.join(Product.product_type, Product.product_style,
            ).outerjoin(Product.product_images, Product.product_retailers, 
                ProductRetailer.retailer
            ).options(
                contains_eager(Product.product_type), 
                contains_eager(Product.product_style),
                contains_eager(Product.product_images),
                contains_eager(Product.product_retailers),
                contains_eager(Product.product_retailers, 
                    ProductRetailer.retailer),
            ).filter(Product.id==product_id
            ).limit(100).all()[0]
        ## TODO need to determine best method to parse the NamedTuple for
        ## selecting specific columns
        #data = db_session.query(*columns).join(Product.product_type, 
            #Product.product_style
            #).outerjoin(Product.product_images
            #).filter(Product.id==product_id
            #).first()
    except IndexError as error:
        message = "'%s' record does not exist." % product_id
        return jsonify(message=message, success=False, error=404), 404
    except Exception as error:
        message = '%s: %s' % (error.__class__.__name__, error)
        return jsonify(message=message, success=False, error=500), 500

    if data is None:
        message = "'%s' record does not exist." % product_id
        return jsonify(message=message, success=False, error=404), 404
    else:
        ## need to use the JSONEncoder class for datetime objects
        response = make_response(json.dumps(dict(data=data, message=message,
            success=True), cls=json_encoder(False, columns)))
        response.headers['Content-Type'] = 'application/json'
        response.headers['mimetype'] = 'application/json'
        return response