Exemple #1
0
    def get(self):
        # Retrieve existing vendor(s)
        _id = request.args.get('id')
        _market = request.args.get('market')

        # If no params
        if not _id and not _market:
            res = self.get_all()
            return reply_success(schemas.dump(res))

        # Throw error if more than one param
        if _id and _market:
            return reply_error(
                {"message": "Cannot use more than one parameter"})

        # Filter on id
        if _id:
            res = self.get_one(_id)
            if not res:
                return reply_missing(id=_id)

        # Filter on marketId
        if _market:
            marketVendors = db.session().query(MarketVendorModel).filter_by(
                marketId=_market)
            vendorIds = [mv.vendorId for mv in marketVendors]
            res = self.get_all_in(vendorIds)
            if not res:
                return reply_missing(id=_market)
            return reply_success(schemas.dump(res))
        return reply_success(schema.dump(res))
Exemple #2
0
 def get(self):
     # Retrieve existing item(s)
     _userName = request.args.get('userName')
     if not _userName:
         res = self.get_all()
         return reply_success(schemas.dump(res))
     res = self.get_one(_userName)
     if not res:
         return reply_missing(nonexistant=_userName)
     return reply_success(schema.dump(res))
Exemple #3
0
 def get(self):
     # Retrieve existing items(s)
     _id = request.args.get('id')
     if not _id:
         res = self.get_all()
         return reply_success(schemas.dump(res))
     res = self.get_one(_id)
     if not res:
         return reply_missing(id=_id)
     return reply_success(schema.dump(res))
Exemple #4
0
 def post(self):
     # Create new item(s)
     payload = request.get_json()
     try:
         obj = schema.load(payload)
         db.session.add(obj)
         db.session.commit()
         return reply_success(schema.dump(obj))
     except Exception as err:
         log.error(err)
         return reply_error(str(err))
Exemple #5
0
    def get(self, zipcode=None):
        parser = reqparse.RequestParser()
        parser.add_argument('distance', type=int, ignore=False)
        args = parser.parse_args()

        _distance = args.get('distance')

        # Pass all markets into distance filter
        vendors = db.session.query(VendorModel).all()
        locatedVendors = ZipCodes().locateThings(vendors, zipcode, _distance)
        return reply_success(locatedVendors)
Exemple #6
0
    def get(self, id=None):

        parser = reqparse.RequestParser()
        parser.add_argument('distance', type=int, ignore=False)
        parser.add_argument('zipcode', type=int, ignore=False)
        args = parser.parse_args()

        _ids = args.get('ids')
        _distance = args.get('distance')
        _zipcode = args.get('zipcode')

        produceVendors = db.session().query(VendorProduceModel, VendorModel) \
            .filter_by(produceId=id) \
            .join(VendorModel, VendorProduceModel.vendorId == VendorModel.id)

        vendors = [v for vp, v in produceVendors]
        if _zipcode:
            vendors = ZipCodes().locateThings(vendors, int(_zipcode),
                                              _distance)
            return reply_success(vendors)
        return reply_success(schemas.dump(vendors))
Exemple #7
0
    def get(self):

        parser = reqparse.RequestParser()
        parser.add_argument('id',
                            type=int,
                            required=True,
                            action='append',
                            ignore=False)
        parser.add_argument('distance', type=int, ignore=False)
        parser.add_argument('zipcode', type=int, ignore=False)
        parser.add_argument('intersect', type=bool, ignore=False)
        args = parser.parse_args()

        _ids = args['id']
        _zipcode = args['zipcode']
        _distance = args['distance']
        _intersect = args['intersect']

        produceVendors = db.session().query(VendorProduceModel, VendorModel) \
            .filter(VendorProduceModel.produceId.in_(_ids)) \
            .join(VendorModel, VendorProduceModel.vendorId == VendorModel.id)

        if _intersect:
            allVendors = []
            for id in _ids:
                vendors = [v for vp, v in produceVendors if vp.produceId == id]
                allVendors.append(vendors)
            vendors = list(set.intersection(*map(set, allVendors)))
        else:
            vendors = [v for vp, v in produceVendors]

        if _zipcode:
            vendors = ZipCodes().locateThings(vendors, int(_zipcode),
                                              _distance)
            return reply_success(vendors)

        return reply_success(schemas.dump(vendors))
Exemple #8
0
    def get(self):

        parser = reqparse.RequestParser()
        parser.add_argument('id', type=int, required=True, action='append', ignore=False)
        parser.add_argument('distance', type=int, ignore=False)
        parser.add_argument('zipcode', type=int, ignore=False)
        parser.add_argument('intersect', type=bool, ignore=False)
        args = parser.parse_args()

        _ids = args['id']
        _zipcode = args['zipcode']
        _distance = args['distance']
        _intersect = args['intersect']

        produceMarkets = db.session().query(VendorProduceModel, VendorModel, MarketVendorModel, MarketModel) \
            .filter(VendorProduceModel.produceId.in_(_ids)) \
            .join(VendorModel, VendorProduceModel.vendorId == VendorModel.id) \
            .join(MarketVendorModel, MarketVendorModel.vendorId == VendorProduceModel.vendorId) \
            .join(MarketModel, MarketVendorModel.marketId == MarketModel.id)

        if _intersect:
            allMarkets = []
            for id in _ids:
                markets = [m for vp, v, mv, m in produceMarkets if vp.produceId == id]
                allMarkets.append(markets)
            markets = list(set.intersection(*map(set, allMarkets)))
        else:
            markets = [m for vp, v, mv, m in produceMarkets]

        if _zipcode:
            markets = ZipCodes().locateThings(markets, int(_zipcode), _distance)
            dedup = list({v['id']: v for v in markets}.values())
            return reply_success(dedup)

        # Dedup
        markets = list({v.id: v for v in markets}.values())
        return reply_success(schemas.dump(markets))
Exemple #9
0
    def get(self, id=None):

        parser = reqparse.RequestParser()
        parser.add_argument('distance', type=int, ignore=False)
        parser.add_argument('zipcode', type=int, ignore=False)
        args = parser.parse_args()

        _zipcode = args['zipcode']
        _distance = args['distance']

        produceMarkets = db.session().query(VendorProduceModel, VendorModel, MarketVendorModel, MarketModel) \
            .filter_by(produceId=id) \
            .join(VendorModel, VendorProduceModel.vendorId == VendorModel.id) \
            .join(MarketVendorModel, MarketVendorModel.vendorId == VendorProduceModel.vendorId) \
            .join(MarketModel, MarketVendorModel.marketId == MarketModel.id)

        if not _zipcode:
            markets = [{
                        "produceId": id,
                        "marketName": m.marketName,
                        "marketId": m.id
                        }
                for vp, v, mv, m in produceMarkets]
            dedup = list({v['marketId']: v for v in markets}.values())
            return reply_success(dedup)
        else:
            markets = [m for vp, v, mv, m in produceMarkets]
            locatedMarkets = ZipCodes().locateThings(markets, _zipcode, _distance)
            locatedMarketIds = [lm['id'] for lm in locatedMarkets]
            returnMarkets = [{
                        "produceId": id,
                        "marketName": m.marketName,
                        "marketId": m.id
                        }
                for vp, v, mv, m in produceMarkets if m.id in locatedMarketIds]
            dedup = list({v['marketId']: v for v in returnMarkets}.values())
            return reply_success(dedup)
Exemple #10
0
 def get(self, id=None):
     vendorProduce = db.session().query(VendorProduceModel, ProduceModel) \
         .filter_by(vendorId=id) \
         .join(ProduceModel, VendorProduceModel.produceId == ProduceModel.id)
     produce = [{
                 "produceId": vp.produceId,
                 "quantity": vp.quantity,
                 "produceName": p.produceName,
                 "produceType": p.produceType,
                 "s3_url": p.s3_url
                 }
         for vp, p in vendorProduce]
     if not produce:
         return reply_missing(id=id)
     return reply_success(produce)