def count_storage_positions(storages):
        """Return the number of items that can be stored in storages.

        If any of these storages are "UnmanagedStorage" objects, then the
        result will be -1 as we cannot know how many items can be stored here.
        """
        count = 0
        for storage in storages:
            # If storage is an unmanaged storage, we no longer care about
            # "number of positions".
            if IUnmanagedStorage.providedBy(storage):
                return -1
            # If storage is a StoragePosition, simply increment the count.
            elif IStoragePosition.providedBy(storage):
                count += 1
            # If storage is a ManagedStorage, increment count for each
            # available StoragePosition
            elif IManagedStorage.providedBy(storage):
                count += storage.getFreePositions()
            else:
                raise ValidationError("Storage %s is not a valid storage type" %
                                      storage)
        return count
    def count_storage_positions(self, storages):
        """Return the number of items that can be stored in storages.

        If any of these storages are "UnmanagedStorage" objects, then the
        result will be -1 as we cannot know how many items can be stored here.
        """
        count = 0
        for storage in storages:
            # If storage is an unmanaged storage, we no longer care about
            # "number of positions".
            if IUnmanagedStorage.providedBy(storage):
                return -1
            # If storage is a StoragePosition, simply increment the count.
            elif IStoragePosition.providedBy(storage):
                count += 1
            # If storage is a ManagedStorage, increment count for each
            # available StoragePosition
            elif IManagedStorage.providedBy(storage):
                count += storage.get_free_positions()
            else:
                raise ValidationError(
                    "Storage %s is not a valid storage type" % storage)
        return count
    def create_kits(self):
        """Create the new kits
        """
        title_template = self.form.get('titletemplate', None)
        id_template = self.form.get('idtemplate', None)
        seq_start = int(self.form.get('seq_start', None))
        kit_count = int(self.form.get('kit_count', None))
        project_uid = self.form.get('Project_uid', None)
        kit_template_uid = self.form.get('KitTemplate_uid', None)
        kits = []

        kit_storages = self.get_kit_storages()
        if all(
            [IStoragePosition.providedBy(storage)
             for storage in kit_storages]):
            nr_positions = self.count_storage_positions(kit_storages)
            if kit_count > nr_positions:
                raise ValidationError(
                    u"Not enough kit storage positions available.  Please select "
                    u"or create additional storages for kits.")

        for x in range(seq_start, seq_start + kit_count):
            obj = api.content.create(container=self.context,
                                     type='Kit',
                                     id=id_template.format(id=x),
                                     title=title_template.format(id=x),
                                     Project=project_uid,
                                     KitTemplate=kit_template_uid,
                                     DateCreated=DateTime())
            self.assign_stockitems_to_kit(obj)
            self.context.manage_renameObject(obj.id, id_template.format(id=x))
            kits.append(obj)
        # store kits
        self.assign_kit_to_storage(kits, kit_storages)

        return kits
    def create_kits(self):
        """Create the new kits
        """
        title_template = self.form.get('titletemplate', None)
        id_template = self.form.get('idtemplate', None)
        seq_start = int(self.form.get('seq_start', None))
        kit_count = int(self.form.get('kit_count', None))
        project_uid = self.form.get('Project_uid', None)
        kit_template_uid = self.form.get('KitTemplate_uid', None)
        kits = []

        kit_storages = self.get_kit_storages()
        if all([IStoragePosition.providedBy(storage) for storage in kit_storages]):
            nr_positions = self.count_storage_positions(kit_storages)
            if kit_count > nr_positions:
                raise ValidationError(
                    u"Not enough kit storage positions available.  Please select "
                    u"or create additional storages for kits.")

        for x in range(seq_start, seq_start + kit_count):
            obj = api.content.create(
                container=self.context,
                type='Kit',
                id=id_template.format(id=x),
                title=title_template.format(id=x),
                Project=project_uid,
                KitTemplate=kit_template_uid,
                DateCreated=DateTime()
            )
            self.assign_stockitems_to_kit(obj)
            self.context.manage_renameObject(obj.id, id_template.format(id=x))
            kits.append(obj)
        # store kits
        self.assign_kit_to_storage(kits, kit_storages)

        return kits