def create_migrate(migrates: list): """ Manages the creation of a Migrate event, like doing so for the devices. Heavily inspired by the hook 'on_insert_snapshot', uses the same idea on a group of devices. """ all_events = [] # We will delete all events if exception try: for migrate in migrates: if 'from' in migrate: setattr(g, MIGRATE_RETURNED_SAME_AS, dict()) devices_id = [] migrate['components'] = [] for device in migrate['devices']: if device['@type'] in Component.types: raise SchemaError('devices', 'You cannot directly migrate components.') creator = MigrateCreator(device, device.pop('components')) events = creator.execute() all_events += events migrate['events'] = [new_events['_id'] for new_events in events] devices_id.append(device['_id']) migrate['components'] += [component['_id'] for component in creator.components] migrate['unsecured'] = creator.unsecured getattr(g, MIGRATE_RETURNED_SAME_AS).update(creator.returned_same_as) from ereuse_devicehub.resources.hooks import set_date set_date(None, migrates) migrate['devices'] = devices_id except Exception as e: for event in all_events: # Could result in 404 (ex: delete an 'Add' after deleting 'Register' of the same device) execute_delete(Naming.resource(event['@type']), event['_id']) raise e
def post_devices(registers: list): """ Main function for Register. For the given devices, POST the new ones. This method rollbacks the database when raising exceptions, like when no device has been POSTed. If the function is called by post_internal(), as the method keeps the reference of the passed in devices, the caller will see how their devices are replaced by the db versions, plus a 'new' property acting as a flag to indicate if the device is new or not. If a device exists, the input device is replaced by the version of the database, loosing any change the input device was introducing (except benchmarks, which are updated). See `_execute_register` for more info. :raise InnerRequestError: for any error provoked by a failure in the POST of a device (except if the device already existed). It carries the original error sent by the POST. :raise NoDevicesToProcess: Raised to avoid creating empty registers, that actually did not POST any device """ log = [] try: for register in registers: caller_device = register[ 'device'] # Keep the reference from where register['device'] points to _execute_register(caller_device, register.get('created'), log) register['device'] = caller_device['_id'] if 'components' in register: caller_components = register['components'] register['components'] = [] for component in caller_components: component['parent'] = caller_device['_id'] _execute_register(component, register.get('created'), log) if component[ 'new']: # todo put new in g., don't use device register['components'].append(component['_id']) if caller_device['new']: set_components(register) elif not register['components']: text = 'Device {} and components {} already exist.'.format( register['device'], register['components']) raise NoDevicesToProcess(text) else: add_components([ register ]) # The device is not new but we have new computers # Note that we only need to copy a place from the parent if this already existed if 'place' in caller_device: inherit_place(caller_device['place'], register['device'], register['components']) except Exception as e: for device in reversed(log): # Rollback deleteitem_internal(Naming.resource(device['@type']), device) raise e else: from ereuse_devicehub.resources.hooks import set_date set_date(None, registers) # Let's get the time AFTER creating the devices
def on_insert_snapshot(items): for item in items: if 'label' in item: item['device']['labelId'] = item['label'] # todo as we do not update the values of a device, # todo we will never update, thus materializing new label ids snapshot = Snapshot(item['device'], item['components'], item.get('created')) item['events'] = [new_events['_id'] for new_events in snapshot.execute()] item['device'] = item['device']['_id'] item['components'] = [component['_id'] for component in item['components']] item['unsecured'] = snapshot.unsecured from ereuse_devicehub.resources.hooks import set_date set_date(None, items) # Let's get the time AFTER creating the other events
def post_devices(registers: list): """ Main function for Register. For the given devices, POST the new ones. This method rollbacks the database when raising exceptions, like when no device has been POSTed. If the function is called by post_internal(), as the method keeps the reference of the passed in devices, the caller will see how their devices are replaced by the db versions, plus a 'new' property acting as a flag to indicate if the device is new or not. If a device exists, the input device is replaced by the version of the database, loosing any change the input device was introducing (except benchmarks, which are updated). See `_execute_register` for more info. :raise InnerRequestError: for any error provoked by a failure in the POST of a device (except if the device already existed). It carries the original error sent by the POST. :raise NoDevicesToProcess: Raised to avoid creating empty registers, that actually did not POST any device """ log = [] try: for register in registers: caller_device = register['device'] # Keep the reference from where register['device'] points to _execute_register(caller_device, register.get('created'), log) register['device'] = caller_device['_id'] if 'components' in register: caller_components = register['components'] register['components'] = [] for component in caller_components: component['parent'] = caller_device['_id'] _execute_register(component, register.get('created'), log) if component['new']: # todo put new in g., don't use device register['components'].append(component['_id']) if caller_device['new']: set_components(register) elif not register['components']: raise NoDevicesToProcess() except Exception as e: for device in reversed(log): # Rollback deleteitem_internal(Naming.resource(device['@type']), device) raise e else: from ereuse_devicehub.resources.hooks import set_date set_date(None, registers) # Let's get the time AFTER creating the devices
def on_insert_snapshot(items): for item in items: if 'label' in item: item['device']['labelId'] = item[ 'label'] # todo as we do not update the values of a device, # todo we will never update, thus materializing new label ids if item['snapshotSoftware'] == 'Workbench': snapshot = Snapshot(item['device'], item['components'], item.get('created')) else: # In App or web we do not ask for component info snapshot = SnapshotNotProcessingComponents(item['device'], item.get('created')) item['events'] = [ new_events['_id'] for new_events in snapshot.execute() ] item['device'] = snapshot.device['_id'] item['components'] = [ component['_id'] for component in snapshot.components ] item['unsecured'] = snapshot.unsecured from ereuse_devicehub.resources.hooks import set_date set_date(None, items) # Let's get the time AFTER creating the other events