Ejemplo n.º 1
0
 def on_created(self, object, params):
     if app_config.mwdb.enable_karton and not object.is_analyzed():
         object.spawn_analysis(arguments=params.get("karton_arguments", {}))
     hooks.on_created_object(object)
     tags = params.get("tags")
     for tag in tags:
         db_tag = Tag(tag=tag["tag"])
         db_tag, _ = Tag.get_or_create(db_tag)
         hooks.on_created_tag(object, db_tag)
Ejemplo n.º 2
0
    def create_object(self, params):
        params = dict(params)

        # Validate parent object
        if params["parent"] is not None:
            if not g.auth_user.has_rights(Capabilities.adding_parents):
                raise Forbidden("You are not permitted to link with parent")

            parent_object = Object.access(params["parent"])

            if parent_object is None:
                raise NotFound("Parent object not found")
        else:
            parent_object = None

        # Validate metakeys
        metakeys = params["metakeys"]
        for metakey in params["metakeys"]:
            key = metakey["key"]
            if not MetakeyDefinition.query_for_set(key).first():
                raise NotFound(
                    f"Metakey '{key}' not defined or insufficient "
                    "permissions to set that one"
                )

        # Validate upload_as argument
        share_with = get_shares_for_upload(params["upload_as"])

        item, is_new = self._create_object(params, parent_object, share_with, metakeys)

        try:
            db.session.commit()

            if is_new:
                hooks.on_created_object(item)
                self.on_created(item)
            else:
                hooks.on_reuploaded_object(item)
                self.on_reuploaded(item)
        finally:
            item.release_after_upload()

        logger.info(
            f"{self.ObjectType.__name__} added",
            extra={"dhash": item.dhash, "is_new": is_new},
        )
        schema = self.ItemResponseSchema()
        return schema.dump(item)
Ejemplo n.º 3
0
    def create_pulled_object(self, item, is_new):
        try:
            db.session.commit()

            if is_new:
                hooks.on_created_object(item)
                self.on_created(item)
            else:
                hooks.on_reuploaded_object(item)
                self.on_reuploaded(item)
        finally:
            item.release_after_upload()

        logger.info(
            f"{self.ObjectType.__name__} added",
            extra={
                "dhash": item.dhash,
                "is_new": is_new
            },
        )
        schema = self.ItemResponseSchema()
        return schema.dump(item)
Ejemplo n.º 4
0
    def create_object(self, params):
        params = dict(params)

        # Validate parent object
        if params["parent"] is not None:
            if not g.auth_user.has_rights(Capabilities.adding_parents):
                raise Forbidden("You are not permitted to link with parent")

            parent_object = Object.access(params["parent"])

            if parent_object is None:
                raise NotFound("Parent object not found")
        else:
            parent_object = None

        # Validate metakeys
        metakeys = params["metakeys"]
        for metakey in params["metakeys"]:
            key = metakey["key"]
            if not MetakeyDefinition.query_for_set(key).first():
                raise NotFound(f"Metakey '{key}' not defined or insufficient "
                               "permissions to set that one")

        # Validate upload_as argument
        upload_as = params["upload_as"]
        if upload_as == "*":
            # If '*' is provided: share with all user's groups except 'public'
            share_with = [
                group for group in g.auth_user.groups if group.name != "public"
            ]
        else:
            share_group = Group.get_by_name(upload_as)
            # Does group exist?
            if share_group is None:
                raise NotFound(f"Group {upload_as} doesn't exist")
            # Has user access to group?
            if share_group not in g.auth_user.groups and not g.auth_user.has_rights(
                    Capabilities.sharing_objects):
                raise NotFound(f"Group {upload_as} doesn't exist")
            # Is group pending?
            if share_group.pending_group is True:
                raise NotFound(f"Group {upload_as} is pending")
            share_with = [share_group, Group.get_by_name(g.auth_user.login)]

        item, is_new = self._create_object(params, parent_object, share_with,
                                           metakeys)

        db.session.commit()

        if is_new:
            hooks.on_created_object(item)
            self.on_created(item)
        else:
            hooks.on_reuploaded_object(item)
            self.on_reuploaded(item)

        logger.info(f'{self.ObjectType.__name__} added',
                    extra={
                        'dhash': item.dhash,
                        'is_new': is_new
                    })
        schema = self.ItemResponseSchema()
        return schema.dump(item)