Beispiel #1
0
class ReferenceResource(Resource):
    @api.parameters(PaginationParameters())
    @api.response(ReferenceSchema(many=True))
    def get(self, args):
        """
        List of references.
        """
        return Reference.query.offset(args['offset']).limit(args['limit'])
Beispiel #2
0
class UserAllFollowing(Resource):
    @api.login_required(oauth_scopes=['users:read'])
    @api.permission_required(permissions.OwnerRolePermission())
    @api.parameters(PaginationParameters())
    @api.response(schemas.UserSchema(many=True))
    def get(self, args):
        """Get all following users of current user
        """
        return User.query_all_following(current_user.id) \
            .offset(args['offset']).limit(args['limit'])
Beispiel #3
0
class StoryResource(Resource):
    @api.parameters(PaginationParameters())
    @api.response(StorySchema(many=True))
    def get(self, args):
        """
        List of all stories.

        :param args: query parameters
        :return: a list of stories starting from ``offset`` limited by
        ``limit`` parameter.
        """
        return Story.query.offset(args['offset']).limit(args['limit'])
Beispiel #4
0
class OrganizationResource(Resource):
    @api.parameters(PaginationParameters())
    @api.response(OrganizationSchema(many=True))
    def get(self, args):
        """
        List of organizations.
        """
        return Organization.query.offset(args['offset']).limit(args['limit'])

    @api.login_required(oauth_scopes=['datasets:write'])
    @api.parameters(AddOrganizationParameters())
    @api.response(OrganizationSchema())
    def post(self, args):
        """Create a new organization
        """
        with api.commit_or_abort(
                db.session,
                default_error_message="Failed to create a new organization."):
            new_org = Organization.create(**args)
            db.session.add(new_org)
        return new_org
Beispiel #5
0
class LicenseResource(Resource):
    @api.parameters(PaginationParameters())
    @api.response(LicenseSchema(many=True))
    def get(self, args):
        """
        List of licenses.
        """
        return License.query.offset(args['offset']).limit(args['limit'])

    @api.login_required(oauth_scopes=['datasets:write'])
    @api.parameters(AddLicenseParameters())
    @api.response(LicenseSchema())
    def post(self, args):
        """Create a new license
        """
        with api.commit_or_abort(
                db.session,
                default_error_message="Failed to create a new license."):
            new_license = License.create(**args)
            db.session.add(new_license)
        return new_license
Beispiel #6
0
class PublisherResource(Resource):
    @api.parameters(PaginationParameters())
    @api.response(PublisherSchema(many=True))
    def get(self, args):
        """
        List of publishers.
        """
        return Publisher.query.offset(args['offset']).limit(args['limit'])

    @api.login_required(oauth_scopes=['datasets:write'])
    @api.parameters(AddPublisherParameters())
    @api.response(PublisherSchema())
    def post(self, args):
        """Create a new publisher
        """
        with api.commit_or_abort(
                db.session,
                default_error_message="Failed to create a new publisher."):
            new_publisher = Publisher.create(**args)
            db.session.add(new_publisher)
        return new_publisher
Beispiel #7
0
class Users(Resource):
    """
    Manipulations with users.
    """

    @api.login_required(oauth_scopes=['users:read'])
    @api.permission_required(permissions.AdminRolePermission())
    @api.parameters(PaginationParameters())
    @api.response(schemas.UserSchema(many=True))
    def get(self, args):
        """
        List of all users.

        Returns a list of users starting from ``offset`` limited by ``limit``
        parameter.
        """
        return User.query.offset(args['offset']).limit(args['limit'])

    @api.parameters(parameters.AddUserParameters())
    @api.response(schemas.UserSchema())
    @api.doc(id='create_user')
    def post(self, args):
        """
        Create a new user.
        """
        with api.commit_or_abort(
                db.session,
                default_error_message="Failed to create a new user."
        ):
            new_user = User(
                is_active=True,
                is_regular_user=True,
                **args
            )
            db.session.add(new_user)
        return new_user
Beispiel #8
0
class DatasetResource(Resource):
    """
    Manipulations with datasets
    """

    @api.parameters(PaginationParameters())
    @api.response(DatasetSchema(many=True))
    def get(self, args):
        """
        List of all datasets

        :param args: query parameters
        :return: a list of datasets starting from ``offset`` limited by
        ``limit`` parameter.
        """
        return Dataset.query.filter_by(deleted=False) \
            .offset(args['offset']) \
            .limit(args['limit'])

    @api.login_required(oauth_scopes=['datasets:write'])
    @api.parameters(AddDatasetParameters())
    @api.response(DatasetSchema())
    @api.response(code=HTTPStatus.CONFLICT)
    def post(self, args):
        """Create a new dataset
        """
        with api.commit_or_abort(
                db.session,
                default_error_message="Failed to create a new dataset."
        ):
            contributor = current_user
            args['contributor_id'] = contributor.id

            if 'license_name' in args:
                license_name = args.pop('license_name')
                try:
                    license = License.get(license_name=license_name)
                except ObjectDoesNotExist:
                    log.warning('License "{}" not found, and created automatically.'.format(license_name))
                    license = License.create(name=license_name)
                args['license_id'] = license.id

            if 'organization_name' in args:
                organization_name = args.pop('organization_name')
                try:
                    organization = Organization.get(org_name=organization_name)
                except ObjectDoesNotExist:
                    log.warning('Organization "{}" not found, and created automatically.'.format(organization_name))
                    organization = Organization.create(name=organization_name)
                args['organization_id'] = organization.id

            if 'publisher_name' in args:
                publisher_name = args.pop('publisher_name')
                try:
                    publisher = Publisher.get(publisher_name=publisher_name)
                except ObjectDoesNotExist:
                    log.warning('Publisher "{}" not found, and created automatically.'.format(publisher_name))
                    publisher = Publisher.create(name=publisher_name)
                args['publisher_id'] = publisher.id

            return Dataset.create(**args)