def execute(self, database): SNAPSHOT_SOFTWARE = { 'DDI': 'Workbench', 'Scan': 'AndroidApp', 'DeviceHubClient': 'Web' } for snapshot in DeviceEventDomain.get({'@type': "devices:Snapshot"}): with suppress(KeyError): snapshot['snapshotSoftware'] = SNAPSHOT_SOFTWARE[snapshot.get( 'snapshotSoftware', 'DDI')] DeviceEventDomain.update_one_raw( snapshot['_id'], {'$set': { 'snapshotSoftware': snapshot['snapshotSoftware'] }}) for device in DeviceDomain.get({'events._id': snapshot['_id']}): materialized_snapshot = find( device['events'], lambda event: event['_id'] == snapshot['_id']) materialized_snapshot['snapshotSoftware'] = snapshot[ 'snapshotSoftware'] DeviceDomain.update_one_raw( device['_id'], {'$set': { 'events': device['events'] }})
def execute(self, database): for device in DeviceDomain.get({'hid': 'dummy'}): try: hid = DeviceDomain.hid(device['manufacturer'], device['serialNumber'], device['model']) DeviceDomain.update_one_raw(device['_id'], {'$set': {'hid': hid}}) except KeyError: DeviceDomain.update_one_raw(device['_id'], {'$unset': {'hid': ''}, '$set': {'isUidSecured': False}})
def materialize_component_info(): devices = DeviceDomain.get({'@type': Computer.type_name}) for device in devices: if 'components' in device: # Let's reset the ram and hard-drive counter for _type in ('totalRamSize', 'totalHardDriveSize'): DeviceDomain.update_one_raw(device['_id'], {'$set': { _type: 0 }}) update_materialized_computer(device['_id'], device['components'], add=True)
def avoid_repeating_allocations(allocates: list): """ Checks that we are not allocating to an account that is already an owner of the device This method must execute after :param allocates: :return: """ for allocate in allocates: devices_with_repeating_owners = DeviceDomain.get({ '$or': [ {'_id': {'$in': [allocate['devices']]}}, {'owners': {'$in': [allocate['to']]}} ] }) ids = [device['_id'] for device in devices_with_repeating_owners] allocate['devices'] = list(set(allocate['devices']) - set(ids)) if len(allocate['devices']) == 0: raise AlreadyAllocated()
def avoid_repeating_allocations(allocates: list): """ Checks that we are not allocating to an account that is already an owner of the device This method must execute after :param allocates: :return: """ for allocate in allocates: devices_with_repeating_owners = DeviceDomain.get({ '$or': [{ '_id': { '$in': [allocate['devices']] } }, { 'owners': { '$in': [allocate['to']] } }] }) ids = [device['_id'] for device in devices_with_repeating_owners] allocate['devices'] = list(set(allocate['devices']) - set(ids)) if len(allocate['devices']) == 0: raise AlreadyAllocated()
def re_materialize_owners(): for device in DeviceDomain.get({'@type': 'Computer'}): materialize_owners([device['_id']]) print('Owners re-materialized in devices.')