예제 #1
0
def list_promotions():
    r""" Returns all of the Promotions

    Search the promotions in the database.
    <ul>
    <li>Only the first non-empty field will be considered.</li>
    <li>An empty query will result in a list of all promotion entries in the database</li>
    </ul>
    ---
    tags:
    - promotions
    produces:
    - application/json
    parameters:
    - name: name
      in: query
      description: name of the promotion
      required: false
      type: string
    - name: product_id
      in: query
      description: product_id for the promotion
      required: false
      type: integer
      minimum: 0
      format: int32
    - name: discount_ratio
      in: query
      description: discount ratio of the promotion
      required: false
      type: integer
      maximum: 100.0
      minimum: 0
      format: int32
    responses:
      200:
        description: search results matching criteria
        schema:
          type: array
          items:
            $ref: '#/definitions/ResponsePromotionObject'
      400:
        description: bad input parameter
    """
    promotions = []
    promotion_id = request.args.get('promotion_id')
    name = request.args.get('name')
    product_id = request.args.get('product_id')
    discount_ratio = request.args.get('discount_ratio')
    if name:
        promotions = Promotion.find_by_name(name)
    elif product_id:
        promotions = Promotion.find_by_product_id(product_id)
    elif discount_ratio:
        promotions = Promotion.find_by_discount_ratio(discount_ratio)
    else:
        promotions = Promotion.all()

    results = [promotion.serialize() for promotion in promotions]
    return make_response(jsonify(results), status.HTTP_200_OK)
예제 #2
0
 def test_find_by_discount_ratio(self):
     """ Find a Promotion by Discount ratio """
     Promotion(name="20%OFF", product_id=9527, discount_ratio=80).save()
     Promotion(name="50%OFF", product_id=26668).save()
     promotions = Promotion.find_by_discount_ratio(80)
     Promotion.logger.info(promotions)
     self.assertEqual(promotions[0].product_id, 9527)
     self.assertEqual(promotions[0].name, "20%OFF")
예제 #3
0
def list_promotions():
    """ Returns all of the Promotions """
    promotions = []
    promotion_id = request.args.get('promotion_id')
    name = request.args.get('name')
    product_id = request.args.get('product_id')
    discount_ratio = request.args.get('discount_ratio')
    if name:
        promotions = Promotion.find_by_name(name)
    elif product_id:
        promotions = Promotion.find_by_product_id(product_id)
    elif discount_ratio:
        promotions = Promotion.find_by_discount_ratio(discount_ratio)
    else:
        promotions = Promotion.all()

    results = [promotion.serialize() for promotion in promotions]
    return make_response(jsonify(results), status.HTTP_200_OK)