def process_same_as(self, device: dict) -> (bool, list):
        """
        Tries to match the input device with an existing one by detecting a similar url in sameAs and url fields, and
        then processes them.

        Given a set of sameAs and url in the param device, it tries to find at least one of those url ones in another
        device in the database. If found, both devices are thought to be the same and both 'sameAs' fields are merged
        (note that Register does update the sameAs).

        :param device: The device to check if whose sameAs and URL match any of the already existing ones.
        :return: A boolean indicating if a device was found in the database with at least one matching url and a list
        of sameAs for the device
        """
        urls = [device['url']] + device.get('sameAs', [])
        self.urls.append(device.pop('url'))
        matching_same_as = False
        try:
            already_existing_device = DeviceDomain.get_one(
                {'sameAs': {
                    '$in': urls
                }})
        except DeviceNotFound:
            same_as = device['sameAs'] = urls
        else:
            device['_id'] = already_existing_device['_id']
            urls_set = set(urls)
            # we do not want the url of the existing device to be part of the sameAs
            urls_set.discard(already_existing_device.get('url'))
            same_as = device['sameAs'] = list(
                set(already_existing_device['sameAs']) | urls_set)
            matching_same_as = True
        return matching_same_as, same_as
Beispiel #2
0
 def device_set_place(device_id: str, place_id: str):
     device = DeviceDomain.get_one(device_id)
     if 'place' in device:
         current_app.data.driver.db['places'].update_one(
             {'_id': device['place']}, {'$pull': {
                 'devices': device_id
             }})
     current_app.data.driver.db['devices'].update_one(
         {'_id': device_id}, {'$set': {
             'place': place_id
         }})
    def generate_returned_same_as(self):
        """
        Populates self.returned_same_as with a dict where the URL of devices in the caller agent as keys and a copy of
        'sameAs' in this agent.

        This value will be returned to the caller agent (see
        :py:func:`ereuse_devicehub.resources.event.device.migrate.hooks.return_same_as`) which can use it to update
        its 'sameAs' values with new references.
        """
        for device, url in zip([self.device] + self.components, self.urls):
            device = DeviceDomain.get_one(device['_id'])
            same_as = set(device['sameAs'])
            same_as.remove(url)
            same_as.add(
                DeviceDomain.url_agent_for(
                    AccountDomain.get_requested_database(), device['_id']))
            self.returned_same_as[url] = list(same_as)
Beispiel #4
0
def delete_device(_, register):
    if register.get('@type') == Register.type_name:
        for device_id in [register['device']] + register.get('components', []):
            execute_delete(Naming.resource(DeviceDomain.get_one(device_id)['@type']), device_id)
Beispiel #5
0
def _update_same_as(returned_same_as: dict):
    for url, same_as in list(returned_same_as.items()):
        _id = url.split('/')[-1]
        device = DeviceDomain.get_one({'_id': _id})
        same_as = set(device.get('sameAs', [])) | set(same_as)
        DeviceDomain.update_one_raw(_id, {'$set': {'sameAs': list(same_as)}})
Beispiel #6
0
def delete_device(_, register):
    if register.get('@type') == Register.type_name:
        for device_id in [register['device']] + register.get('components', []):
            execute_delete(
                Naming.resource(DeviceDomain.get_one(device_id)['@type']),
                device_id)
Beispiel #7
0
def _update_same_as(returned_same_as: dict):
    for url, same_as in returned_same_as.items():
        _id = url.split('/')[-1]
        device = DeviceDomain.get_one({'_id': _id})
        same_as = set(device.get('sameAs', [])) | set(same_as)
        DeviceDomain.update_one_raw(_id, {'$set': {'sameAs': list(same_as)}})
Beispiel #8
0
 def device_set_place(device_id: str, place_id: str):
     device = DeviceDomain.get_one(device_id)
     if 'place' in device:
         current_app.data.driver.db['places'].update_one({'_id': device['place']}, {'$pull': {'devices': device_id}})
     current_app.data.driver.db['devices'].update_one({'_id': device_id}, {'$set': {'place': place_id}})