|
|
@ -9,7 +9,7 @@ import flask_restful.reqparse
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy.engine
|
|
|
|
import sqlalchemy.engine
|
|
|
|
|
|
|
|
|
|
|
|
from db import db, Product
|
|
|
|
from db import db, Product, Variant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app = flask.Flask(__name__)
|
|
|
@ -53,6 +53,13 @@ product_fields = {
|
|
|
|
'image_url': flask_restful.fields.String(),
|
|
|
|
'image_url': flask_restful.fields.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
variant_fields = {
|
|
|
|
|
|
|
|
'id': flask_restful.fields.Integer(),
|
|
|
|
|
|
|
|
'color': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'front_url': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'back_url': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filter_parser = flask_restful.reqparse.RequestParser()
|
|
|
|
filter_parser = flask_restful.reqparse.RequestParser()
|
|
|
|
filter_parser.add_argument('filter', type=str)
|
|
|
|
filter_parser.add_argument('filter', type=str)
|
|
|
@ -102,8 +109,47 @@ class ProductsResource(flask_restful.Resource):
|
|
|
|
return products, 200, {'X-Total-Count': count}
|
|
|
|
return products, 200, {'X-Total-Count': count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VariantResource(flask_restful.Resource):
|
|
|
|
|
|
|
|
@flask_restful.marshal_with(variant_fields)
|
|
|
|
|
|
|
|
def get(self, product_id, id):
|
|
|
|
|
|
|
|
q = db.session.query(Variant).filter(Variant.product_id == product_id, Variant.id == id)
|
|
|
|
|
|
|
|
variant = q.first()
|
|
|
|
|
|
|
|
if not variant:
|
|
|
|
|
|
|
|
flask_restful.abort(404, message='Variant {0} does not exist'.format(id))
|
|
|
|
|
|
|
|
return variant, 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VariantsResource(flask_restful.Resource):
|
|
|
|
|
|
|
|
@flask_restful.marshal_with(variant_fields)
|
|
|
|
|
|
|
|
def get(self, product_id):
|
|
|
|
|
|
|
|
args = filter_parser.parse_args()
|
|
|
|
|
|
|
|
q = db.session.query(Variant).filter(Variant.product_id == product_id)
|
|
|
|
|
|
|
|
if args['filter']:
|
|
|
|
|
|
|
|
q = q.filter(Variant.color.ilike('%{}%'.format(args['filter'])))
|
|
|
|
|
|
|
|
count = q.count()
|
|
|
|
|
|
|
|
if args['sort_order'] == 'random':
|
|
|
|
|
|
|
|
q = q.order_by(sqlalchemy.func.random())
|
|
|
|
|
|
|
|
elif args['sort_by']:
|
|
|
|
|
|
|
|
col = getattr(Variant, args['sort_by'], None)
|
|
|
|
|
|
|
|
if col:
|
|
|
|
|
|
|
|
if args['sort_order']:
|
|
|
|
|
|
|
|
order_by = getattr(col, args['sort_order'], None)
|
|
|
|
|
|
|
|
if order_by:
|
|
|
|
|
|
|
|
q = q.order_by(order_by())
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
q = q.order_by(col)
|
|
|
|
|
|
|
|
if args['page_size']:
|
|
|
|
|
|
|
|
q = q.limit(args['page_size'])
|
|
|
|
|
|
|
|
if args['page_number'] and args['page_size']:
|
|
|
|
|
|
|
|
q = q.offset(args['page_number'] * args['page_size'])
|
|
|
|
|
|
|
|
variants = q.all()
|
|
|
|
|
|
|
|
return variants, 200, {'X-Total-Count': count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api.add_resource(ProductResource, '/products/<int:id>')
|
|
|
|
api.add_resource(ProductResource, '/products/<int:id>')
|
|
|
|
api.add_resource(ProductsResource, '/products')
|
|
|
|
api.add_resource(ProductsResource, '/products')
|
|
|
|
|
|
|
|
api.add_resource(VariantResource, '/products/<int:product_id>/variants/<int:id>')
|
|
|
|
|
|
|
|
api.add_resource(VariantsResource, '/products/<int:product_id>/variants')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|