def validate_form_inputs(self): """Validate form inputs """ form = self.request.form title_template = form.get('titletemplate', None) id_template = form.get('idtemplate', None) if not (title_template and id_template): raise ValidationError(u'ID and Title template are both required.') if not ('{id}' in title_template and '{id}' in id_template): raise ValidationError(u'ID and Title templates must contain {id} ' u'for ID sequence substitution') try: seq_start = int(form.get('seq_start', None)) except: raise ValidationError( u'Sequence start and all counts must be integers') uid_1 = form.get('first_biosepcimen_limit', None) uid_2 = form.get('last_biospecimen_limit', None) if not uid_1 or not uid_2: raise ValidationError(u'Kits range is required. Or project select has no kits!') biospecimens = objects_between_two_uids(self.context, uid_1, uid_2, 'Sample', 'bika_sample_workflow', 'bika_cancellation_workflow', 'sample_received') bio_count = len(biospecimens) aliquot_count = int(form.get('aliquot_count', None)) count = bio_count * aliquot_count # Check that none of the IDs conflict with existing items ids = [x.id for x in self.context.objectValues('Aliquot')] for x in range(count): check = id_template.format(id=seq_start + x) if check in ids: raise ValidationError( u'The ID %s exists, cannot be created.' % check) # Check that the storages selected has sufficient positions to contain # the biospecimen to generate. storage_uids = self.form['aliquot_storage_uids'].split(',') aliquot_storages = get_storage_objects(self.context, storage_uids) if all([IManagedStorage.providedBy(storage) for storage in aliquot_storages]): nr_positions = count_storage_positions(aliquot_storages) if aliquot_count > nr_positions: raise ValidationError( u"Not enough kit storage positions available. Please select " u"or create additional storages for kits.") return { 'title_template': title_template, 'id_template': id_template, 'seq_start': seq_start, 'first_bio_limit': uid_1, 'last_bio_limit': uid_2, 'biospecimens': biospecimens, 'count': count, 'aliquot_count': aliquot_count, 'storages': aliquot_storages }
def validate_form_inputs(self): form = self.request.form title_template = form.get('titletemplate', None) id_template = form.get('idtemplate', None) if not (title_template and id_template): raise ValidationError(u'ID and Title template are both required.') if not ('{id}' in title_template and '{id}' in id_template): raise ValidationError(u'ID and Title templates must contain {id} ' u'for ID sequence substitution') try: seq_start = int(form.get('seq_start', None)) except: raise ValidationError( u'Sequence start and all counts must be integers') first_kit_limit = form.get('first_kit_limit', None) last_kit_limit = form.get('last_kit_limit', None) if not first_kit_limit or not last_kit_limit: raise ValidationError( u'Kits range is required. Or project select has no kits!') kits = self.kits_between_limits(first_kit_limit, last_kit_limit) count = len(kits) biospecimen_per_kit = int(form.get('biospecimen_per_kit', None)) biospecimen_count = count * biospecimen_per_kit # Check that none of the IDs conflict with existing items ids = [x.id for x in self.context.objectValues()] for x in range(biospecimen_count): check = id_template.format(id=seq_start + x) if check in ids: raise ValidationError(u'The ID %s exists, cannot be created.' % check) # Check that the storages selected has sufficient positions to contain # the biospecimen to generate. bio_storages = self.get_biospecimen_storages() if all( [IManagedStorage.providedBy(storage) for storage in bio_storages]): nr_positions = self.count_storage_positions(bio_storages) if biospecimen_count > nr_positions: raise ValidationError( u"Not enough kit storage positions available. Please select " u"or create additional storages for kits.") return { 'title_template': title_template, 'id_template': id_template, 'seq_start': seq_start, 'first_kit_limit': first_kit_limit, 'last_kit_limit': last_kit_limit, 'kits': kits, 'biospecimen_per_kit': biospecimen_per_kit, 'biospecimen_count': biospecimen_count, 'storages': bio_storages }
def filter_stockitems_by_storage_location(self, items): """Return stockitems in the selected storages """ si_storages = self.get_si_storages() stockitems = [] for storage in si_storages: if IUnmanagedStorage.providedBy(storage): sis = storage.getBackReferences('ItemStorageLocation') stockitems += [si for si in sis if si in items] elif IManagedStorage.providedBy(storage): sis = storage.only_items_of_portal_type('StockItem') stockitems += [si for si in sis if si in items] return stockitems
def count_storage_positions(storages): """"Return the number of items that can be stored in storages. This method is called in case all the storages are of type Managed. """ count = 0 for storage in storages: # If storage is a ManagedStorage, increment count for each # available StoragePosition if IManagedStorage.providedBy(storage): count += storage.getFreePositions() else: raise ValidationError("Storage %s is not a valid storage type" % storage) return count
def count_storage_positions(storages): """"Return the number of items that can be stored in storages. This method is called in case all the storages are of type Managed. """ count = 0 for storage in storages: # If storage is a ManagedStorage, increment count for each # available StoragePosition if IManagedStorage.providedBy(storage): count += storage.getFreePositions() else: raise ValidationError("Storage %s is not a valid storage type" % storage) return count
def filter_stockitems_by_storage_location(self, items): """Return stockitems in the selected storages """ si_storages = self.get_si_storages() stockitems = [] for storage in si_storages: if IUnmanagedStorage.providedBy(storage): sis = storage.getBackReferences('ItemStorageLocation') stockitems += [si for si in sis if si in items] elif IManagedStorage.providedBy(storage): sis = storage.only_items_of_portal_type('StockItem') stockitems += [si for si in sis if si in items] # if si_storages: # return [item for item in items # if item.getStorageLocation() in si_storages] # else: # return items return stockitems
def assign_kit_to_storage(self, kits, storages): """ assign position to created kits. """ for storage in storages: if IManagedStorage.providedBy(storage): free_positions = storage.get_free_positions() if len(kits) <= len(free_positions): for i, kit in enumerate(kits): kit.setStorageLocation(free_positions[i]) self.wf.doActionFor(free_positions[i], 'occupy') else: for i, position in enumerate(free_positions): kits[i].setStorageLocation(position) self.wf.doActionFor(position, 'occupy') kits = kits[len(free_positions):] elif IUnmanagedStorage.providedBy(storage): # Case of unmanaged storage there is no limit in storage until # user manually set the storage as full. for kit in kits: kit.setStorageLocation(storage)
def assign_items_to_storages(context, items, storages): """ store items inside selected storages """ wf = getToolByName(context, 'portal_workflow') for storage in storages: if IManagedStorage.providedBy(storage): free_positions = storage.get_free_positions() if len(items) <= len(free_positions): for i, item in enumerate(items): item.setStorageLocation(free_positions[i]) wf.doActionFor(free_positions[i], 'occupy') else: for i, position in enumerate(free_positions): items[i].setStorageLocation(position) wf.doActionFor(position, 'occupy') items = items[len(free_positions):] elif IUnmanagedStorage.providedBy(storage): # Case of unmanaged storage there is no limit in storage until # user manually set the storage as full. for item in items: item.setStorageLocation(storage)
def assign_kit_to_storage(self, kits, storages): """ assign position to created kits. """ wf = getToolByName(self.context, 'portal_workflow') for storage in storages: if IManagedStorage.providedBy(storage): free_positions = storage.get_free_positions() if len(kits) <= len(free_positions): for i, kit in enumerate(kits): kit.setStorageLocation(free_positions[i]) wf.doActionFor(free_positions[i], 'occupy') else: for i, position in enumerate(free_positions): kits[i].setStorageLocation(position) wf.doActionFor(position) kits = kits[len(free_positions):] elif IUnmanagedStorage.providedBy(storage): # Case of unmanaged storage there is no limit in storage until # user manually set the storage as full. for kit in kits: kit.setStorageLocation(storage)
def assign_items_to_storages(context, items, storages): """ store items inside selected storages """ wf = getToolByName(context, 'portal_workflow') for storage in storages: if IManagedStorage.providedBy(storage): free_positions = storage.get_free_positions() if len(items) <= len(free_positions): for i, item in enumerate(items): item.setStorageLocation(free_positions[i]) wf.doActionFor(free_positions[i], 'occupy') else: for i, position in enumerate(free_positions): items[i].setStorageLocation(position) wf.doActionFor(position, 'occupy') items = items[len(free_positions):] elif IUnmanagedStorage.providedBy(storage): # Case of unmanaged storage there is no limit in storage until # user manually set the storage as full. for item in items: item.setStorageLocation(storage)
def assign_biospecimens_to_storages(self, biospecimens, storages): """ Assign positions to biospecimens inside storages """ wf = getToolByName(self.context, 'portal_workflow') for storage in storages: if IManagedStorage.providedBy(storage): free_positions = storage.get_free_positions() if len(biospecimens) <= len(free_positions): for i, biospecimen in enumerate(biospecimens): biospecimen.setStorageLocation(free_positions[i]) wf.doActionFor(free_positions[i], 'occupy') else: for i, position in enumerate(free_positions): biospecimens[i].setStorageLocation(position) wf.doActionFor(position, 'occupy') biospecimens = biospecimens[len(free_positions):] elif IUnmanagedStorage.providedBy(storage): # Case of unmanaged storage there is no limit in storage until # user manually set the storage as full. for biospecimen in biospecimens: biospecimen.setStorageLocation(storage)
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 validate_form_inputs(self): form = self.request.form title_template = form.get('titletemplate', None) id_template = form.get('idtemplate', None) if not (title_template and id_template): raise ValidationError(u'ID and Title template are both required.') if not ('{id}' in title_template and '{id}' in id_template): raise ValidationError(u'ID and Title templates must contain {id} ' u'for ID sequence substitution') try: seq_start = int(form.get('seq_start', None)) kit_count = int(form.get('kit_count', None)) except: raise ValidationError( u'Sequence start and all counts must be integers') # verify ID sequence start if seq_start < 1: raise ValidationError(u'Sequence Start should be > 0') # verify number of kits if kit_count < 1: raise ValidationError(u'Kit count should be > 0') # Kit template required kit_template_uid = self.form.get('KitTemplate_uid', None) if not kit_template_uid: raise ValidationError(u'Kit template field is required.') # Kit storage destination is required field. kit_storage_uids = form.get('kit_storage_uids', '') if not kit_storage_uids: raise ValidationError(u'You must select the Storage where the kit ' u'items will be stored.') # Stock Item storage (where items will be taken from) is required si_storage_uids = form.get('si_storage_uids', '') if not si_storage_uids: raise ValidationError(u'You must select the Storage where the ' u'stock items will be taken from.') # Check that none of the IDs conflict with existing items ids = [x.id for x in self.context.objectValues()] for x in range(kit_count): check = id_template.format(id=seq_start + x) if check in ids: raise ValidationError( u'The ID %s exists, cannot be created.' % check) # Check there are enough stock items in stock to create the kits kit_template = self.bsc(UID=kit_template_uid)[0].getObject() for item in kit_template.getProductList(): items = self.product_stockitems(item['product_uid']) items = self.filter_stockitems_by_storage_location(items) if len(items) < int(item['quantity']) * kit_count: raise ValidationError( u"There is insufficient stock available for " \ u"product '%s'." % item['product']) kit_storages = self.get_kit_storages() if all([IManagedStorage.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.")
def validate_form_inputs(self): """Validate form inputs """ form = self.request.form title_template = form.get('titletemplate', None) id_template = form.get('idtemplate', None) if not (title_template and id_template): raise ValidationError(u'ID and Title template are both required.') if not ('{id}' in title_template and '{id}' in id_template): raise ValidationError(u'ID and Title templates must contain {id} ' u'for ID sequence substitution') try: seq_start = int(form.get('seq_start', None)) except: raise ValidationError( u'Sequence start and all counts must be integers') uid_1 = form.get('first_biosepcimen_limit', None) uid_2 = form.get('last_biospecimen_limit', None) if not uid_1 or not uid_2: raise ValidationError( u'Kits range is required. Or project select has no kits!') biospecimens = objects_between_two_uids(self.context, uid_1, uid_2, 'Sample', 'bika_sample_workflow', 'bika_cancellation_workflow', 'sample_received') bio_count = len(biospecimens) aliquot_count = int(form.get('aliquot_count', None)) count = bio_count * aliquot_count # Check that none of the IDs conflict with existing items ids = [x.id for x in self.context.objectValues('Aliquot')] for x in range(count): check = id_template.format(id=seq_start + x) if check in ids: raise ValidationError(u'The ID %s exists, cannot be created.' % check) # Check that the storages selected has sufficient positions to contain # the biospecimen to generate. storage_uids = self.form['aliquot_storage_uids'].split(',') aliquot_storages = get_storage_objects(self.context, storage_uids) if all([ IManagedStorage.providedBy(storage) for storage in aliquot_storages ]): nr_positions = count_storage_positions(aliquot_storages) if aliquot_count > nr_positions: raise ValidationError( u"Not enough kit storage positions available. Please select " u"or create additional storages for kits.") return { 'title_template': title_template, 'id_template': id_template, 'seq_start': seq_start, 'first_bio_limit': uid_1, 'last_bio_limit': uid_2, 'biospecimens': biospecimens, 'count': count, 'aliquot_count': aliquot_count, 'storages': aliquot_storages }