<s> import json <EOL> import pymongo <EOL> from flask import request , abort , json <EOL> from flask . ext import restful <EOL> from flask . ext . restful import reqparse <EOL> from flask_rest_service import app , api , mongo <EOL> from bson . objectid import ObjectId <EOL> from bson . code import Code <EOL> class ProductsList ( restful . Resource ) : <EOL> def get ( self ) : <EOL> limit = request . args . get ( '<STR_LIT>' , default = <NUM_LIT:50> , type = int ) <EOL> skip = request . args . get ( '<STR_LIT>' , default = <NUM_LIT:0> , type = int ) <EOL> count = request . args . get ( '<STR_LIT:count>' , default = <NUM_LIT:0> , type = int ) <EOL> short = request . args . get ( '<STR_LIT>' , default = <NUM_LIT:0> , type = int ) <EOL> query = request . args . get ( '<STR_LIT:q>' ) <EOL> data = dict ( ( key , { '<STR_LIT>' : request . args . get ( key ) , '<STR_LIT>' : '<STR_LIT>' } ) for key in request . args . keys ( ) ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> del data [ '<STR_LIT>' ] <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> del data [ '<STR_LIT>' ] <EOL> if request . args . get ( '<STR_LIT:count>' ) : <EOL> del data [ '<STR_LIT:count>' ] <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> del data [ '<STR_LIT>' ] <EOL> if request . args . get ( '<STR_LIT:q>' ) : <EOL> del data [ '<STR_LIT:q>' ] <EOL> fieldsNeeded = { '<STR_LIT:code>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> if request . args . get ( '<STR_LIT>' ) and short == <NUM_LIT:1> : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> and not request . args . get ( '<STR_LIT:q>' ) : <EOL> return mongo . db . products . find ( data , fieldsNeeded ) . count ( ) <EOL> elif not request . args . get ( '<STR_LIT:count>' ) and not request . args . get ( '<STR_LIT:q>' ) : <EOL> return mongo . db . products . find ( data , fieldsNeeded ) . sort ( '<STR_LIT>' , pymongo . DESCENDING ) . skip ( skip ) . limit ( limit ) <EOL> elif request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> and request . args . get ( '<STR_LIT:q>' ) : <EOL> return mongo . db . products . find ( { "<STR_LIT>" : { "<STR_LIT>" : query } } , fieldsNeeded ) . count ( ) <EOL> else : <EOL> return mongo . db . products . find ( { "<STR_LIT>" : { "<STR_LIT>" : query } } , fieldsNeeded ) . sort ( '<STR_LIT>' , pymongo . DESCENDING ) . skip ( skip ) . limit ( limit ) <EOL> else : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> and not request . args . get ( '<STR_LIT:q>' ) : <EOL> return mongo . db . products . find ( data ) . count ( ) <EOL> elif not request . args . get ( '<STR_LIT:count>' ) and not request . args . get ( '<STR_LIT:q>' ) : <EOL> return mongo . db . products . find ( data ) . sort ( '<STR_LIT>' , pymongo . DESCENDING ) . skip ( skip ) . limit ( limit ) <EOL> elif request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> and request . args . get ( '<STR_LIT:q>' ) : <EOL> return mongo . db . products . find ( { "<STR_LIT>" : { "<STR_LIT>" : query } } ) . count ( ) <EOL> else : <EOL> return mongo . db . products . find ( { "<STR_LIT>" : { "<STR_LIT>" : query } } ) . sort ( '<STR_LIT>' , pymongo . DESCENDING ) . skip ( skip ) . limit ( limit ) <EOL> class ProductsStats ( restful . Resource ) : <EOL> def get ( self ) : <EOL> map = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> reduce = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> listRes = [ ] <EOL> result = mongo . db . products . map_reduce ( map , reduce , "<STR_LIT>" ) <EOL> for doc in result . find ( ) : <EOL> res = { } <EOL> res [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> res [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> res [ '<STR_LIT:count>' ] = doc [ '<STR_LIT:value>' ] [ '<STR_LIT:count>' ] <EOL> res [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> res [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> res [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> res [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> listRes . append ( res ) <EOL> return listRes <EOL> class ProductId ( restful . Resource ) : <EOL> def get ( self , barcode ) : <EOL> return mongo . db . products . find_one ( { "<STR_LIT:code>" : barcode } ) <EOL> class ProductsBrands ( restful . Resource ) : <EOL> def get ( self ) : <EOL> count = request . args . get ( '<STR_LIT:count>' , default = <NUM_LIT:0> , type = int ) <EOL> query = request . args . get ( '<STR_LIT>' ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> map = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> reduce = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> result = mongo . db . products . map_reduce ( map , reduce , "<STR_LIT>" ) <EOL> res = [ ] <EOL> for doc in result . find ( { "<STR_LIT>" : { '<STR_LIT>' : query , '<STR_LIT>' : '<STR_LIT>' } } ) : <EOL> res . append ( doc [ '<STR_LIT>' ] ) <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( res ) <EOL> else : <EOL> return res <EOL> else : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( mongo . db . products . distinct ( '<STR_LIT>' ) ) <EOL> else : <EOL> return mongo . db . products . distinct ( '<STR_LIT>' ) <EOL> class ProductsCategories ( restful . Resource ) : <EOL> def get ( self ) : <EOL> count = request . args . get ( '<STR_LIT:count>' , default = <NUM_LIT:0> , type = int ) <EOL> query = request . args . get ( '<STR_LIT>' ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> map = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> reduce = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> result = mongo . db . products . map_reduce ( map , reduce , "<STR_LIT>" ) <EOL> res = [ ] <EOL> for doc in result . find ( { "<STR_LIT>" : { '<STR_LIT>' : query , '<STR_LIT>' : '<STR_LIT>' } } ) : <EOL> res . append ( doc [ '<STR_LIT>' ] ) <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( res ) <EOL> else : <EOL> return res <EOL> else : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( mongo . db . products . distinct ( '<STR_LIT>' ) ) <EOL> else : <EOL> return mongo . db . products . distinct ( '<STR_LIT>' ) <EOL> class ProductsCountries ( restful . Resource ) : <EOL> def get ( self ) : <EOL> count = request . args . get ( '<STR_LIT:count>' , default = <NUM_LIT:0> , type = int ) <EOL> query = request . args . get ( '<STR_LIT>' ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> map = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> reduce = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> result = mongo . db . products . map_reduce ( map , reduce , "<STR_LIT>" ) <EOL> res = [ ] <EOL> for doc in result . find ( { "<STR_LIT>" : { '<STR_LIT>' : query , '<STR_LIT>' : '<STR_LIT>' } } ) : <EOL> res . append ( doc [ '<STR_LIT>' ] ) <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( res ) <EOL> else : <EOL> return res <EOL> else : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( mongo . db . products . distinct ( '<STR_LIT>' ) ) <EOL> else : <EOL> return mongo . db . products . distinct ( '<STR_LIT>' ) <EOL> class ProductsAdditives ( restful . Resource ) : <EOL> def get ( self ) : <EOL> count = request . args . get ( '<STR_LIT:count>' , default = <NUM_LIT:0> , type = int ) <EOL> query = request . args . get ( '<STR_LIT>' ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> map = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> reduce = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> result = mongo . db . products . map_reduce ( map , reduce , "<STR_LIT>" ) <EOL> res = [ ] <EOL> for doc in result . find ( { "<STR_LIT>" : { '<STR_LIT>' : query , '<STR_LIT>' : '<STR_LIT>' } } ) : <EOL> res . append ( doc [ '<STR_LIT>' ] ) <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( res ) <EOL> else : <EOL> return res <EOL> else : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( mongo . db . products . distinct ( '<STR_LIT>' ) ) <EOL> else : <EOL> return mongo . db . products . distinct ( '<STR_LIT>' ) <EOL> class ProductsAllergens ( restful . Resource ) : <EOL> def get ( self ) : <EOL> count = request . args . get ( '<STR_LIT:count>' , default = <NUM_LIT:0> , type = int ) <EOL> query = request . args . get ( '<STR_LIT>' ) <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> map = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> reduce = Code ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> result = mongo . db . products . map_reduce ( map , reduce , "<STR_LIT>" ) <EOL> res = [ ] <EOL> for doc in result . find ( { "<STR_LIT>" : { '<STR_LIT>' : query , '<STR_LIT>' : '<STR_LIT>' } } ) : <EOL> res . append ( doc [ '<STR_LIT>' ] ) <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( res ) <EOL> else : <EOL> return res <EOL> else : <EOL> if request . args . get ( '<STR_LIT:count>' ) and count == <NUM_LIT:1> : <EOL> return len ( mongo . db . products . distinct ( '<STR_LIT>' ) ) <EOL> else : <EOL> return mongo . db . products . distinct ( '<STR_LIT>' ) <EOL> api . add_resource ( ProductsList , '<STR_LIT>' ) <EOL> api . add_resource ( ProductsStats , '<STR_LIT>' ) <EOL> api . add_resource ( ProductId , '<STR_LIT>' ) <EOL> api . add_resource ( ProductsBrands , '<STR_LIT>' ) <EOL> api . add_resource ( ProductsCategories , '<STR_LIT>' ) <EOL> api . add_resource ( ProductsCountries , '<STR_LIT>' ) <EOL> api . add_resource ( ProductsAdditives , '<STR_LIT>' ) <EOL> api . add_resource ( ProductsAllergens , '<STR_LIT>' ) </s>
