Example #1
0
    def save_brand(self, obj):
        table = 'brewoptix-brands'

        check_for_required_keys(obj, brand_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, brand_attributes)

        obj['user_id'] = self._user_id

        # check if brand with same name already exists
        query = {
            'KeyConditionExpression':
            Key('name').eq(obj['name']),
            'FilterExpression':
            Attr('latest').eq(True) & Attr('active').eq(True)
            & Attr('supplier_id').eq(obj['supplier_id']),
            'IndexName':
            'by_brand_name'
        }

        # only gets partition keys
        response = self._storage.get_items(table, query)

        if response["Count"] > 0:
            if 'entity_id' in obj:
                # filter the current obj from the list. The list always returns the current obj in this case
                if len([
                        item for item in response['Items']
                        if item['entity_id'] != obj['entity_id']
                ]) > 0:
                    raise CannotModifyEntityStates
            else:
                raise CannotModifyEntityStates

        # upload logo png to s3 bucket
        image_string = None
        if "logo" in obj:
            image_string = obj.pop("logo")

        brand_obj = self._storage.save(table, obj)
        self.sns_publish("brands", obj)  # publish notification

        brand = clean(brand_obj)

        if image_string:
            logo_filepath = self.base64_to_png(image_string)

            if 'S3_ENDPOINT' in os.environ:
                s3 = boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT'])
            else:
                s3 = boto3.client('s3')

            bucket_name = os.environ['S3_UPLOADS_BUCKET_NAME']

            s3_filepath = "{SUPPLIER_ID}/brands/{BRAND_ID}/logo.png".format(
                SUPPLIER_ID=obj["supplier_id"],
                BRAND_ID=brand_obj["entity_id"])
            s3.upload_file(logo_filepath, bucket_name, s3_filepath)

        return brand
Example #2
0
    def save_distributor_supplier(self, obj):
        table = 'brewoptix-distributor-suppliers'

        check_for_required_keys(obj, distributor_suppliers_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, distributor_suppliers_attributes)

        obj['user_id'] = self._user_id

        distributor_obj = self._storage.save(table, obj)

        distributor = clean(distributor_obj)

        return distributor
    def save_retail_package(self, obj):
        table = 'brewoptix-retail-packages'

        check_for_required_keys(obj, retail_package_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, retail_package_attributes)

        obj['user_id'] = self._user_id

        retail_package_obj = self._storage.save(table, obj)
        self.sns_publish("retail-packages", obj)  # publish notification

        retail_package = clean(retail_package_obj)

        return retail_package
    def save_supplier_distributor(self, obj):
        table = 'brewoptix-supplier-distributors'

        check_for_required_keys(obj, supplier_distributors_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, supplier_distributors_attributes)

        obj['user_id'] = self._user_id

        distributor_obj = self._storage.save(table, obj)
        self.sns_publish("supplier-distributors", obj)  # publish notification

        distributor = clean(distributor_obj)

        return distributor
Example #5
0
    def save_product(self, obj):
        table = 'brewoptix-products'

        check_for_required_keys(obj, product_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, product_attributes)

        obj['user_id'] = self._user_id

        product_type_obj = self._storage.save(table, obj)
        self.sns_publish("products", obj)  # publish notification

        product_type = clean(product_type_obj)

        return product_type
Example #6
0
    def save_merchandise(self, obj):
        table = 'brewoptix-merchandise'

        check_for_required_keys(obj, merchandise_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, merchandise_attributes)

        obj['user_id'] = self._user_id

        merchandise_obj = self._storage.save(table, obj)

        self.sns_publish("merchandise", obj)  # publish notification

        merchandise = clean(merchandise_obj)

        return merchandise
Example #7
0
    def save_container(self, obj):
        table = 'brewoptix-containers'

        check_for_required_keys(obj, container_attributes)

        # check if content datatype is right
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, container_attributes)

        obj['user_id'] = self._user_id

        container_obj = self._storage.save(table, obj)
        self.sns_publish("containers", obj)  # publish notification

        container = clean(container_obj)

        return container
Example #8
0
    def save_on_hand(self, obj):
        table = 'brewoptix-on-hand-inventory'

        check_for_required_keys(obj, on_hand_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, on_hand_attributes)

        obj['user_id'] = self._user_id

        obj['observation_date'] = maya.parse(obj['observation_date']).epoch

        on_hand_obj = self._storage.save(table, obj)
        self.sns_publish("on-hand", obj)  # publish notification

        on_hand = clean(on_hand_obj)
        on_hand['observation_date'] = datetime.utcfromtimestamp(
            on_hand['observation_date']).isoformat().split('T')[0]

        return on_hand
Example #9
0
    def save_production(self, obj):
        table = 'brewoptix-production'

        check_for_required_keys(obj, production_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, production_attributes)

        obj['user_id'] = self._user_id

        # convert production_date to epoch integer
        obj['production_date'] = maya.parse(obj['production_date']).epoch

        production_obj = self._storage.save(table, obj)
        self.sns_publish("production", obj)  # publish notification

        production = clean(production_obj)
        production['production_date'] = datetime.utcfromtimestamp(
            production['production_date']).isoformat().split('T')[0]
        return production
    def save_count(self, obj):
        obj_type = 'counts'

        check_for_required_keys(obj, count_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, count_attributes)

        obj['user_id'] = self._user_id

        # convert observation_month to epoch integer
        obj['count_date'] = maya.parse(obj['count_date']).epoch

        count_obj = self._storage.save(obj_type, obj)
        self.sns_publish("counts", obj)  # publish notification

        count = clean(count_obj)

        count['count_date'] = datetime.utcfromtimestamp(
            count['count_date']).isoformat().split('T')[0]

        return count
Example #11
0
    def update_purchase_order(self, obj):
        table = 'brewoptix-purchase-orders'

        check_for_required_keys(obj, purchase_order_attributes)

        # check if content datatype is right
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, purchase_order_attributes)

        if 'entity_id' not in obj:
            raise BadParameters

        obj['user_id'] = self._user_id

        purchase_order = self._storage.get(table, obj['entity_id'])

        if not purchase_order:
            raise NoSuchEntity("Purchase Order")

        obj['order_date'] = maya.parse(obj['order_date']).epoch
        if "pack_date" in obj:
            obj['pack_date'] = maya.parse(obj['pack_date']).epoch
        if "ship_date" in obj:
            obj['ship_date'] = maya.parse(obj['ship_date']).epoch

        purchase_order_obj = self._storage.save(table, obj)
        self.sns_publish("purchase-orders", obj)  # publish notification

        purchase_order = clean(purchase_order_obj)
        purchase_order['order_date'] = datetime.utcfromtimestamp(
            purchase_order['order_date']).isoformat().split('T')[0]
        if "pack_date" in purchase_order:
            purchase_order['pack_date'] = maya.to_iso8601(
                datetime.utcfromtimestamp(
                    purchase_order['pack_date'])).split('T')[0]
        if "ship_date" in purchase_order:
            purchase_order['ship_date'] = maya.to_iso8601(
                datetime.utcfromtimestamp(
                    purchase_order['ship_date'])).split('T')[0]
        return purchase_order
Example #12
0
    def save_adjustment(self, obj):
        table = 'brewoptix-adjustment-inventory'

        check_for_required_keys(obj, adjustment_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, adjustment_attributes)

        obj['user_id'] = self._user_id

        # convert observation_month to epoch integer
        obj['adjustment_date'] = maya.parse(obj['adjustment_date']).epoch

        self.is_product_exists(obj["supplier_id"], obj["product_id"])

        adjustment_obj = self._storage.save(table, obj)
        self.sns_publish("adjustments", obj)  # publish notification

        adjustment = clean(adjustment_obj)

        adjustment['adjustment_date'] = datetime.utcfromtimestamp(
            adjustment['adjustment_date']).isoformat().split('T')[0]

        return adjustment
Example #13
0
    def add_purchase_order(self, obj):
        table = 'brewoptix-purchase-orders'

        check_for_required_keys(obj, purchase_order_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, purchase_order_attributes)

        obj['user_id'] = self._user_id

        if 'order_number' not in obj or not obj['order_number']:
            current_order_number = self._create_order_number(
                obj['supplier_id'])
            obj['order_number'] = current_order_number

        # convert observation_month to epoch integer
        obj['order_date'] = maya.parse(obj['order_date']).epoch
        if "pack_date" in obj:
            obj['pack_date'] = maya.parse(obj['pack_date']).epoch
        if "ship_date" in obj:
            obj['ship_date'] = maya.parse(obj['ship_date']).epoch

        purchase_order_obj = self._storage.save(table, obj)
        self.sns_publish("purchase-orders", obj)  # publish notification

        purchase_order = clean(purchase_order_obj)

        purchase_order['order_date'] = datetime.utcfromtimestamp(
            purchase_order['order_date']).isoformat().split('T')[0]
        if "pack_date" in purchase_order:
            purchase_order['pack_date'] = maya.to_iso8601(
                datetime.utcfromtimestamp(
                    purchase_order['pack_date'])).split('T')[0]
        if "ship_date" in purchase_order:
            purchase_order['ship_date'] = maya.to_iso8601(
                datetime.utcfromtimestamp(
                    purchase_order['ship_date'])).split('T')[0]
        return purchase_order
Example #14
0
    def save_supplier(self, obj):
        table = 'brewoptix-suppliers'

        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, supplier_attributes)
        check_for_required_keys(obj, supplier_attributes, exclude=["main_contact_id"])

        is_new_supplier = True

        if 'entity_id' in obj:
            existing_supplier = self._storage.get(table, obj['entity_id'])
            if existing_supplier:
                is_new_supplier = False
                existing_supplier = clean(existing_supplier)

        if not is_new_supplier and not self.is_current_user_admin(obj['entity_id']):
            raise NotAnAdminUser

        if 'main_contact_id' not in obj:
            obj['main_contact_id'] = self._user_id

        if is_new_supplier:
            if "users" in obj and obj["users"]:
                raise CannotUpdateUsers
        else:
            if "users" in obj:
                # cannot change users from this endpoint
                old_users = {}
                new_users = {}
                for user in existing_supplier['users']:
                    old_users[user['user_id']] = user
                for user in obj['users']:
                    new_users[user['user_id']] = user

                if any(user_id not in old_users.keys() for user_id in new_users):
                    raise CannotUpdateUsers

                for user_id in new_users:
                    new_role = new_users[user_id]['role']
                    old_role = old_users[user_id]['role']
                    if new_role != old_role:
                        raise CannotUpdateUsers

        if is_new_supplier:
            # get user name
            user_obj = self._storage.get_by_user_id('brewoptix-users', self._user_id)
            if user_obj:
                user_obj = clean(user_obj)
                user_name = user_obj.get('firstname', '') + " " + user_obj.get('lastname', '')
                user_name = user_name.rstrip()
                if not user_name or user_name.startswith(" "):
                    user_name = self._email
            else:
                user_name = self._email

            obj["users"] = [
                {
                    "user_id": self._user_id,
                    "user_name": user_name,
                    "role": "admin"
                }
            ]
        else:
            obj["users"] = existing_supplier['users']

        obj['user_id'] = self._user_id

        # upload/replace logo png to s3 bucket
        image_string = None
        if "logo" in obj:
            image_string = obj.pop("logo")

        if "has_logo" not in obj:
            obj["has_logo"] = False

        supplier_obj = self._storage.save(table, obj)
        self.sns_publish("suppliers", obj)  # publish notification

        if is_new_supplier:
            # Add supplier_id to user, to denote that this user added/updated the supplier
            self.add_supplier_to_app_metadata(supplier_id=obj['entity_id'],
                                              role="admin",
                                              valid=True)

        supplier = clean(supplier_obj)

        if image_string:
            logo_filepath = self.base64_to_png(image_string)

            if 'S3_ENDPOINT' in os.environ:
                s3 = boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT'])
            else:
                s3 = boto3.client('s3')

            bucket_name = os.environ['S3_UPLOADS_BUCKET_NAME']

            s3_filepath = "{SUPPLIER_ID}/logo.png".format(SUPPLIER_ID=supplier["entity_id"])
            resp = s3.upload_file(logo_filepath, bucket_name, s3_filepath)

        return supplier
Example #15
0
    def save_distributor(self, obj):
        table = 'brewoptix-distributors'

        check_for_required_keys(obj, distributors_attributes)
        content = {k: v for k, v in obj.items() if k not in base_attributes}
        check_properties_datatypes(content, distributors_attributes)

        is_new_distributor = True

        if 'entity_id' in obj:
            existing_distributor = self._storage.get(table, obj['entity_id'])
            if existing_distributor:
                is_new_distributor = False
                existing_distributor = clean(existing_distributor)

        if not is_new_distributor and not self.is_current_user_distributor_admin(obj['entity_id']):
            raise NotAnAdminUser

        # cannot update users field through this endpoint
        if is_new_distributor:
            if "users" in obj and obj["users"]:
                raise CannotUpdateUsers
        else:
            if "users" in obj:
                # cannot change users from this endpoint
                old_users = {}
                new_users = {}
                for user in existing_distributor['users']:
                    old_users[user['user_id']] = user
                for user in obj['users']:
                    new_users[user['user_id']] = user

                if any(user_id not in old_users.keys() for user_id in new_users):
                    raise CannotUpdateUsers

                for user_id in new_users:
                    new_role = new_users[user_id]['role']
                    old_role = old_users[user_id]['role']
                    if new_role != old_role:
                        raise CannotUpdateUsers

        if is_new_distributor:
            # get user name
            user_obj = self._storage.get_by_user_id('brewoptix-users', self._user_id)
            if user_obj:
                user_obj = clean(user_obj)
                user_name = user_obj.get('firstname', '') + " " + user_obj.get('lastname', '')
                user_name = user_name.rstrip()
                if not user_name or user_name.startswith(" "):
                    user_name = self._email
            else:
                user_name = self._email

            obj["users"] = [
                {
                    "user_id": self._user_id,
                    "user_name": user_name,
                    "role": "admin"
                }
            ]
        else:
            obj["users"] = existing_distributor['users']

        obj['user_id'] = self._user_id

        distributor_obj = self._storage.save(table, obj)
        self.sns_publish("distributors", obj)  # publish notification

        if is_new_distributor:
            # Add distributor_id to user, to denote that this user added/updated the distributor
            self.add_distributor_to_app_metadata(distributor_id=obj['entity_id'],
                                                 role="admin",
                                                 valid=True)

        distributor = clean(distributor_obj)

        return distributor