예제 #1
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def post(self, file_id):
        # noinspection PyUnusedLocal
        args = self.post_parser.parse_args()
        colln = get_colln()
        user = get_user(required=True)
        files = request.files.getlist("file")

        file_anno_doc = db_helper.read_by_id(colln, file_id)
        if file_anno_doc is None:
            return error_response(message="file not found", code=404)

        file_anno = JsonObject.make_from_dict(file_anno_doc)
        target_resource_id = file_anno.target
        target_resource = JsonObject.make_from_dict(
            db_helper.read_by_id(colln, target_resource_id))

        has_update_permission = permission_manager.has_persmission(
            user, Permission.UPDATE, obj=target_resource)

        if not has_update_permission:
            return error_response(
                message="user has no permission for this operation", code=403)
        for f in files:
            full_path = resource_file_path(target_resource_id,
                                           file_anno.body.path)
            os.remove(full_path)
            f.save(full_path)
            return {"success": True}
예제 #2
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def delete(self):
        args = self.delete_parser.parse_args()
        colln = get_colln()
        user = get_user(required=True)

        resource_ids = jsonify_argument(args['resource_ids'])
        check_argument_type(resource_ids, (list, ))

        ids_validity = False not in [
            isinstance(_id, str) for _id in resource_ids
        ]
        if not ids_validity:
            return error_response(message='ids should be strings', code=404)

        delete_report = []

        for _id in resource_ids:
            deleted, deleted_res_ids = db_helper.delete(
                colln, _id, user, permission_manager=permission_manager)
            for deleted_res_id in deleted_res_ids:
                delete_resource_dir(deleted_res_id)
            delete_report.append({
                "deleted":
                deleted,
                "deleted_dependents_count":
                len(deleted_res_ids)
            })

        return delete_report
예제 #3
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def delete(self, file_id):
        colln = get_colln()
        user = get_user(required=True)

        try:
            delete_resource_file(colln, user, file_id)
        except PermissionError:
            return error_response(
                message="user has no permission for this operation", code=403)
        return {"success": True}
예제 #4
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def post(self):
        args = self.post_parser.parse_args()
        colln = get_colln()
        user = get_user(required=True)

        resource_doc = jsonify_argument(args['resource_json'],
                                        key='resource_json')
        check_argument_type(resource_doc, (dict, list), key='resource_json')

        created_docs = []

        resource_docs = resource_doc if isinstance(resource_doc,
                                                   list) else [resource_doc]
        '''
        TODO thought: can check integrity of all resources first,
        and then after ensuring all has integrity, we can proceed.
        instead of deleting some, and then halt at some
        '''

        for n, doc in enumerate(resource_docs):
            # noinspection PyBroadException
            try:
                resource = JsonObject.make_from_dict(doc)
                if resource.json_class in self.white_listed_classes:
                    raise TypeError('object type is not supported')
                handle_creation_details(colln, user, resource)
                resource.validate()
            except Exception as e:
                return error_response(
                    message='{} th JsonObject\'s schema is invalid'.format(n),
                    code=404,
                    posted=created_docs,
                    errorAt=n,
                    error=str(e))
            try:
                created_doc = db_helper.update(
                    colln,
                    resource,
                    user,
                    permission_manager=permission_manager)
                created_docs.append(created_doc)
            except OrphanResourceError:
                return error_response(
                    message="cannot leave dependent one as an orphan",
                    code=404)

        if isinstance(resource_doc, dict):
            resource_id = created_docs[0]['_id']
            files = request.files.getlist("files")
            purpose = args['files_purpose']
            for f in files:
                save_file(colln, user, resource_id, f, purpose)
        return created_docs
예제 #5
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def post(self, resource_id):
        args = self.post_parser.parse_args()
        colln = get_colln()
        user = get_user(required=True)

        files = request.files.getlist("files")
        purpose = args['files_purpose']
        file_annos = []
        for f in files:
            anno = save_file(colln, user, resource_id, f,
                             purpose).to_json_map()
            anno.pop('body', None)
            file_annos.append(anno)
        return file_annos
예제 #6
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def delete(self, resource_id):
        args = self.get_parser.parse_args()
        colln = get_colln()
        user = get_user(required=True)

        filter_doc = jsonify_argument(args['filter_doc'],
                                      key='filter_doc') or {}
        check_argument_type(filter_doc, (dict, ), key='filter_doc')

        deleted_all, deleted_res_ids = db_helper.delete_annotations(
            colln,
            resource_id,
            user,
            filter_doc=filter_doc,
            permission_manager=permission_manager)
        return {"deleted_all": deleted_all, "deleted_res_ids": deleted_res_ids}
예제 #7
0
파일: rest.py 프로젝트: poojapi/ullekhanam
    def post(self):
        args = self.post_parser.parse_args()
        colln = get_colln()
        user = get_user(required=True)

        trees = jsonify_argument(args['trees'], key='trees')
        check_argument_type(trees, (list, ), key='trees')

        result_trees = []
        try:
            for i, tree in enumerate(trees):
                # noinspection PyTypeChecker
                result_tree = update_tree(colln, user, tree,
                                          'tree{}'.format(i), None)
                result_trees.append(result_tree)
        except TreeCrawlError as e:
            print(e)
            return error_response(message="error in tree crawling",
                                  code=404,
                                  error_position=e.tree_position,
                                  succeded_trees=result_trees,
                                  error=str(e.error),
                                  node_json=e.node_json)
        return result_trees