def checkout(self, _id):
        """
        Checkout a created basket, this will set the status to complete.

        :param _id: basket id to lookup for cancelation. \n
        :type _id: string. \n
        :returns: True if success else False. \n
        :rtype: boolean.
        """
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                _filter = {"_id": ObjectId(_id)}
                update = {"state": BasketState.CLOSED.name}
                document = client.farmers.basket.find_one_and_update(
                    _filter, {"$set": update},
                    return_document=ReturnDocument.AFTER)
                if document:
                    return True
        except errors.DuplicateKeyError as duplicate_key_error:
            self.logger.error(duplicate_key_error)
            return False
        self.logger.error("Could not set basket to checkout state!")
        return False
    def create(self, codes):
        """
        Creates a basket with a list of item codes, sets the date 
        it was created and sets the status to open.

        :param codes: Item codes from inventory. \n
        :type codes: list. \n
        :returns: True if success else False. \n
        :rtype: boolean.
        """
        basket = None
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                document = {
                    "items": codes,
                    "promos": [],
                    "state": BasketState.OPEN.name,
                    "created": datetime.datetime.utcnow()
                }
                basket = client.farmers.basket.save(document)
                if basket:
                    return basket
                self.logger.error("Error in creating basket!")
        except errors.DuplicateKeyError as duplicate_key_error:
            self.logger.error(duplicate_key_error)
        return None
    def additem(self, _id, code):
        """
        Adds item by code to basket.

        :param _id: basket id. \n
        :type _id: string. \n
        :param code: Code of item from inventory to add to bucket. \n
        :type code: string. \n
        :returns: True if success else False. \n
        :rtype: boolean.
        """
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            updated = None
            with connection_factory.get_connection() as client:
                _filter = {"_id": ObjectId(_id)}
                basket = client.farmers.basket.find_one(_filter)
                if basket:
                    items = basket["items"]
                    items.append(code)
                    update = {"items": items}
                    updated = client.farmers.basket.find_one_and_update(
                        _filter, {"$set": update},
                        return_document=ReturnDocument.AFTER)
            if updated:
                return True
            self.logger.error("Could not update basket")
        except Exception as exception:
            self.logger.error(exception)
        return False
Beispiel #4
0
    def read(self, code=None, name=None):
        """
        Read an item from inventory using code or name.

        :param code: item code. \n
        :type code: string. \n
        :param name: item name. \n
        :type name: string. \n
        :raises:
          - ValueError: if neither code nor name is supplied.
        :returns: dictionary composed of code, name and price or None if it can't be found. \n
        :rypte: `dict`.
        """
        if not code and not name:
            raise ValueError("Need to supply either item code or name!")
        item = None
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                if code:
                    _filter = {"code": code}
                else:
                    _filter = {"name": name}
                item = client.farmers.inventory.find_one(_filter)
                if item:
                    item.pop("_id")
        except Exception as exception:
            self.logger.error(exception)
            return item
        return item
Beispiel #5
0
    def delete(self, code=None, name=None):
        """
        Delete an item from inventory using code or name.

        :param code: item code. \n
        :type code: string. \n
        :param name: item name. \n
        :type name: string. \n
        :raises:
          - ValueError: if neither code nor name is supplied.
        :returns: True if succes else False. \n
        :rtype: boolean.
        """
        if not code and not name:
            raise ValueError("Need to supply either item code or name!")
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                if code:
                    _filter = {"code": code}
                else:
                    _filter = {"name": name}
                result = client.farmers.inventory.delete_one(_filter)
                if result and result.deleted_count == 1:
                    return True
        except Exception as exception:
            self.logger.error(exception)
            return False
        return False
Beispiel #6
0
    def read(self):
        """
        Read all items from inventory.

        :returns: dictionary composed of code(key), name and price. \n
        :rypte: `dict`.
        """
        items = {}
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                for item in client.farmers.inventory.find():
                    items[item["code"]] = {
                        "name": item["name"],
                        "price": item["price"]
                    }
        except Exception as exception:
            self.logger.error(exception)
            return items
        return items
    def removeitem(self, _id, code):
        """
        Removes item by code from basket.

        :param _id: basket id to lookup. \n
        :type _id: string. \n
        :param code: Code of item from inventory to remove from bucket. \n
        :type code: string. \n
        :returns: True if success else False. \n
        :rtype: boolean.
        """
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            updated = None
            with connection_factory.get_connection() as client:
                _filter = {"_id": ObjectId(_id)}
                basket = client.farmers.basket.find_one(_filter)
                if basket:
                    if basket["state"] != BasketState.OPEN.name:
                        self.logger.error(
                            "Cannot change the item list of a basket that is not opened."
                        )
                        return False
                    items = basket["items"]
                    if code in items:
                        items.remove(code)
                        update = {"items": items}
                        updated = client.farmers.basket.find_one_and_update(
                            _filter, {"$set": update},
                            return_document=ReturnDocument.AFTER)
                    else:
                        self.logger.error("Code not in items list.")
            if updated:
                return True
            self.logger.error("Could not remove item from basket")
        except Exception as exception:
            self.logger.error(exception)
        return False
    def read(self, _id):
        """
        Lookup a basket and return its contents.

        :param _id: basket id to lookup. \n
        :type _id: string. \n
        :returns: Dictionary of basket contents. \n
        :rtype: dict.
        """
        basket = None
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                _filter = {"_id": ObjectId(_id)}
                basket = client.farmers.basket.find_one(_filter)
                if basket:
                    return basket
                self.logger.error("Could not find basket with id %s", _id)
        except Exception as exception:
            self.logger.error(exception)
        return None
Beispiel #9
0
    def create(self, code, name, price):
        """
        Create an item in inventory.

        :param code: item code. \n
        :type code: string. \n
        :param name: item name. \n
        :type name: string. \n
        :param price: item price. \n
        :type price: \n
        :returns: True if success else False. \n
        :rypte: boolean.
        """
        connection_factory = factory.connection_factory(
            self.connection_factory_type)
        try:
            with connection_factory.get_connection() as client:
                document = {"code": code, "name": name, "price": price}
                client.farmers.inventory.save(document)
        except errors.DuplicateKeyError as duplicate_key_error:
            self.logger.error(duplicate_key_error)
            return False
        return True
Beispiel #10
0
    def update(self, code, name=None, price=None):
        """
        Updates an item from inventory using name or price or both.

        :param code: item code. \n
        :type code: string. \n
        :param name: item name. \n
        :type name: string. \n
        :raises:
          - ValueError: if neither code nor name is supplied.
        :returns: dictionary composed of code, name and price or None if it can't be found. \n
        :rypte: `dict`.
        """
        if not code and not name:
            raise ValueError("Need to supply either item code or name!")
        document = None
        try:
            connection_factory = factory.connection_factory(
                self.connection_factory_type)
            with connection_factory.get_connection() as client:
                _filter = {"code": code}
                if name and price:
                    update = {"name": name, "price": float(price)}
                elif name:
                    update = {"name": name}
                else:
                    update = {"price": float(price)}
                document = client.farmers.inventory.find_one_and_update(
                    _filter, {"$set": update},
                    return_document=ReturnDocument.AFTER)
                if document:
                    document.pop("_id")
        except Exception as exception:
            self.logger.error(exception)
            return document
        return document