Ejemplo n.º 1
0
def create_product(shop):
    category = Category()
    category.title = 'test_title'
    category.description = 'test_description'
    category.shop = shop.id
    category.save()
    product = Product()
    product.title = 'test_title'
    product.description = 'test_description'
    product.category = category.id
    product.price = 1000
    product.image = 'test_image'
    product.save()
    return product
Ejemplo n.º 2
0
 def test_create(self):
     """
     Test a category creation
     """
     shop = create_shop()
     category = Category()
     category.title = 'test_title'
     category.description = 'test_description'
     category.shop = shop.id
     category.save()
     storage.close()
     savedCat = shop.categories[0]
     self.assertIsNotNone(savedCat)
     us = storage.get(User, shop.user)
     storage.delete(us)
     storage.save()
     storage.close()
Ejemplo n.º 3
0
 def test_products(self):
     """
     Test the products @property
     """
     shop = create_shop()
     category = Category()
     category.title = 'test_title'
     category.description = 'test_description'
     category.shop = shop.id
     category.save()
     product = Product()
     product.title = 'test_title'
     product.description = 'test_description'
     product.price = 1000
     product.category = category.id
     product.image = 'test_image'
     product.save()
     storage.close()
     savedProd = category.products[0]
     self.assertEqual(savedProd.id, product.id)
     us = storage.get(User, shop.user)
     storage.delete(us)
     storage.save()
     storage.close()
Ejemplo n.º 4
0
def create_category():
    """
    Creates a new category linked to a shop
    create a shop if the user doesn't have one
    ---
    parameters:
      - in: header
        name: authorization
        required: true
        schema:
          type: string
      - in: query
        name: title
        required: true
        schema:
          type: string
      - in: query
        name: description
        required: true
        schema:
          type: string
    responses:
      200:
        description: the category was saved succefully
        schema:
          type: object
          parameters:
            id:
              type: string
              example: 1239102941-1231290412-dfs12rf-123re-1234r223
      400:
        description: incomplete request
        schema:
          type: object
          parameters:
            message:
              type: string
              example: incomplete request
    """
    state = request.get_json()
    user = request.user
    # tests the input values
    print(state)
    if not state['title'] or not state['description']:
        return jsonify(message="incomplete request"), 400
    # verify the store for the user
    shops = storage.filter_by(Shop, 'user', user)
    shop = None
    for shp in shops:
        if shp.user == user:
            shop = shp
    # if shop doesn't exists create a new instance
    if not shop:
        shop = Shop()
        shop.user = user
        shop.save()
    # Create the Category instance
    category = Category()
    category.title = state['title']
    category.description = state['description']
    category.shop = shop.id
    category.save()
    return jsonify(id=category.id)