Exemple #1
0
 def _get_product(category, json_product):
     '''Gets a cleaned product from a product in json format.'''
     product = {
         'product_id': None,
         'name': None,
         'category_id': category.category_id,
         'description': '',
         'nutri_score': None,
         'stores': '',
         'url': None
     }
     product = Product(product)
     if 'product_name_fr' in json_product:
         if json_product['product_name_fr'] != '':
             product.name = json_product['product_name_fr']
     elif 'product_name' in json_product:
         if json_product['product_name'] != '':
             product.name = json_product['product_name']
     if 'generic_name_fr' in json_product:
         product.description = json_product['generic_name_fr']
     elif 'generic_name' in json_product:
         product.description = json_product['generic_name']
     if 'nutrition_grade_fr' in json_product:
         if json_product['nutrition_grade_fr'] in 'abcdeABCDE':
             product.nutri_score = json_product['nutrition_grade_fr']
     if 'stores' in json_product:
         product.stores = json_product['stores']
     if 'url' in json_product:
         url = json_product['url'].lower()
         if 'https://' in url and '.openfoodfacts.org/' in url:
             product.url = json_product['url']
     if not (product.name and product.nutri_score and product.url):
         product = None
     return product
Exemple #2
0
def create_product(name: str, value: str, description: str,
                   img_link: str) -> Product:
    product = Product()
    product.name = name
    product.value = value
    product.description = description
    product.img_link = img_link
    return product
Exemple #3
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
 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()
Exemple #5
0
def create_product():
    """
    Creates a new product linked to a category
    ---
    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
      - in: query
        name: category
        required: true
        schema:
          type: string
      - in: query
        name: price
        required: true
        schema:
          type: integer
      - in: query
        name: category
        required: true
        schema:
          type: atring
      - in: query
        name: image
        required: true
        schema:
          type: string
    responses:
      200:
        description: success at storing the new Product
        schema:
          type: object
          parameters:
            id:
              type: string
              example: 23refe34r23-4r234r-234r234r234r-r23423r4
            shop:
              type: boolean
              example: true
    """
    user = storage.get(User, request.user)
    state = request.get_json()
    product = Product()
    product.title = state['title']
    product.description = state['description']
    product.category = state['category']
    product.price = str(state['price'])
    # check the values
    imagePath = '/usr/src/app/api/v1/shop/images/{}.b64'.format(product.id)
    print(request.user)
    with open(imagePath, 'w') as imageFile:
        imageFile.write(state['image'])
    product.image = imagePath
    product.save()
    # print(state)
    if not user.shops[0].country:
        return jsonify(shop='true')
    return jsonify(id=product.id)