Example #1
0
 def materialize_events(cls, resource: str, events: list):
     if resource in DeviceEvent.resource_types:
         for event in events:
             trimmed_event = {field_name: event[field_name] for field_name in cls.fields if field_name in event}
             query = {'$push': {'events': {'$each': [trimmed_event], '$position': 0}}}
             devices = [event['device']] if 'device' in event else event['devices']
             if 'parent' in event:  # Let's materialize the events (test, erasure...) of the component to the parent
                 devices.append(event['parent'])
             DeviceDomain.update_raw(devices, query)
             DeviceDomain.update_raw(event.get('components', []), query)
Example #2
0
def materialize_public_in_components(resource: str, devices: list):
    """
    Materializes the 'public' field of the passed in devices in their components.
    :param resource:
    :param devices:
    :return:
    """
    if resource in Device.resource_types:
        for device in devices:
            if device.get('components'):  # If empty do not try to execute
                DeviceDomain.update_raw(device['components'], {'$set': {'public': device.get('public', False)}})
Example #3
0
 def dematerialize_event(cls, _, event: dict):
     if event.get('@type') in DeviceEvent.types:
         device = [event['device']] if 'device' in event else []
         parent = [event['parent']] if 'parent' in device else []
         for device_id in event.get('devices', []) + event.get(
                 'components', []) + device + parent:
             DeviceDomain.update_raw(
                 device_id, {'$pull': {
                     'events': {
                         '_id': event['_id']
                     }
                 }})
Example #4
0
def materialize_public_in_components(resource: str, devices: list):
    """
    Materializes the 'public' field of the passed in devices in their components.
    :param resource:
    :param devices:
    :return:
    """
    if resource in Device.resource_types:
        for device in devices:
            if device.get('components'):  # If empty do not try to execute
                DeviceDomain.update_raw(
                    device['components'],
                    {'$set': {
                        'public': device.get('public', False)
                    }})
Example #5
0
 def materialize_events(cls, resource: str, events: list):
     if resource in DeviceEvent.resource_types:
         for event in events:
             trimmed_event = {
                 field_name: event[field_name]
                 for field_name in cls.FIELDS if field_name in event
             }
             query = {
                 '$push': {
                     'events': {
                         '$each': [trimmed_event],
                         '$position': 0
                     }
                 }
             }
             devices = [event['device']
                        ] if 'device' in event else event['devices']
             if 'parent' in event:  # Let's materialize the events (test, erasure...) of the component to the parent
                 devices.append(event['parent'])
             DeviceDomain.update_raw(devices, query)
             DeviceDomain.update_raw(event.get('components', []), query)
Example #6
0
def materialize_actual_owners_remove(events: list):
    for event in events:
        properties = {'$pull': {'owners': event['from']}}
        DeviceDomain.update_raw(event.get('components', []), properties)
        return DeviceDomain.update_raw(event['devices'], properties)
Example #7
0
def materialize_actual_owners_add(allocates: list):
    for allocate in allocates:
        properties = {'$addToSet': {'owners': allocate['to']}}
        DeviceDomain.update_raw(allocate['devices'], properties)
        DeviceDomain.update_raw(allocate.get('components', []), properties)
Example #8
0
 def dematerialize_event(cls, resource: str, event: dict):
     if event.get('@type') in DeviceEvent.types:
         device = [event['device']] if 'device' in event else []
         parent = [event['parent']] if 'parent' in device else []
         for device_id in event.get('devices', []) + event.get('components', []) + device + parent:
             DeviceDomain.update_raw(device_id, {'$pull': {'events': {'_id': event['_id']}}})
Example #9
0
def materialize_actual_owners_remove(events: list):
    for event in events:
        properties = {'$pull': {'owners': event['from']}}
        DeviceDomain.update_raw(event.get('components', []), properties)
        return DeviceDomain.update_raw(event['devices'], properties)
Example #10
0
def materialize_actual_owners_add(allocates: list):
    for allocate in allocates:
        properties = {'$addToSet': {'owners': allocate['to']}}
        DeviceDomain.update_raw(allocate['devices'], properties)
        DeviceDomain.update_raw(allocate.get('components', []), properties)