Example #1
0
def inherit_place(place_id: ObjectId, device_id: str or ObjectId,
                  components: list):
    """Copies the place from the parent device to the new components and materializes them in the place"""
    ComponentDomain.update_raw(components, {'$set': {'place': place_id}})
    PlaceDomain.update_one_raw(place_id,
                               {'$addToSet': {
                                   'components': components
                               }})
Example #2
0
def unset_place(resource_name: str, event: dict):
    if resource_name in Event.resource_types:
        if "place" in event:
            place = PlaceDomain.get_one(event["place"])
            device = [event["device"]] if "device" in event else []
            devices = event.get("devices", []) + device
            execute_patch("places", {"devices": list(set(place["devices"]) - set(devices))}, event["place"])
Example #3
0
def unset_place(resource_name: str, event: dict):
    if resource_name in Event.resource_types:
        if 'place' in event:
            place = PlaceDomain.get_one(event['place'])
            device = [event['device']] if 'device' in event else []
            devices = event.get('devices', []) + device
            execute_patch('places', {'devices': list(set(place['devices']) - set(devices))}, event['place'])
Example #4
0
def _remove_devices_from_places(migrate):
    """Removes the devices of the migrate from their place"""
    for device in migrate:
        if 'place' in device:
            place = PlaceDomain.get_one(device['place'])
            devices = set(place['devices'])
            devices.remove(device['_id'])
            execute_patch('places', {'@type': 'Place', 'devices': list(devices)}, place['_id'])
Example #5
0
def _remove_devices_from_places(migrate):
    """Removes the devices of the migrate from their place"""
    for device in migrate:
        if 'place' in device:
            place = PlaceDomain.get_one(device['place'])
            devices = set(place['devices'])
            devices.remove(device['_id'])
            execute_patch('places', {'@type': 'Place', 'devices': list(devices)}, place['_id'])
Example #6
0
def remove_devices_from_place(migrates: dict):
    """
    Removes the devices that have been moved to another db from all places, as accounts are not supposed to interact
    with them anymore, and they would end up stuck in those places.
    """
    for migrate in migrates:
        if 'to' in migrate:
            devices_to_remove = set(migrate['devices'])
            for place in PlaceDomain.get({'devices': {'$in': migrate['devices']}}):
                payload = {
                    '@type': Place.type_name,
                    'devices': list(set(place['devices']) - devices_to_remove)
                }
                execute_patch(Place.resource_name, payload, place['_id'])
Example #7
0
def remove_devices_from_place(migrates: dict):
    """
    Removes the devices that have been moved to another db from all places, as accounts are not supposed to interact
    with them anymore, and they would end up stuck in those places.
    """
    for migrate in migrates:
        if 'to' in migrate:
            devices_to_remove = set(migrate['devices'])
            for place in PlaceDomain.get({'devices': {'$in': migrate['devices']}}):
                payload = {
                    '@type': Place.type_name,
                    'devices': list(set(place['devices']) - devices_to_remove)
                }
                execute_patch(Place.resource_name, payload, place['_id'])
Example #8
0
def set_place(resource_name: str, events: list):
    """
    Sets the place of the devices. This method must execute after 'get_place' of this module.

    The event performs PATCH o\ hf place, so the effect is like setting the devices to the place.
    :param resource_name:
    :param events:
    :return:
    """
    if resource_name in Event.resource_types:
        for event in events:
            if 'place' in event:
                place = PlaceDomain.get_one(event['place'])
                device = [event['device']] if 'device' in event else []
                execute_patch('places', {'devices': list(set(place['devices'] + event.get('devices', []) + device))},
                              event['place'])
Example #9
0
def set_place(resource_name: str, events: list):
    """
    Sets the place of the devices. This method must execute after 'get_place' of this module.

    The event performs PATCH of place, so the effect is like setting the devices to the place.
    :param resource_name:
    :param events:
    :return:
    """
    if resource_name in Event.resource_types:
        for event in events:
            if "place" in event:
                place = PlaceDomain.get_one(event["place"])
                device = [event["device"]] if "device" in event else []
                execute_patch(
                    "places",
                    {"devices": list(set(place["devices"] + event.get("devices", []) + device))},
                    event["place"],
                )
Example #10
0
def get_place(resource_name: str, events: list):
    """

    :param resource_name:
    :param events:
    :return:
    """
    if resource_name in Event.resource_types:
        for event in events:
            if "geo" in event:
                try:
                    place = PlaceDomain.get_with_coordinates(event["geo"]["coordinates"])
                except (KeyError, NoPlaceForGivenCoordinates) as e:
                    # Receive and Locate are forced to have a place for their coordinates
                    if event["@type"] in (DeviceEventDomain.new_type(x) for x in ("Receive", "Locate")):
                        raise e
                else:
                    if "place" in event:
                        if event["place"]["_id"] != str(place["_id"]):  # geo 1 place 1
                            raise CoordinatesAndPlaceDoNotMatch()
                    else:
                        event["place"] = place["_id"]  # geo 1 place found in DB
Example #11
0
def get_place(resource_name: str, events: list):
    """

    :param resource_name:
    :param events:
    :return:
    """
    if resource_name in Event.resource_types:
        for event in events:
            if 'geo' in event:
                try:
                    place = PlaceDomain.get_with_coordinates(event['geo']['coordinates'])
                except (KeyError, NoPlaceForGivenCoordinates) as e:
                    # Receive and Locate are forced to have a place for their coordinates
                    if event['@type'] in (DeviceEventDomain.new_type(x) for x in ('Receive', 'Locate')):
                        raise e
                else:
                    if 'place' in event:
                        if event['place']['_id'] != str(place['_id']):  # geo 1 place 1
                            raise CoordinatesAndPlaceDoNotMatch()
                    else:
                        event['place'] = place['_id']  # geo 1 place found in DB
Example #12
0
def set_place_in_devices(items: list):
    for item in items:
        if 'devices' in item:
            PlaceDomain.update_devices(set(), set(item['devices']),
                                       item['_id'])
Example #13
0
def update_place_in_devices(replaced_place: dict, original_place: dict):
    PlaceDomain.update_devices(set(original_place['devices']),
                               set(replaced_place['devices']),
                               replaced_place['_id'])
Example #14
0
def set_place_in_devices(items: list):
    for item in items:
        if 'devices' in item:
            PlaceDomain.update_devices(set(), set(item['devices']), item['_id'])
Example #15
0
def unset_place_in_devices(place):
    PlaceDomain.update_devices(set(place['devices']), set(), None)
Example #16
0
def update_place_in_devices(replaced_place: dict, original_place: dict):
    PlaceDomain.update_devices(set(original_place['devices']), set(replaced_place['devices']), replaced_place['_id'])
Example #17
0
def unset_place_in_devices(place):
    PlaceDomain.update_devices(set(place['devices']), set(), None)