Exemple #1
0
    def post(cls):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:bid:create', cdict, cdict)

        try:
            data = request.get_json(force=True)
            return bid.Bid.create(data, g.context).to_dict(), 201
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #2
0
    def delete(cls, bid_id):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:bid:delete', cdict, cdict)

        try:
            b = bid.Bid.get(bid_id, g.context)
            b.destroy(g.context)
            return {'message': 'Bid deleted.'}
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #3
0
    def delete(cls, offer_id):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:offer:delete', cdict, cdict)

        try:
            o = offer.Offer.get(offer_id, g.context)
            o.destroy(g.context)
            return {'message': 'Offer deleted.'}
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #4
0
    def delete(cls, contract_id):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:contract:delete', cdict, cdict)

        try:
            c = contract.Contract.get(contract_id, g.context)
            c.destroy(g.context)
            return {'message': 'Contract deleted.'}
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #5
0
    def get(cls, bid_id=None):
        cdict = g.context.to_policy_values()

        if bid_id is None:
            policy.authorize('flocx_market:bid:get_all', cdict, cdict)
            return [x.to_dict() for x in bid.Bid.get_all(g.context)]
        try:
            policy.authorize('flocx_market:bid:get', cdict, cdict)
            return bid.Bid.get(bid_id, g.context).to_dict()
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #6
0
    def delete(cls, offer_contract_relationship_id):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:offer_contract_relationship:delete',
                         cdict, cdict)

        try:
            o = ocr.OfferContractRelationship.get(
                g.context, offer_contract_relationship_id)
            o.destroy(g.context)
            return {'message': 'OfferContractRelationship deleted.'}

        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #7
0
    def put(cls, bid_id):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:bid:update', cdict, cdict)

        data = request.get_json(force=True)
        try:
            b = bid.Bid.get(bid_id, g.context)
            # we only allow status field to be modified
            if 'status' in data:
                b.status = data['status']
                return b.save(g.context).to_dict()
            return b.to_dict()
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #8
0
    def put(cls, offer_contract_relationship_id):
        cdict = g.context.to_policy_values()
        policy.authorize('flocx_market:offer_contract_relationship:update',
                         cdict, cdict)

        data = request.get_json(force=True)
        try:
            o = ocr.OfferContractRelationship.get(
                g.context, offer_contract_relationship_id)
            if o is None:
                return {'message': 'OfferContractRelationship not found.'}, 404
            # we only allow status field to be modified
            if 'status' in data:
                o.status = data['status']
                return o.save(g.context).to_dict()
            return o.to_dict()
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #9
0
    def get(cls, offer_contract_relationship_id=None):
        cdict = g.context.to_policy_values()

        try:
            if offer_contract_relationship_id:
                policy.authorize(
                    'flocx_market:offer_contract_relationship:get', cdict,
                    cdict)
                ocr_with_id = ocr.OfferContractRelationship \
                    .get(g.context, offer_contract_relationship_id)
                return ocr_with_id.to_dict()

            policy.authorize(
                'flocx_market:offer_contract_relationship:get_all', cdict,
                cdict)
            offer_id = request.args.get('offer_id')
            contract_id = request.args.get('contract_id')
            status = request.args.get('status')

            possible_filters = {
                'offer_id': offer_id,
                'contract_id': contract_id,
                'status': status
            }
            filters = {}
            for key, value in possible_filters.items():
                if value is not None:
                    filters[key] = value

            ocrs = ocr.OfferContractRelationship.get_all(g.context, filters)
            if ocrs is None:
                return {'message': 'OfferContractRelationship not found'}, 404

            if type(ocrs) == list:
                return [a.to_dict() for a in ocrs]
            else:
                return ocrs.to_dict()
        except exception.MarketplaceException as e:
            return json.dumps(e.message), e.code
Exemple #10
0
def test_authorize_policy_not_registered():
    creds = {'roles': ['generic_user']}
    with raises(oslo_policy.PolicyNotRegistered):
        policy.authorize('flocx_market:foo:bar', creds, creds)
Exemple #11
0
def test_unauthorized():
    creds = {'roles': ['generic_user']}
    with raises(oslo_policy.PolicyNotAuthorized):
        policy.authorize('flocx_market:offer:get', creds, creds)
Exemple #12
0
def test_authorized():
    creds = {'roles': ['flocx_market_admin']}
    assert (policy.authorize('flocx_market:offer:get', creds, creds))