Esempio n. 1
0
def get_product(book, gui):
    '''Get product form Woo'''

    dictionary = {}

    try:
        # Get product ID from DB.
        mysql_request = MySQL(isbn=book)
        mysql_response = mysql_request.db_mysql()

        if mysql_response:
            product = WooCommerce(book=book)
            request = product.get_woo_product(mysql_response)

            if request:
                try:
                    dictionary["id"] = request["id"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    for attribute in request["attributes"]:
                        dictionary[product.get_translation(attribute["name"],
                                                           "en")] = product.list_expander(attribute["options"]).replace("amp;", "")  # pylint: disable=line-too-long
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    dictionary["name"] = request["name"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    dictionary["description"] = request["description"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    categories_list = []
                    categories = request["categories"]
                    for category in categories:
                        categories_list.append(category["name"].replace(
                            "amp;", ""))

                    dictionary["categories"] = categories_list
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    tags_list = []
                    tags = request["tags"]
                    for tag in tags:
                        tags_list.append(tag["name"].replace("amp;", ""))

                    dictionary["tags"] = tags_list
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    dictionary["image"] = request["images"][0]["src"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    dictionary["price"] = request["regular_price"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    dictionary["sale_price"] = request["sale_price"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                try:
                    dictionary["amount"] = request["stock_quantity"]
                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                for key in gui:
                    if "_box" in key and gui[key]:
                        if key.split('_box')[0] not in dictionary:
                            dictionary[key.split('_box')[0]] = None

                        if dictionary[key.split('_box')[0]] == "":
                            dictionary[key.split('_box')[0]] = None

    except Exception as error:  # pylint: disable=broad-except
        logger.info(error)

    return dictionary
Esempio n. 2
0
    def post_woo_products(self):
        '''Post WooCommerce product'''
        try:
            # Auth
            auth = self.get_woo_request()
            # Upload image to media
            image = wp(self.book["image"])

            data = {
                "name":
                self.book["name"],
                "description":
                self.book["description"],
                "sku":
                self.book["isbn"],
                "categories": [],
                "tags": [],
                "attributes": [
                    {
                        "id": 1,
                        "name": "Tytuł",  # cspell: disable-line
                        "position": 1,
                        "visible": True,
                        "variation": True,
                        "options": [self.book["title"]]
                    },
                    {
                        "id": 2,
                        "name": "Autor",  # cspell: disable-line
                        "position": 2,
                        "visible": True,
                        "variation": True,
                        "options": [self.book["authors"]]
                    },
                    {
                        "id": 3,
                        "name": "Wydawnictwo",  # cspell: disable-line
                        "position": 3,
                        "visible": True,
                        "variation": True,
                        "options": [self.book["publisher"]]
                    },
                    {
                        "id": 4,
                        "name": "Rok wydania",  # cspell: disable-line
                        "position": 4,
                        "visible": True,
                        "variation": True,
                        "options": [self.book["publish_date"]]
                    },
                    {
                        "id": 5,
                        "name": "Okładka",  # cspell: disable-line
                        "position": 5,
                        "visible": True,
                        "variation": True,
                        "options": [self.book["binding"]]
                    },
                    {
                        "id": 6,
                        "name": "ISBN",
                        "position": 6,
                        "visible": True,
                        "variation": True,
                        "options": [self.book["isbn"]]
                    }
                ]
            }

            # Tags
            try:
                if self.book["tags"]:
                    tags = self.validate_tags()
                    for tag in tags:
                        data["tags"].append({'id': tag})
            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            # Image
            try:
                if image:
                    data["images"] = [{"src": image}]
            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            # Price
            try:
                if self.book["price"]:
                    data["regular_price"] = self.book["price"]
            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            # Sale Price
            try:
                if self.book["sale_price"]:
                    data["sale_price"] = self.book["sale_price"]
            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            # Amount
            try:
                if self.book["amount"]:
                    data["manage_stock"] = True
                    data["stock_quantity"] = self.book["amount"]
            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            # Get category ID
            try:
                categories = self.validate_category()
                for category in categories:
                    data["categories"].append({'id': category})
            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            # Send request
            response = auth.post("products", data).json()

            # Send none if status code found in error codes
            if "data" in response:
                if response.get("data", {}).get("status") in self.error_codes:
                    self.error_catch.append(inspect.getouterframes(inspect.currentframe())[0].function)  # pylint: disable=line-too-long
                    return None

            # Format output
            try:
                output = {
                    'id': response["id"],
                    'name': response["name"],
                    'link': response["permalink"],
                    'source': False
                }

            except Exception as error:  # pylint: disable=broad-except
                logger.info(error)

            if response["data"]["status"] == 400:

                try:
                    mysql_request = MySQL(isbn=self.book["isbn"])
                    request = mysql_request.db_mysql()

                except Exception as error:  # pylint: disable=broad-except
                    logger.info(error)

                if request:
                    product = self.get_woo_product(request)
                    if product["stock_quantity"]:
                        data["stock_quantity"] = int(data["stock_quantity"]) + product["stock_quantity"]  # pylint: disable=line-too-long

                    try:
                        response = self.update_woo_products(
                            product["id"], data)
                        output = {
                            'id': response["id"],
                            'name': response["name"],
                            'link': response["permalink"],
                            'source': True
                        }

                        return output

                    except Exception as error:  # pylint: disable=broad-except
                        logger.info(error)
                else:
                    return None

        except Exception as error:  # pylint: disable=broad-except
            logger.info(error)
        return output