Beispiel #1
0
    def _init_model(self, resource, model, meta):
        mapper = class_mapper(model)

        self.model = model

        if meta.id_attribute:
            self.id_column = getattr(model, resource.meta.id_attribute)
            self.id_attribute = meta.id_attribute
        else:
            self.id_column = mapper.primary_key[0]
            self.id_attribute = mapper.primary_key[0].name

        self.id_field = self._get_field_from_column_type(
            self.id_column, self.id_attribute, io="r"
        )
        self.default_sort_expression = self._get_sort_expression(
            model, meta, self.id_column
        )

        fs = resource.schema
        if meta.include_id:
            fs.set('$id', self.id_field)
        else:
            fs.set('$uri', fields.ItemUri(resource, attribute=self.id_attribute))

        if meta.include_type:
            fs.set('$type', fields.ItemType(resource))

        # resource name: use model table's name if not set explicitly
        if not hasattr(resource.Meta, 'name'):
            meta['name'] = model.__tablename__.lower()

        fs = resource.schema
        include_fields = meta.get('include_fields', None)
        exclude_fields = meta.get('exclude_fields', None)
        read_only_fields = meta.get('read_only_fields', ())
        write_only_fields = meta.get('write_only_fields', ())
        pre_declared_fields = {f.attribute or k for k, f in fs.fields.items()}

        for name, column in mapper.columns.items():
            if (
                (include_fields and name in include_fields)
                or (exclude_fields and name not in exclude_fields)
                or not (include_fields or exclude_fields)
            ):
                if column.primary_key or column.foreign_keys:
                    continue
                if name in pre_declared_fields:
                    continue

                io = "rw"
                if name in read_only_fields:
                    io = "r"
                elif name in write_only_fields:
                    io = "w"

                if "w" in io and not (column.nullable or column.default):
                    fs.required.add(name)
                fs.set(name, self._get_field_from_column_type(column, name, io=io))
Beispiel #2
0
    class Schema(object):
        name = fields.String(
            description=
            'A unique human-readable name to denote the query policy.')

        description = fields.String(
            description=
            'A description of what data the query policy is governing the access to.'
        )

        policyFilters = POLICY_FILTERS_SCHEMA

        resource = fields.ItemUri(
            'web.server.api.permission_api_models.BackendResource',
            attribute='resource_id',
        )
Beispiel #3
0
    def _init_model(self, resource, model, meta):
        super(PeeweeManager, self)._init_model(resource, model, meta)
        self.model = model

        if meta.id_attribute:
            self.id_attribute = meta.id_attribute
            self.id_column = model._meta.fields[meta.id_attribute]
        else:
            self.id_attribute = model._meta.primary_key.name
            self.id_column = model._meta.primary_key

        self.id_field = meta.id_field_class(attribute=self.id_attribute,
                                            io="r")

        if not hasattr(resource.Meta, 'name'):
            meta['name'] = model._meta.db_table.lower()

        fs = resource.schema
        if meta.include_id:
            fs.set('$id', self.id_field)
        else:
            fs.set('$uri', fields.ItemUri(resource,
                                          attribute=self.id_attribute))

        if meta.include_type:
            fs.set('$type', fields.ItemType(resource))

        include_fields = meta.get('include_fields', None)
        exclude_fields = meta.get('exclude_fields', None)
        read_only_fields = meta.get('read_only_fields', ())
        write_only_fields = meta.get('write_only_fields', ())
        pre_declared_fields = {f.attribute or k for k, f in fs.fields.items()}

        for name, column in model._meta.fields.items():
            if (include_fields and name in include_fields) or \
                    (exclude_fields and name not in exclude_fields) or \
                    not (include_fields or exclude_fields):
                if column.primary_key or name in model._meta.rel:
                    continue
                if name in pre_declared_fields:
                    continue

                args = ()
                kwargs = {}

                if isinstance(column, (pw.CharField, pw.TextField)):
                    field_class = fields.String
                    if hasattr(column, 'max_length') and column.max_length:
                        kwargs['max_length'] = column.max_length
                elif isinstance(column, pw.IntegerField):
                    field_class = fields.Integer
                elif isinstance(column, (pw.DecimalField, pw.FloatField)):
                    field_class = fields.Number
                elif isinstance(column, pw.BooleanField):
                    field_class = fields.Boolean
                elif isinstance(column, pw.DateField):
                    field_class = fields.Date
                elif isinstance(column, pw.DateTimeField):
                    field_class = fields.DateTime
                elif isinstance(column, pw.BlobField):
                    field_class = fields.raw
                elif isinstance(column, postgres_ext.ArrayField):
                    field_class = fields.Array
                    args = (fields.String, )
                elif postgres_ext and \
                        isinstance(column, postgres_ext.HStoreField):
                    field_class = fields.Object
                    args = (fields.String, )
                elif postgres_ext and \
                        isinstance(column, (postgres_ext.JSONField,
                                            postgres_ext.BinaryJSONField)):
                    field_class = fields.Raw
                    kwargs = {"schema": {}}
                else:
                    field_class = fields.String

                kwargs['nullable'] = column.null

                if column.default is not None:
                    kwargs['default'] = column.default

                io = "rw"
                if name in read_only_fields:
                    io = "r"
                elif name in write_only_fields:
                    io = "w"

                if not (column.null or column.default):
                    fs.required.add(name)

                fs.set(name, field_class(*args,
                                         io=io,
                                         attribute=name,
                                         **kwargs))
Beispiel #4
0
    description='An individual group with its constituent users and roles.',
    model=Group,
)
''' A regex pattern that only allows alphanumeric characters, dashes, underscores
and spaces.
'''
ALPHANUMERIC_AND_DELIMITER = r'(^[a-zA-Z0-9]+[a-zA-Z0-9-_ ]*[a-zA-Z0-9]+)$'

INTERNATIONALIZED_ALPHANUMERIC_AND_DELIMITER = (
    r'(^[A-zÀ-ÿ0-9]+[A-zÀ-ÿ0-9-_ ]*[A-zÀ-ÿ0-9]*)$')
INTERNATIONALIZED_ALPHANUMERIC_AND_DELIMITER_OR_EMPTY = (
    r'(^([A-zÀ-ÿ0-9]+[A-zÀ-ÿ0-9-_ ]*[A-zÀ-ÿ0-9]*))|(\s*)$')

HISTORY_CHANGE_FIELDS = {
    'user':
    fields.ItemUri('web.server.api.user_api_models.UserResource',
                   attribute='user_id'),
    'revisionId':
    fields.Integer(attribute='id',
                   description='The unique history identifier'),
    'objectUri':
    None,
    'changes':
    fields.Any(attribute='changes', description='History item changes'),
    'revisionDate':
    fields.DateTimeString(attribute='created',
                          description='History item created date'),
}


def generate_history_record_schema(uri_field, resource_name):
    updated_fields = HISTORY_CHANGE_FIELDS.copy()
    fields.String(), attribute='specification', formatter=get_dashboard_title, io='r'
)

DESCRIPTION_SCHEMA = fields.String(
    description='A short description of the dashboard.', nullable=True
)

AUTHOR_USERNAME_SCHEMA = fields.Custom(
    fields.Email(pattern=EMAIL_PATTERN, nullable=True),
    description='The author\'s username. If not visible to the user, this will be null.',
    attribute='author_username',
    io='r',
)

AUTHOR_URI_SCHEMA = fields.ItemUri(
    'web.server.api.user_api_models.UserResource', attribute='author_id'
)

RESOURCE_URI_SCHEMA = fields.ItemUri(
    'web.server.api.permission_api_models.BackendResource', attribute='resource_id'
)

CREATED_SCHEMA = fields.DateTimeString(
    description='When the dashboard was created.', attribute='created', io='r'
)

LAST_MODIFIED_SCHEMA = fields.DateTimeString(
    description='The last time the dashboard was modified.',
    attribute='last_modified_real',
    io='r',
)