コード例 #1
0
 def test_get_customer_promo_list(self):
     """ Get a list of Customers with given promo status TRUE """
     customer = Customer.find_by_promo(True)
     resp = self.app.get('/customers?promo=true')
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
     data = json.loads(resp.data)
     self.assertTrue(len(data) == 1)
コード例 #2
0
def list_customers():
    # yml files start with '---'
    # definitions can only define once, then you can use $ref to use it
    """
    Retrieve a list of Customers
    This endpoint will return all Customers unless a query parameter is specificed
    ---
    tags:
      - Customers
    description: The Customers endpoint allows you to query Customers
    parameters:
      - name: username
        in: query
        description: the username of Customer you are looking for
        required: false
        type: string
      - name: firstname
        in: query
        description: the firstname of Customer you are looking for
        required: false
        type: string
      - name: lastname
        in: query
        description: the lastname of Customer you are looking for
        required: false
        type: string
      - name: email
        in: query
        description: the email of Customer you are looking for
        required: false
        type: string
      - name: active
        in: query
        description: the active status of Customer you are looking for
        required: false
        type: string
      - name: promo
        in: query
        description: the promotion status of Customer you are looking for
        required: false
        type: string
    definitions:
      Customer:
        type: object
        properties:
          id:
            type: integer
            description: unique id assigned internallt by service
          username:
            type: string
            description: the customer's username
          password:
            type: string
            description: the customer's password
          firstname:
            type: string
            description: the customer's firstname
          lastname:
              type: string
              description: the customer's lastname
          address:
            type: string
            description: the customer's address
          phone:
            type: string
            description: the customer's phone number
          email:
            type: string
            description: the customer's email
          active:
            type: boolean
            description: the active status of a customer
          promo:
            type: boolean
            description: the promotion status of a customer
    responses:
      200:
        description: An array of Customers
        schema:
          type: array
          items:
            schema:
              $ref: '#/definitions/Pet'
    """

    customers = []
    username = request.args.get('username')
    email = request.args.get('email')
    firstname = request.args.get('firstname')
    lastname = request.args.get('lastname')
    promos = request.args.get('promo')
    actives = request.args.get('active')

    if username:
        customers = Customer.find_by_username(username)
        if not customers:
            raise NotFound(
                "Customer with username '{}' was not found.".format(username))
    elif email:
        customers = Customer.find_by_email(email)
        if not customers:
            raise NotFound(
                "Customer with email '{}' was not found.".format(email))
    elif firstname:
        customers = Customer.find_by_firstname(firstname)
        if not customers:
            raise NotFound(
                "Customer with firstname '{}' was not found.".format(
                    firstname))
    elif lastname:
        customers = Customer.find_by_lastname(lastname)
        if not customers:
            raise NotFound(
                "Customer with lastname '{}' was not found.".format(lastname))
    elif promos:
        promos = promos.lower()
        if promos == 'true':
            promo = True
        elif promos == 'false':
            promo = False
        else:
            raise ValueError
        customers = Customer.find_by_promo(promo)
        if not customers:
            raise NotFound(
                "Customer with promotion status '{}' was not found.".format(
                    promo))
    elif actives:
        actives = actives.lower()
        if actives == 'true':
            active = True
        elif actives == 'false':
            active = False
        else:
            raise ValueError
        customers = Customer.find_by_active(active)
        if not customers:
            raise NotFound(
                "Customer with active status '{}' was not found.".format(
                    active))
    else:
        customers = Customer.all()

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