예제 #1
0
파일: hooks.py 프로젝트: eReuse/DeviceHub
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"])
예제 #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'])
예제 #3
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'])
예제 #4
0
파일: hooks.py 프로젝트: eReuse/DeviceHub
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'])
예제 #5
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'])
예제 #6
0
파일: hooks.py 프로젝트: eReuse/DeviceHub
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'])
예제 #7
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'])
예제 #8
0
파일: hooks.py 프로젝트: eReuse/DeviceHub
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"],
                )