class STOCK_MAPPER(object): def __init__(self): self.__product_mapper = PRODUCT_MAPPER() self.__entity_mapper = ENTITY_MAPPER() self.__unit_mapper = UNIT_MAPPER() def __stock_from_row(self, stock_row): my_logger.debug(message='%s' % stock_row) return STOCK(id=stock_row['stock']['id'], product=lambda: self.__product_mapper.find(stock_row['stock']['product'])[0], entity=lambda: self.__entity_mapper.find(stock_row['stock']['entity'])[0], maximum=stock_row['stock']['maximum'], maximum_unit=self.__unit_mapper.find(stock_row['stock']['maximum_unit']) if stock_row['stock']['maximum_unit'] is not None else None, minimum=stock_row['stock']['minimum'], minimum_unit=self.__unit_mapper.find(stock_row['stock']['minimum_unit']) if stock_row['stock']['minimum_unit'] is not None else None) def find(self, product_id, entity_id): if type(entity_id) is ListType: _stock_rows = current.db((current.db.stock.entity.belongs(tuple(entity_id))) & (current.db.stock.product == product_id) & (current.db.entity.id == current.db.stock.entity)).select() else: _stock_rows = current.db((current.db.stock.entity == entity_id) & (current.db.stock.product == product_id) & (current.db.entity.id == current.db.stock.entity)).select() return [self.__stock_from_row(_stock_row) for _stock_row in _stock_rows]
def read(): mylogger.debug(message='request.vars:%s' % request.vars) exposure_card_mapper = EXPOSURE_CARD_MAPPER() product_mapper = PRODUCT_MAPPER() error = '' # getting the exposure card exposure_card = exposure_card_mapper.find(exposure_card_id=request.args[0])[0] if 'cas_number' in request.vars: _product_id = request.vars.cas_number # duclicity check not performed by the field validator #_value, _error = db.exposure_item.product.validate(_product_id) #mylogger.debug(message='_error, _value:%s, %s' % (_error, _value)) # but in the c_exposure_card.py class with an exceptions.AssertionError _product = product_mapper.find(product_id=_product_id)[0] try: exposure_card.add_exposure_item_for_product(_product) exposure_card_mapper.update(exposure_card) except AssertionError, e: error = cc.get_string('EXPOSURE_CARD_PRODUCT_ALREADY_PRESENT_ERROR')
class STORAGE_MAPPER(object): def __init__(self): self.__product_mapper = PRODUCT_MAPPER() self.__person_mapper = PERSON_MAPPER() self.__store_location_mapper = STORE_LOCATION_MAPPER() self.__supplier_mapper = SUPPLIER_MAPPER() self.__unit_mapper = UNIT_MAPPER() def __storage_from_row(self, storage_row): return STORAGE(id=storage_row['id'], volume_weight=storage_row['volume_weight'], unit=lambda: self.__unit_mapper.find(unit_id=storage_row['unit']) \ if storage_row['unit'] is not None \ else None, nb_items=storage_row['nb_items'], creation_datetime=storage_row['creation_datetime'], entry_datetime=storage_row['entry_datetime'], exit_datetime=storage_row['exit_datetime'], expiration_datetime=storage_row['expiration_datetime'], opening_datetime=storage_row['opening_datetime'], comment=storage_row['comment'], barecode=storage_row['barecode'], reference=storage_row['reference'], batch_number=storage_row['batch_number'], archive=storage_row['archive'], to_destroy=storage_row['to_destroy'], product=lambda: self.__product_mapper.find(product_id=storage_row['product'])[0], person=lambda: self.__person_mapper.find(person_id=storage_row['person'])[0], store_location=lambda: self.__store_location_mapper.find(store_location_id=storage_row['store_location'])[0], supplier=lambda: self.__supplier_mapper.find(supplier_id=storage_row['supplier'])[0], # storage history modification_datetime=storage_row['modification_datetime'] if 'modification_datetime' in storage_row else None, has_borrowing=lambda: self.has_borrowing(storage_row['id']), retrieve_borrower=lambda: self.retrieve_borrower(storage_row['id']), retrieve_borrow_datetime=lambda: self.retrieve_borrow_datetime(storage_row['id']), retrieve_borrow_comment=lambda: self.retrieve_borrow_comment(storage_row['id']), has_history=lambda: self.has_history(storage_row['id'])) def find(self, storage_id=None, storage_history_id=None, entity_id=None, negate_entity_search=False, store_location_id=None, product_id=None, unit_reference_id=None, archive=False, history=False, limitby=None, orderby=None): my_logger.debug(message='storage_id:%s' % storage_id) assert (storage_history_id is not None and history) or (storage_history_id is None), "history must be True with a storage_history_id!" if history: table = 'storage_history' else: table = 'storage' query_list = [] if storage_id is not None or storage_history_id is not None: if history: if storage_history_id is not None: query_list.append(current.db.storage_history.id == storage_history_id) else: query_list.append(current.db.storage_history.current_record == storage_id) else: if type(storage_id) is ListType: query_list.append(current.db[table]['id'].belongs(storage_id)) else: query_list.append(current.db[table]['id'] == storage_id) if entity_id is not None: if type(entity_id) is ListType: if negate_entity_search: query_list.append((current.db[table]['store_location']==current.db.store_location.id) & (~current.db.store_location.entity.belongs(tuple(entity_id)))) else: query_list.append((current.db[table]['store_location']==current.db.store_location.id) & (current.db.store_location.entity.belongs(tuple(entity_id)))) else: if negate_entity_search: query_list.append((current.db[table]['store_location']==current.db.store_location.id) & (~current.db.store_location.entity==entity_id)) else: query_list.append((current.db[table]['store_location']==current.db.store_location.id) & (current.db.store_location.entity==entity_id)) if store_location_id is not None: query_list.append(current.db[table]['store_location'] == store_location_id) if product_id is not None: if type(product_id) is ListType: query_list.append(current.db[table]['product'].belongs(product_id)) else: query_list.append(current.db[table]['product'] == product_id) if unit_reference_id is not None: query_list.append((current.db.storage.unit == current.db.unit.id) & (current.db.unit.reference == unit_reference_id)) if archive is not None: final_query = (current.db[table]['archive']==archive) else: final_query = (current.db[table]['id']>0) # building the final query for query in query_list: my_logger.debug(message='query:%s' % str(query)) final_query = final_query.__and__(query) my_logger.debug(message='final_query:%s' % str(final_query)) _storage_rows = current.db(final_query).select(current.db[table]['ALL'], left=(current.db.borrow.on(current.db[table]['id'] == current.db.borrow.storage)), limitby=limitby, orderby=orderby) my_logger.debug(message='len(_storage_rows):%s' % str(len(_storage_rows))) my_logger.debug(message='_storage_rows:%s' % str(_storage_rows)) if len(_storage_rows) == 0: return [] else: return [self.__storage_from_row(_storage_row) for _storage_row in _storage_rows] def has_history(self, storage_id): return current.db(current.db.storage_history.current_record==storage_id).count() > 0 def has_borrowing(self, storage_id): return current.db(current.db.borrow.storage==storage_id).count() > 0 def retrieve_borrower(self, storage_id): borrower = current.db((current.db.borrow.storage==storage_id) & (current.db.borrow.borrower==current.db.person.id)).select(current.db.person.id).first() if borrower is not None: return PERSON_MAPPER().find(person_id=borrower.id)[0] else: return None def retrieve_borrow_comment(self, storage_id): borrow = current.db((current.db.borrow.storage==storage_id)).select(current.db.borrow.comment).first() if borrow is not None: return borrow.comment else: return None def retrieve_borrow_datetime(self, storage_id): borrow = current.db((current.db.borrow.storage==storage_id)).select(current.db.borrow.creation_datetime).first() if borrow is not None: return borrow.creation_datetime else: return None def delete(self, storage): # STORAGE type current.db(current.db.storage.id==storage.id).delete() current.db.commit() def update(self, storage): # STORAGE type row = current.db(current.db.storage.id==storage.id).select().first() row.update_record(product=storage.product.id, store_location=storage.store_location.id, volume_weight=storage.volume_weight, unit=storage.unit.id if storage.unit is not None else None, nb_items=storage.nb_items, entry_datetime=storage.entry_datetime, exit_datetime=storage.exit_datetime, expiration_datetime=storage.expiration_datetime, opening_datetime=storage.opening_datetime, comment=storage.comment, barecode=storage.barecode, reference=storage.reference, batch_number=storage.batch_number, supplier=storage.supplier.id, archive=storage.archive, to_destroy=storage.to_destroy) current.db.commit() @staticmethod def create_barecode(product_id): """Return the generated barecode from a product """ mylogger.debug(message='create_barecode') product_cas_number = current.db(current.db.product.id == product_id).select(current.db.product.cas_number).first().cas_number mylogger.debug(message='product_id:%s' % product_id) mylogger.debug(message='product_cas_number:%s' % product_cas_number) last_storage_id = current.db(current.db.storage).count() mylogger.debug(message='last_storage_id:%s' % last_storage_id) today = datetime.date.today() today = today.strftime('%Y%m%d') barecode = '%s_%s_%s.1' % (product_cas_number, today, last_storage_id) mylogger.debug(message='barecode:%s' % barecode) return barecode
class EXPOSURE_ITEM_MAPPER(object): """Database exposure item table mapper. Request the database to create EXPOSURE_ITEM instances. """ def __init__(self): pass def _exposure_item_from_row(self, _exposure_item_row): """Return an EXPOSURE_ITEM instance from a row. """ from c_product_mapper import PRODUCT_MAPPER self.__product_mapper = PRODUCT_MAPPER() return EXPOSURE_ITEM(id=_exposure_item_row['id'], creation_datetime=_exposure_item_row['creation_datetime'], product=lambda: self.__product_mapper.find(product_id=_exposure_item_row['product'])[0], kind_of_work=_exposure_item_row['kind_of_work'], cpe=_exposure_item_row['cpe'], ppe=_exposure_item_row['ppe'], nb_exposure=_exposure_item_row['nb_exposure'], exposure_time=_exposure_item_row['exposure_time'], simultaneous_risk=_exposure_item_row['simultaneous_risk']) def find(self, exposure_item_id=None, orderby=None): """Select exposure items in the database. exposure_item_id -- search by exposure_item_id or list of ids """ my_logger.debug(message='exposure_item_id:%s' % exposure_item_id) query_list = [] if exposure_item_id is not None: if type(exposure_item_id) is ListType: query_list.append(current.db.exposure_item.id.belongs(exposure_item_id)) else: query_list.append(current.db.exposure_item.id == exposure_item_id) # joining with the product an name table for the orderby parameter final_query = (current.db.exposure_item.product == current.db.product.id) & (current.db.product.name == current.db.name.id) # building the final query for query in query_list: my_logger.debug(message='query:%s' % str(query)) final_query = final_query.__and__(query) # getting the EXPOSURE_ITEM in the db _exposure_item_rows = current.db(final_query).select(current.db.exposure_item.ALL, orderby=orderby) my_logger.debug(message='_exposure_item_rows:%s' % str(_exposure_item_rows)) if len(_exposure_item_rows) == 0: return [] else: return [self._exposure_item_from_row(_exposure_item_row) for _exposure_item_row in _exposure_item_rows] def delete(self, exposure_item): """Delete an exposure_item. exposure_item -- an EXPOSURE_ITEM instance return: the last row id """ # deleting exposure cards references linked_exposure_cards = current.db(current.db.exposure_card.exposure_item.contains(exposure_item.id)).select() for linked_exposure_card in linked_exposure_cards: exposure_items = linked_exposure_card.exposure_item exposure_items.remove(exposure_item.id) linked_exposure_card.update_record(exposure_items=exposure_items) _id = current.db(current.db.exposure_item.id == exposure_item.id).delete() current.db.commit() return _id def update(self, exposure_item): # EXPOSURE_ITEM type my_logger.debug(message='exposure_item:%s' % str(exposure_item)) if isinstance(exposure_item.id, (int, long)): _is_new = False else: _is_new = True my_logger.debug(message='_is_new:%s' % _is_new) if _is_new: _ret = current.db.exposure_item.insert(product=exposure_item.product.id, kind_of_work=exposure_item.kind_of_work, cpe=exposure_item.cpe, ppe=exposure_item.ppe, nb_exposure=exposure_item.nb_exposure, exposure_time=exposure_item.exposure_time, simultaneous_risk=exposure_item.simultaneous_risk) else: row = current.db(current.db.exposure_item.id == exposure_item.id).select().first() row.update_record(product=exposure_item.product.id, kind_of_work=exposure_item.kind_of_work, cpe=exposure_item.cpe, ppe=exposure_item.ppe, nb_exposure=exposure_item.nb_exposure, exposure_time=exposure_item.exposure_time, simultaneous_risk=exposure_item.simultaneous_risk) _ret = row.id current.db.commit() return _ret
class STOCK_STORE_LOCATION_MAPPER(object): def __init__(self): self.__product_mapper = PRODUCT_MAPPER() self.__store_location_mapper = STORE_LOCATION_MAPPER() self.__storage_mapper = STORAGE_MAPPER() self.__unit_mapper = UNIT_MAPPER() def __stock_store_location_from_row(self, stock_store_location_row): return STOCK_STORE_LOCATION(id=stock_store_location_row['id'], product=lambda: self.__product_mapper.find(stock_store_location_row['product'])[0], store_location=lambda: self.__store_location_mapper.find(stock_store_location_row['store_location'])[0], unit_reference=lambda: self.__unit_mapper.find(stock_store_location_row['unit_reference']) \ if stock_store_location_row['unit_reference'] is not None \ else None, volume_weight_actual=stock_store_location_row['volume_weight_actual'], volume_weight_total=stock_store_location_row['volume_weight_total'], storages=lambda: self.find(store_location_id=stock_store_location_row['store_location'], product_id=stock_store_location_row['product'], unit_reference_id=stock_store_location_row['unit_reference'])) # development/test function - not to be used in the code def new_for_store_location_and_product_and_unit(self, store_location_id, product_id, unit_reference_id): _stock_store_location_row = {} _stock_store_location_row['id'] = -1 # not used _stock_store_location_row['store_location'] = store_location_id _stock_store_location_row['product'] = product_id _stock_store_location_row['unit_reference'] = unit_reference_id _stock_store_location_row['volume_weight_actual'] = 0 _stock_store_location_row['volume_weight_total'] = 0 return self.__stock_store_location_from_row(_stock_store_location_row) def find(self, store_location_id, product_id=None, unit_reference_id=None, no_unit_reference=False): assert ( (no_unit_reference and (unit_reference_id is None)) or ((not no_unit_reference) and (unit_reference_id is not None)), "unit_reference_id and no_unit_reference parameters incoherence!") query_list = [] if product_id is not None: query_list.append( current.db.stock_store_location.product == product_id) if no_unit_reference: query_list.append( current.db.stock_store_location.unit_reference == None) elif unit_reference_id is not None: query_list.append(current.db.stock_store_location.unit_reference == unit_reference_id) else: query_list.append(current.db.stock_store_location.id > 0) #query_list.append(current.db.stock_store_location.unit_reference == unit_reference_id) final_query = (current.db.stock_store_location.store_location == store_location_id) # building the final query for query in query_list: my_logger.debug(message='query:%s' % str(query)) final_query = final_query.__and__(query) _stock_store_location_rows = current.db(final_query).select() if len(_stock_store_location_rows) == 0: return [] else: return [ self.__stock_store_location_from_row(_stock_store_location_row) for _stock_store_location_row in _stock_store_location_rows ] def exists(self, store_location_id, product_id, unit_reference_id=None, no_unit_reference=False): assert ( (no_unit_reference and (unit_reference_id is None)) or ((not no_unit_reference) and (unit_reference_id is not None)), "unit_reference_id and no_unit_reference parameters incoherence!") query_list = [] if no_unit_reference: query_list.append( current.db.stock_store_location.unit_reference == None) elif unit_reference_id is not None: query_list.append(current.db.stock_store_location.unit_reference == unit_reference_id) else: query_list.append(current.db.stock_store_location.id > 0) final_query = ((current.db.stock_store_location.store_location == store_location_id) & (current.db.stock_store_location.product == product_id)) # building the final query for query in query_list: my_logger.debug(message='query:%s' % str(query)) final_query = final_query.__and__(query) return current.db(final_query).count() > 0 def save(self, stock_store_location): _unit_reference_id = stock_store_location.unit_reference.id \ if stock_store_location.unit_reference is not None \ else None current.db.stock_store_location.insert( volume_weight_total=stock_store_location.volume_weight_total, volume_weight_actual=stock_store_location.volume_weight_actual, unit_reference=_unit_reference_id, store_location=stock_store_location.store_location.id, product=stock_store_location.product.id) current.db.commit() def update(self, stock_store_location): _unit_reference_id = stock_store_location.unit_reference.id \ if stock_store_location.unit_reference is not None \ else None current.db( current.db.stock_store_location.id == stock_store_location.id).update( volume_weight_total=stock_store_location.volume_weight_total, volume_weight_actual=stock_store_location.volume_weight_actual, unit_reference=_unit_reference_id, store_location=stock_store_location.store_location.id, product=stock_store_location.product.id) current.db.commit()