def add_objects(self, api_root, id_, objs, request_time):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            for collection in collections:
                if "id" in collection and id_ == collection["id"]:
                    if "objects" not in collection:
                        collection["objects"] = []
                    failed = 0
                    succeeded = 0
                    for new_obj in objs["objects"]:
                        id_and_version_already_present = False
                        if new_obj['id'] in collection["objects"]:
                            current_obj = collection["objects"][new_obj['id']]
                            if "modified" in new_obj:
                                if new_obj['modified'] == current_obj[
                                        'modified']:
                                    id_and_version_already_present = True
                            else:
                                # There is no modified field, so this object is immutable
                                id_and_version_already_present = True
                        if not id_and_version_already_present:
                            collection["objects"].append(new_obj)
                            self._update_manifest(new_obj, api_root,
                                                  collection["id"])
                            succeeded += 1
                        else:
                            failed += 1

            status = generate_status(request_time, succeeded, failed, 0)
            api_info["status"].append(status)
            return status
Exemplo n.º 2
0
    def add_objects(self, api_root, collection_id, objs, request_time):
        # TODO: Handle if mongodb is not available
        api_root_db = self.client[api_root]
        objects = api_root_db["objects"]
        failed = 0
        succeeded = 0
        for new_obj in objs["objects"]:
            mongo_query = {
                "_collection_id": collection_id,
                "id": new_obj["id"]
            }
            if "modified" in new_obj:
                mongo_query["modified"] = new_obj["modified"]
            existing_entry = objects.find_one(mongo_query)
            if existing_entry:
                failed += 1
            else:
                new_obj.update({"_collection_id": collection_id})
                objects.insert_one(new_obj)
                self._update_manifest(new_obj, api_root, collection_id)
                succeeded += 1

        status = generate_status(request_time, succeeded, failed, 0)
        api_root_db["status"].insert_one(status)
        del status["_id"]
        return status
Exemplo n.º 3
0
    def add_objects(self, api_root, id_, objs, request_time):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])

            for collection in collections:
                if "id" in collection and id_ == collection["id"]:
                    if "objects" not in collection:
                        collection["objects"] = []
                    failed = 0
                    succeeded = 0
                    for new_obj in objs["objects"]:
                        found = False
                        for current_obj in collection["objects"]:
                            if new_obj['id'] == current_obj['id'] and new_obj['modified'] == current_obj['modified']:
                                found = True
                                break
                        if not found:
                            collection["objects"].append(new_obj)
                            self._update_manifest(new_obj, api_root, collection["id"])
                            succeeded += 1
                        else:
                            failed += 1

            status = generate_status(request_time, succeeded, failed, 0)
            api_info["status"].append(status)
            return status
Exemplo n.º 4
0
    def add_objects(self, api_root, id_, objs, request_time):
        if api_root in self.data:
            api_info = self._get(api_root)
            collections = api_info.get("collections", [])
            failed = 0
            succeeded = 0
            pending = 0
            successes = []
            failures = []

            for collection in collections:
                if "id" in collection and id_ == collection["id"]:
                    if "objects" not in collection:
                        collection["objects"] = []
                    try:
                        for new_obj in objs["objects"]:
                            id_and_version_already_present = False
                            for obj in collection["objects"]:
                                id_and_version_already_present = False

                                if new_obj["id"] == obj["id"]:
                                    if "modified" in new_obj:
                                        if new_obj["modified"] == obj[
                                                "modified"]:
                                            id_and_version_already_present = True
                                    else:
                                        # There is no modified field, so this object is immutable
                                        id_and_version_already_present = True
                            if not id_and_version_already_present:
                                collection["objects"].append(new_obj)
                                self._update_manifest(new_obj, api_root,
                                                      collection["id"],
                                                      request_time)
                                successes.append(new_obj["id"])
                                succeeded += 1
                            else:
                                failures.append({
                                    "id":
                                    new_obj["id"],
                                    "message":
                                    "Unable to process object"
                                })
                                failed += 1
                    except Exception as e:
                        raise ProcessingError(
                            "While processing supplied content, an error occurred",
                            422, e)

            status = generate_status(
                request_time,
                "complete",
                succeeded,
                failed,
                pending,
                successes_ids=successes,
                failures=failures,
            )
            api_info["status"].append(status)
            return status
Exemplo n.º 5
0
    def add_objects(self, api_root, collection_id, objs, request_time):
        api_root_db = self.client[api_root]
        objects = api_root_db["objects"]
        failed = 0
        succeeded = 0
        pending = 0
        successes = []
        failures = []

        try:
            for new_obj in objs["objects"]:
                mongo_query = {
                    "_collection_id": collection_id,
                    "id": new_obj["id"]
                }
                if "modified" in new_obj:
                    mongo_query["modified"] = new_obj["modified"]
                existing_entry = objects.find_one(mongo_query)
                if existing_entry:
                    failures.append({
                        "id": new_obj["id"],
                        "message": "Unable to process object"
                    })
                    failed += 1
                else:
                    new_obj.update({"_collection_id": collection_id})
                    objects.insert_one(new_obj)
                    self._update_manifest(new_obj, api_root, collection_id)
                    successes.append(new_obj["id"])
                    succeeded += 1
        except Exception as e:
            raise ProcessingError(
                "While processing supplied content, an error occured", e)

        status = generate_status(request_time,
                                 "complete",
                                 succeeded,
                                 failed,
                                 pending,
                                 successes_ids=successes,
                                 failures=failures)
        api_root_db["status"].insert_one(status)
        status.pop("_id", None)
        return status
Exemplo n.º 6
0
    def add_objects(self, api_root, collection_id, objs, request_time):
        failed = 0
        succeeded = 0
        pending = 0
        successes = []
        failures = []

        file_name = '{}--{}.{}'.format(request_time, objs['id'], 'json')
        p = os.path.join(self.path, api_root)
        fp = os.path.join(p, file_name)

        try:
            add_objs = objs['objects']
            num_objs = len(add_objs)

            try:
                # Each add_object request writes the provided bundle to a new file
                with open(fp, 'w') as out_file:
                    out_file.write(json.dumps(objs, indent=4, sort_keys=True))

                    succeeded += num_objs
                    successes = list(map(lambda x: x['id'], add_objs))

                # Update the cache after the file is written
                self.with_cache(api_root)
            except IOError:
                failed += num_objs
                failures = list(map(lambda x: x['id'], add_objs))

        except Exception as e:
            raise ProcessingError('error adding objects', 500, e)

        status = generate_status(request_time,
                                 'complete',
                                 succeeded,
                                 failed,
                                 pending,
                                 successes_ids=successes,
                                 failures=failures)

        self.statuses.append(status)

        return status