Example #1
0
    def get_products(self):
        """Get products from OpenFoodFacts return a list of raw products."""

        style = color.make_style()
        stdout = base.OutputWrapper(sys.stdout)

        parameters = {
            "json": True,
            "action": "process",
            "page_size": self.PAGE_SIZE,
            "sort_by": self.SORT_BY,
            "tagtype_0": "status",
            "tag_contains_0": "without",
            "tag_0": "to-be-completed",
            "tagtype_1": "status",
            "tag_contains_1": "without",
            "tag_1": "to-be-checked",
            "fields": ",".join(self.FIELDS),
        }

        headers = {
            "user-agent":
            "PurBeurreOC - Web - Version 0.1.0 - projet-11.ojardias.io",
        }

        products = list()
        stdout.write("==== Download products from OpenFoodFacts ====")
        for page in range(self.PAGES):
            stdout.write(f"Downloading page {page}... ")

            parameters["page"] = page

            try:
                response = requests.get(self.URL_BASE,
                                        params=parameters,
                                        headers=headers)
                response.raise_for_status()
            except requests.HTTPError as err:
                stdout.write(style.ERROR("ERROR"))
                raise err
            except requests.ConnectionError as err:
                stdout.write(style.ERROR("ERROR"))
                raise err
            except requests.Timeout as err:
                stdout.write(style.ERROR("ERROR"))
                raise err

            result = response.json()

            if result.get("products"):
                products.extend(result["products"])
                stdout.write(style.SUCCESS("DONE"))
            else:
                stdout.write(style.WARNING("FAIL"))

        return products
Example #2
0
    def feed_db(self, raw_products):

        style = color.make_style()
        stdout = base.OutputWrapper(sys.stdout)

        self._clear_db()

        # Insert each product in the application's database
        stdout.write("====   Insert products in the database    ====")
        stdout.write("Inserting products in the database...")
        for raw_product in raw_products:
            serialized_product = self._serialize_product(raw_product)

            try:
                product = Product(
                    code=serialized_product["code"],
                    name=serialized_product["name"],
                    url=serialized_product["url"],
                    nutriscore_grade=serialized_product["nutriscore_grade"],
                    image_url=serialized_product["image_url"],
                    image_small_url=serialized_product["image_small_url"],
                    salt_100=serialized_product["salt_100"],
                    sugars_100=serialized_product["sugars_100"],
                    saturated_fat_100=serialized_product["saturated_fat_100"],
                    fat_100=serialized_product["fat_100"],
                )
                product.full_clean()
            except ValidationError:
                # Ignore products with ValidationError
                continue
            product.save()

            # Insert associated categories, stores and brands
            for category in serialized_product["categories"]:
                try:
                    obj = Category.objects.get(name=category)
                except ObjectDoesNotExist:
                    try:
                        obj = Category(name=category)
                        obj.full_clean()
                    except ValidationError:
                        # Ignore categories with ValidationError
                        continue
                    obj.save()

                product.categories.add(obj)
        stdout.write(style.SUCCESS("DONE"))
        stdout.write("==============================================")
Example #3
0
 def test_server_formatter_styles(self):
     color_style = color.make_style('')
     formatter = ServerFormatter()
     formatter.style = color_style
     log_msg = 'log message'
     status_code_styles = [
         (200, 'HTTP_SUCCESS'),
         (100, 'HTTP_INFO'),
         (304, 'HTTP_NOT_MODIFIED'),
         (300, 'HTTP_REDIRECT'),
         (404, 'HTTP_NOT_FOUND'),
         (400, 'HTTP_BAD_REQUEST'),
         (500, 'HTTP_SERVER_ERROR'),
     ]
     for status_code, style in status_code_styles:
         record = logging.makeLogRecord({'msg': log_msg, 'status_code': status_code})
         self.assertEqual(formatter.format(record), getattr(color_style, style)(log_msg))
     record = logging.makeLogRecord({'msg': log_msg})
     self.assertEqual(formatter.format(record), log_msg)
Example #4
0
 def test_server_formatter_styles(self):
     color_style = color.make_style('')
     formatter = ServerFormatter()
     formatter.style = color_style
     log_msg = 'log message'
     status_code_styles = [
         (200, 'HTTP_SUCCESS'),
         (100, 'HTTP_INFO'),
         (304, 'HTTP_NOT_MODIFIED'),
         (300, 'HTTP_REDIRECT'),
         (404, 'HTTP_NOT_FOUND'),
         (400, 'HTTP_BAD_REQUEST'),
         (500, 'HTTP_SERVER_ERROR'),
     ]
     for status_code, style in status_code_styles:
         record = logging.makeLogRecord({'msg': log_msg, 'status_code': status_code})
         self.assertEqual(formatter.format(record), getattr(color_style, style)(log_msg))
     record = logging.makeLogRecord({'msg': log_msg})
     self.assertEqual(formatter.format(record), log_msg)
Example #5
0
 def test_server_formatter_styles(self):
     color_style = color.make_style("")
     formatter = ServerFormatter()
     formatter.style = color_style
     log_msg = "log message"
     status_code_styles = [
         (200, "HTTP_SUCCESS"),
         (100, "HTTP_INFO"),
         (304, "HTTP_NOT_MODIFIED"),
         (300, "HTTP_REDIRECT"),
         (404, "HTTP_NOT_FOUND"),
         (400, "HTTP_BAD_REQUEST"),
         (500, "HTTP_SERVER_ERROR"),
     ]
     for status_code, style in status_code_styles:
         record = logging.makeLogRecord({
             "msg": log_msg,
             "status_code": status_code
         })
         self.assertEqual(formatter.format(record),
                          getattr(color_style, style)(log_msg))
     record = logging.makeLogRecord({"msg": log_msg})
     self.assertEqual(formatter.format(record), log_msg)
Example #6
0
"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""

import os
from django.core.management.color import make_style

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_wsgi_application()

style = make_style()
print(style.SUCCESS('To access Camagru, go to: http://localhost:8000/'))