# Path: utils/database.py
# class Database:
#     """
#     This class handles :
#     - Provides a link and a cursor to the database as class attributes
#     Usage :
#     - Database.on()
#     - Database.CURSOR.execute(query, params)
#     - ... etc
#     - Database.off()
#     Available class attributes :
#     - LINK
#     - CURSOR
#     - FILE (path to the database file)
#     """
#     DATABASE_PRODUCTION = 'database.db'
#     """ Normal database filename """
#     DATABASE_TEST = 'database_test.db'
#     """ Test database filename : this database is temporary """
#     TEST_MODE = False
#     """ Does it need to use the test database ? """
# 
#     @classmethod
#     def on(cls):
#         """
#         Connect to the database
#         :rtype: bool
#         """
#         # Don't reconnect if already connected
#         if cls.is_ready():
#             return False
# 
#         # Filename
#         cls.FILE = cls.DATABASE_PRODUCTION
# 
#         if cls.TEST_MODE:
#             cls.FILE = cls.DATABASE_TEST
# 
#         # Connect
#         cls.LINK = sqlite3.connect(cls.FILE)
#         cls.LINK.row_factory = sqlite3.Row
#         cls.CURSOR = cls.LINK.cursor()
# 
#         return True
# 
#     @classmethod
#     def off(cls):
#         """
#         Ends connection with the database.
#         :rtype: bool
#         """
#         if cls.LINK:
#             cls.LINK.close()
#         cls.LINK = None
#         cls.CURSOR = None
#         return True
# 
#     @classmethod
#     def is_ready(cls):
#         """
#         Is the connection open ?
#         :rtype: bool
#         """
#         return hasattr(cls, 'LINK') and cls.LINK != None


from threading import Thread, RLock
from utils import Database
import requests
import models
        """
        Fetch infos from OpenFoodFacts.
        Automaticaly updates attributes with the results.
        :rtype: bool
        """
        # Fetch
        try:
            url = 'https://world.openfoodfacts.org/api/v0/product/{}.json'
            url = url.format(self.barcode)
            attempt = requests.get(url, timeout=10)
            attempt = attempt.json()
        except Exception as trace:
            return False

        # Do we have a product ?
        if attempt['status'] == 0:
            return False

        # Treat data
        name = ''
        if 'product_name' in attempt['product']:
            name = attempt['product']['product_name']

        if 'brands' in attempt['product']:
            name = name + ' - ' + attempt['product']['brands'].split(',')[0]

        self.name = name

        # Get and save image in assets/static/products,
        # only if it is not the test database

# Path: utils/database.py
# class Database:
#     """
#     This class handles :
#     - Provides a link and a cursor to the database as class attributes
#     Usage :
#     - Database.on()
#     - Database.CURSOR.execute(query, params)
#     - ... etc
#     - Database.off()
#     Available class attributes :
#     - LINK
#     - CURSOR
#     - FILE (path to the database file)
#     """
#     DATABASE_PRODUCTION = 'database.db'
#     """ Normal database filename """
#     DATABASE_TEST = 'database_test.db'
#     """ Test database filename : this database is temporary """
#     TEST_MODE = False
#     """ Does it need to use the test database ? """
# 
#     @classmethod
#     def on(cls):
#         """
#         Connect to the database
#         :rtype: bool
#         """
#         # Don't reconnect if already connected
#         if cls.is_ready():
#             return False
# 
#         # Filename
#         cls.FILE = cls.DATABASE_PRODUCTION
# 
#         if cls.TEST_MODE:
#             cls.FILE = cls.DATABASE_TEST
# 
#         # Connect
#         cls.LINK = sqlite3.connect(cls.FILE)
#         cls.LINK.row_factory = sqlite3.Row
#         cls.CURSOR = cls.LINK.cursor()
# 
#         return True
# 
#     @classmethod
#     def off(cls):
#         """
#         Ends connection with the database.
#         :rtype: bool
#         """
#         if cls.LINK:
#             cls.LINK.close()
#         cls.LINK = None
#         cls.CURSOR = None
#         return True
# 
#     @classmethod
#     def is_ready(cls):
#         """
#         Is the connection open ?
#         :rtype: bool
#         """
#         return hasattr(cls, 'LINK') and cls.LINK != None


# Path: utils/findproduct.py
from threading import Thread, RLock
from utils import Database
import requests
import models

        """
        Fetch infos from OpenFoodFacts.
        Automaticaly updates attributes with the results.
        :rtype: bool
        """
        # Fetch
        try:
            url = 'https://world.openfoodfacts.org/api/v0/product/{}.json'
            url = url.format(self.barcode)
            attempt = requests.get(url, timeout=10)
            attempt = attempt.json()
        except Exception as trace:
            return False

        # Do we have a product ?
        if attempt['status'] == 0:
            return False

        # Treat data
        name = ''
        if 'product_name' in attempt['product']:
            name = attempt['product']['product_name']

        if 'brands' in attempt['product']:
            name = name + ' - ' + attempt['product']['brands'].split(',')[0]

        self.name = name

        # Get and save image in assets/static/products,
        # only if it is not the test database

        if 'image_front_url' in attempt['product'] and not Database.TEST_MODE: