Ejemplo n.º 1
0
    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}
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
 def get(self, json_class):
     from sanskrit_ld.schema import json_class_registry
     class_obj = json_class_registry.get(json_class, None)
     if class_obj is None:
         return error_response(
             message='{} is not defined'.format(json_class))
     return class_obj.context
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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}
Ejemplo n.º 6
0
    def get(self, file_id):
        colln = get_colln()
        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
        abs_file_path = resource_file_path(target_resource_id,
                                           file_anno.body.path)

        file_dir = os.path.dirname(abs_file_path)
        file_name = os.path.basename(abs_file_path)
        from flask import send_from_directory
        return send_from_directory(directory=file_dir, filename=file_name)
Ejemplo n.º 7
0
    def get(self, resource_id):
        args = self.get_parser.parse_args()
        colln = get_colln()

        associated_resources_request_doc = jsonify_argument(
            args['associated_resources'], 'associated_resources')
        check_argument_type(associated_resources_request_doc, (dict, ),
                            key='associated_resources',
                            allow_none=True)

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

        if associated_resources_request_doc is not None:
            attach_associated_resources(colln, [resource],
                                        associated_resources_request_doc)
        return resource
Ejemplo n.º 8
0
    def get(self):
        args = self.get_parser.parse_args()
        colln = get_colln()

        selector_doc = jsonify_argument(args['selector_doc'],
                                        key='selector_doc')
        check_argument_type(selector_doc, (dict, ), key='selector_doc')

        fields = jsonify_argument(args['fields'], key='fields')
        check_argument_type(fields, (list, ), key='fields', allow_none=True)

        associated_resources_request_doc = jsonify_argument(
            args['associated_resources'], 'associated_resources')
        check_argument_type(associated_resources_request_doc, (dict, ),
                            key='associated_resources',
                            allow_none=True)

        sort_doc = jsonify_argument(args['sort_doc'], key='sort_doc')
        check_argument_type(sort_doc, (dict, list),
                            key='sort_doc',
                            allow_none=True)

        ops = OrderedDict()
        if sort_doc is not None:
            ops['sort'] = [sort_doc]
        ops['skip'] = [args['start']]
        ops['limit'] = [args['numbers']]

        try:
            resource_reprs = list(
                db_helper.read_and_do(colln,
                                      selector_doc,
                                      ops,
                                      fields=fields,
                                      return_generator=True))
        except (TypeError, ValueError):
            return error_response(
                message='arguments to operations seems invalid', code=400)
        if associated_resources_request_doc is not None:
            attach_associated_resources(colln, resource_reprs,
                                        associated_resources_request_doc)
        return resource_reprs
Ejemplo n.º 9
0
    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