Exemple #1
0
    def last_favorite_substitute(self):
        """
            Method called when last Favorite Substitutes Button is pressed
            Get the index in (list) favorite_substitutes of the first Item in the combobox
            If the index > 0 --> Fill the combox with the last items
            using range Tuple
            Amount of items showed can be configured in the settings
        """
        if self.ui.cb_favorite_substitute.count() > 0:
            first_item = self.ui.cb_favorite_substitute.itemText(0)
            index_in_list = next(
                (index for index, item in enumerate(self.favorite_substitutes)
                 if item.name == first_item),
                -1,
            )

            if index_in_list > 0:
                range = self.get_last_range(
                    index_in_list, CST.NB_OF_FAVORITE_SUBSTITUTE_TO_SHOW)
                favorites_name = [
                    str(favorite) for favorite in
                    self.favorite_substitutes[range[0]:range[1]]
                ]
                widgets.add_list_items_to_combobox(
                    self.ui.cb_favorite_substitute, favorites_name)
Exemple #2
0
    def select_source(self, text):
        """
            Method used when cb_source is activated
            Attributes :receive the Actual text in the combobox

            This method retrieves the category depending of the source
            selected by the user.
            Load the category's name in cb_category Combobox
        """
        if text == "API":
            self.categories = Cat.select_from_api(self.message)
        else:
            self.categories = Cat.select_from_db(self.sql, self.message)
            if len(self.categories) == 0:
                self.categories = Cat.select_from_api(self.message)

        if len(self.categories) > 0:
            self.ui.cb_product.clear()
            self.ui.cb_substitute.clear()
            widgets.empty_table_column(self.ui.tab_product, 1)
            widgets.empty_table_column(self.ui.tab_substitute, 1)
            categories_name = [
                str(category)
                for category in self.categories[:CST.NB_OF_CATEGORY_TO_SHOW]
            ]
            widgets.add_list_items_to_combobox(self.ui.cb_category,
                                               categories_name)
            Message.select(self.message, "category")
Exemple #3
0
    def next_substitute(self):
        """
            Method called when next Substitute Button is pressed
            Get the index in (list) substitutes of the last Item in the combobox
            If the index < len(substitutes) --> Fill the combox with the next items
            using range Tuple.
            Amount of items showed can be configured in the settings
        """
        if (self.ui.cb_category.count() > 0
                and self.ui.cb_category.count() == CST.NB_OF_CATEGORY_TO_SHOW):
            last_item = self.ui.cb_substitute.itemText(
                self.ui.cb_substitute.count() - 1)
            index_in_list = next(
                (index for index, item in enumerate(self.substitutes)
                 if item.name == last_item),
                -1,
            )

            if index_in_list < len(self.substitutes) - 1:
                range = self.get_next_range(len(self.substitutes),
                                            index_in_list,
                                            CST.NB_OF_SUBSTITUTE_TO_SHOW)
                widgets.empty_table_column(self.ui.tab_substitute, 1)
                substitutes_name = [
                    str(substitute)
                    for substitute in self.substitutes[range[0]:range[1]]
                ]
                widgets.add_list_items_to_combobox(self.ui.cb_substitute,
                                                   substitutes_name)
Exemple #4
0
    def last_product(self):
        """
            Method called when last products Button is pressed
            Get the index in (list) productss of the first Item in the combobox
            If the index > 0 --> Fill the combox with the last items
            using range Tuple
            Amount of items showed can be configured in the settings
        """
        if self.ui.cb_product.count() > 0:
            first_item = self.ui.cb_product.itemText(0)
            index_in_list = next(
                (index for index, item in enumerate(self.products)
                 if item.name == first_item),
                -1,
            )

            if index_in_list > 0:
                range = self.get_last_range(index_in_list,
                                            CST.NB_OF_PRODUCT_TO_SHOW)
                self.ui.cb_substitute.clear()
                widgets.empty_table_column(self.ui.tab_product, 1)
                widgets.empty_table_column(self.ui.tab_substitute, 1)
                products_name = [
                    str(product)
                    for product in self.products[range[0]:range[1]]
                ]
                widgets.add_list_items_to_combobox(self.ui.cb_product,
                                                   products_name)
Exemple #5
0
    def next_product(self):
        """
            Method called when next product Button is pressed
            Get the index in (list) products of the last Item in the combobox
            If the index < len(products) --> Fill the combox with the next items
            using range Tuple.
            Amount of items showed can be configured in the settings
        """
        if self.ui.cb_product.count() > 0:
            last_item = self.ui.cb_product.itemText(
                self.ui.cb_product.count() - 1)
            index_in_list = next(
                (index for index, item in enumerate(self.products)
                 if item.name == last_item),
                -1,
            )

            if (index_in_list < len(self.products) - 1 and
                    self.ui.cb_category.count() == CST.NB_OF_CATEGORY_TO_SHOW):
                range = self.get_next_range(len(self.products), index_in_list,
                                            CST.NB_OF_PRODUCT_TO_SHOW)
                self.ui.cb_substitute.clear()
                widgets.empty_table_column(self.ui.tab_product, 1)
                widgets.empty_table_column(self.ui.tab_substitute, 1)
                products_name = [
                    str(product)
                    for product in self.products[range[0]:range[1]]
                ]
                widgets.add_list_items_to_combobox(self.ui.cb_product,
                                                   products_name)
Exemple #6
0
    def select_product(self, text):
        """
        Method used when cb_product is activated
        Attributes :receive the Actual text in the combobox
        Show the product's attribut in tab_product.
        This method retrieves the substitutes depending of the source
        selected by the user.
        Load the substitute's name in cb_substitute Combobox
        """
        if self.ui.cb_product.count() > 0:
            product = next(
                (prod for prod in self.products if prod.name == text), None)
            widgets.populate_tab_column(self.ui.tab_product,
                                        product.list_attributes_to_show(), 1)

            self.substitutes = product.find_substitutes(
                self.sql,
                self.ui.cb_source.currentText(),
                self.ui.cb_category.currentText(),
            )
            if len(self.substitutes) > 0:
                widgets.empty_table_column(self.ui.tab_substitute, 1)
                substitutes_name = [
                    str(substitute) for substitute in
                    self.substitutes[:CST.NB_OF_SUBSTITUTE_TO_SHOW]
                ]
                widgets.add_list_items_to_combobox(self.ui.cb_substitute,
                                                   substitutes_name)
                Message.select(self.message, "substitute")
Exemple #7
0
    def select_category(self):
        """
            Method used when cb_category is activated

            This method retrieves the product depending of the source
            selected by the user.
            Load the product's name in cb_producty Combobox
        """
        if self.ui.cb_category.count() > 0:
            category = self.ui.cb_category.currentText()
            if self.ui.cb_source.currentText() == "API":
                self.products = Prod.select_from_api(self.message, self.sql,
                                                     category)
            else:
                self.products = Prod.select_from_db(self.sql, self.message,
                                                    category)
                if len(self.products) == 0:
                    self.products = Prod.select_from_api(
                        self.message, self.sql, category)

            if len(self.products) > 0:
                self.ui.cb_substitute.clear()
                widgets.empty_table_column(self.ui.tab_product, 1)
                widgets.empty_table_column(self.ui.tab_substitute, 1)
                products_name = [
                    str(product)
                    for product in self.products[:CST.NB_OF_PRODUCT_TO_SHOW]
                ]
                widgets.add_list_items_to_combobox(
                    self.ui.cb_product,
                    products_name,
                )
                Message.select(self.message, "product")
Exemple #8
0
 def add_favorite_to_combobox(self):
     """
         Add names of favorite_substitutes list to a combobox
     """
     if len(self.favorite_substitutes) > 0:
         favorites_name = [
             str(favorite) for favorite in self.favorite_substitutes[0:50]
         ]
         widgets.add_list_items_to_combobox(self.ui.cb_favorite_substitute,
                                            favorites_name)
Exemple #9
0
    def save_substitute(self):
        """
            Method to save favorite substitute in the database

            Select Product from cb_product and convort to Product Object
            Select Substitute from cb_substitute and convort to Substitute (Product) Object

            Add favorite to cb_favorite_substitutes if saved worked
        """
        if len(self.substitutes) > 0 and self.ui.cb_substitute.currentText(
        ) != "":

            product = next(
                (prod for prod in self.products
                 if prod.name == self.ui.cb_product.currentText()),
                None,
            )
            substitute = next(
                (substitute for substitute in self.substitutes
                 if substitute.name == self.ui.cb_substitute.currentText()),
                None,
            )
            favorite = Favorite.add_favorite_substitute(
                self.sql,
                self.message,
                self.ui.cb_category.currentText(),
                product,
                substitute,
            )

            if favorite is not None:
                Message.favorite_saved(self.message)
                self.favorite_substitutes.append(favorite)
                favorites_name = [
                    str(favorite) for favorite in self.
                    favorite_substitutes[:CST.
                                         NB_OF_FAVORITE_SUBSTITUTE_TO_SHOW]
                ]
                widgets.add_list_items_to_combobox(
                    self.ui.cb_favorite_substitute, favorites_name)