UniversalScientificTechnologies / OpenIntranet

Software for warehouse and production management
https://universalscientifictechnologies.github.io/OpenIntranet/
GNU General Public License v3.0
0 stars 2 forks source link

validate keys:vals more specificaly (price > 0 etc #197

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 1 year ago

validate keys:vals more specificaly (price > 0 etc

https://github.com/UniversalScientificTechnologies/OpenIntranet/blob/f0741a81c38ce4a88441c9776db4ced09fbc8ebc/src/OpenIntranet/plugins/order/backend/orders.py#L72


class Order(dict):
    """Order dict can be converted to Order type for convinient methods"""

    def is_valid(self):
        try:
            self.validate()
            return True
        except:
            return False

    def validate(self, id_incluaded:bool=False, date_included:bool=False):
        """
        Raises exeption when order invalid.
        id_icluded: check presence of id
        Whether correct parameters and data values are valid:
        - name
        - description
        - customer (name, other_info)
        - items: list (for each item)
        - prices (price_total, price_to_pay)
        """
        if id_incluaded:
            self.__validate_key_general("_id", ObjectId)
        self.__validate_key_general("name", str)
        self.__validate_key_general("description", str)
        self.__validate_key_general("customer", dict)
        self.__validate_key_general("items", list)
        self.__validate_key_general("price_total", float)
        self.__validate_key_general("price_to_pay", float)

        # TODO validate keys:vals more specificaly (price > 0 etc)

    def set_id(self, id):
        if isinstance(id, str):
            self.update({'_id': ObjectId(id)})
        elif isinstance(id, ObjectId):
            self.update({'_id': id})
        else:
            raise TypeError('Invalid id type given (str of ObjectId required)')

    def get_id_as_str(self, default_ret=None):
        """gets id as str, when not found returns default_ret (None is default)"""
        id = self.get('_id')
        if id is not None and (isinstance(id, ObjectId) or isinstance(id, str)):
            return str(id)
        return default_ret

    def __validate_key_general(self, keyname: str, type):
        """keyname in dict and has given type; raise propper exception if not"""
        value = self.get(keyname)
        if value is None:
            raise KeyError(f"key {keyname} not present in order")
        if not isinstance(value, str):
            raise TypeError(f"Order {keyname} is not {str(type)} type")

class NewOrderFormHandler(BaseHandler):
    def get(self):
        title = "Vytváříte novou objednávku"

22c679797ef32cf322a18b3906308807c12728ac

github-actions[bot] commented 1 year ago

Closed in ee843bc0d9a12bcf18c759c3715f1808aa1c86c7