def setup_schema_fn():
        for class_ in ActivityDetailBase.__subclasses__() + [
                Equipment, NOWApplicationDocumentXref
        ]:
            if hasattr(class_, "__tablename__"):
                try:
                    if class_.__name__.endswith("Schema"):
                        raise ModelConversionError(
                            "For safety, setup_schema can not be used when a"
                            "Model class ends with 'Schema'")

                    class Meta(object):
                        model = class_
                        ordered = True
                        include_fk = True
                        sqla_session = db.session
                        model_converter = CoreConverter
                        exclude = ('create_user', 'create_timestamp',
                                   'update_user', 'update_timestamp')

                    schema_class_name = "%sSchema" % class_.__name__

                    class ModelSchema2(ModelSchema):
                        activity_type_code = fields.String(dump_only=True)
                        activity_detail_id = fields.Integer(dump_only=True)

                    schema_class = type(schema_class_name, (ModelSchema2, ),
                                        {"Meta": Meta})

                    setattr(class_, "_schema", schema_class)
                    print(f'created {class_}')
                except Exception as e:
                    raise e
Example #2
0
    def setup_schema_fn():
        for class_ in ActivityDetailBase.__subclasses__() + [
                Equipment, NOWApplicationDocumentXref, Bond
        ] + sub_models.model_list:
            if hasattr(class_, "__tablename__"):
                try:
                    if class_.__name__.endswith("Schema"):
                        raise ModelConversionError(
                            "For safety, setup_schema can not be used when a"
                            "Model class ends with 'Schema'")
                    exclude_columns = AUDIT_COLUMNS if AuditMixin in class_.__bases__ else (
                    )

                    class Meta(object):
                        model = class_
                        ordered = True
                        include_fk = True
                        sqla_session = db.session
                        model_converter = CoreConverter
                        exclude = exclude_columns

                    # After the schema is created on the class this looks for any schemas that have deferred the creation of
                    # fields and validation. If found they are created and added to the proper schema.
                    for k, v in class_._ModelSchema.__dict__.items():
                        if type(v) == FieldTemplate:
                            current_app.logger.debug(
                                f'creating field for {k} on {class_}')
                            class_._ModelSchema._declared_fields[k] = v.field(
                                validate=validate.OneOf(
                                    choices=STATIC_DATA[v.one_of]))
                    schema_class_name = "%sSchema" % class_.__name__
                    schema_class = type(schema_class_name,
                                        (class_._ModelSchema, ),
                                        {"Meta": Meta})

                    setattr(class_, "_schema", schema_class)
                    current_app.logger.debug(f'created schema for {class_}')
                except Exception as e:
                    raise e

        for class_ in Base._decl_class_registry.values():
            if hasattr(class_, "_schema"):
                try:
                    mapper = inspect(class_)
                    for rel in mapper.relationships:
                        if hasattr(rel.entity.class_, "_schema"):
                            current_app.logger.debug(
                                f'creating nested schema on relationship: {rel.key}'
                            )
                            class_._schema._declared_fields[
                                rel.key] = fields.Nested(
                                    rel.entity.class_._schema,
                                    many=rel.uselist)
                            #exclude=[rel.backref.key] + [pk.name for pk in mapper.primary_keys])
                except Exception as e:
                    raise e
Example #3
0
    def get_model_schema(self, cls, selected_fields=None, exclude_fields=None):
        if hasattr(cls, '__tablename__'):
            if cls.__name__.endswith('Schema'):
                raise ModelConversionError(
                    "For safety, ``get_model_schema`` can not be used when a "
                    "Model class ends with 'Schema'")

            class Meta:
                model = cls
                fields = selected_fields
                exclude = exclude_fields
                model_converter = self.ModelConverter
                sqla_session = self.db.session

            schema_class_name = '%sSchema' % cls.__name__

            return type(schema_class_name, (ModelSchema, ), {'Meta': Meta})
    def setup_schema_fn():
        for class_ in Base._decl_class_registry.values():
            if hasattr(class_, "__tablename__"):
                if class_.__name__.endswith("Schema"):
                    raise ModelConversionError(
                        "For safety, setup_schema can not be used when a"
                        "Model class ends with 'Schema'")

                class Meta(object):
                    model = class_
                    sqla_session = session

                schema_class_name = "%sSchema" % class_.__name__

                schema_class = type(schema_class_name, (ModelSchema, ),
                                    {"Meta": Meta})

                setattr(class_, "__marshmallow__", schema_class)
Example #5
0
        def add_schema(cls):
            if hasattr(cls, '__tablename__'):
                if cls.__name__.endswith('Schema'):
                    raise ModelConversionError(
                        "For safety, setup_schema can not be used when a "
                        "Model class ends with 'Schema'")

                class Meta:
                    model = cls
                    model_converter = self.ModelConverter
                    sqla_session = self.db.session

                schema_class_name = '%sSchema' % cls.__name__

                schema_class = type(schema_class_name, (ModelSchema, ),
                                    {'Meta': Meta})

                setattr(cls, '__marshmallow__', schema_class)
Example #6
0
    def setup_schema_fn():
        # Generate missing schemas
        for class_ in Base._decl_class_registry.values():
            if hasattr(class_, '__tablename__') and not hasattr(
                    class_, '__marshmallow__'):
                if class_.__name__.endswith('Schema'):
                    raise ModelConversionError(
                        "For safety, setup_schema can not be used when a"
                        "Model class ends with 'Schema'")

                class Meta(BaseSchema.Meta):
                    model = class_

                schema_class_name = '%sSchema' % class_.__name__

                schema_class = type(schema_class_name, (BaseSchema, ),
                                    {'Meta': Meta})

                setattr(class_, '__marshmallow__', schema_class)
Example #7
0
    def setup_schema_fn():
        for class_ in Base._decl_class_registry.values():
            if hasattr(class_, "__tablename__"):
                if class_.__name__.endswith("Schema"):
                    raise ModelConversionError(
                        "For safety, setup_schema can not be used when a"
                        "Model class ends with 'Schema'")

                class Meta(object):
                    model = class_
                    load_instance = True
                    include_fk = True

                schema_class_name = "%sSchema" % class_.__name__

                schema_class = type(schema_class_name,
                                    (SQLAlchemyAutoSchema, ), {"Meta": Meta})

                setattr(class_, "__schema__", schema_class)
    def setup_schema_fn():
        for class_ in BaseDao._decl_class_registry.values():
            if hasattr(class_, '__tablename__'):
                if class_.__name__.endswith('Schema'):
                    raise ModelConversionError(
                        "For safety, setup_schema can not be used when a"
                        "Model class ends with 'Schema'"
                    )

                class Meta(object):
                    model = class_
                    sqla_session = session
                    dump_only = ('pkId', 'created', 'modified')

                schema_class_name = '%sSchema' % class_.__name__

                schema_class = type(
                    schema_class_name,
                    (ModelSchema,),
                    {'Meta': Meta}
                )

                setattr(class_, '__marshmallow__', schema_class)
Example #9
0
    def _setup_marshmallow(self, class_):
        if class_.__name__.endswith("Schema"):
            raise ModelConversionError(
                "For safety, setup_schema can not be used when a"
                "Model class ends with 'Schema'")

        class Meta(object):
            model = class_
            # sqla_session = DBConn.scoped_session
            include_fk = True
            load_instance = True
            include_relationships = True

        # for rel in inspect(class_).relationships:
        #     to_nest = rel.mapper.class_
        #     only = [col.name for col in to_nest.__table__.columns]
        #     nested = Nested(self._marsh_name(to_nest), many=True, only=only)
        #     setattr(Meta, rel.class_attribute.key, nested)

        schema_class_name = self._marsh_name(class_)
        schema_class = type(schema_class_name, (SQLAlchemyAutoSchema, ),
                            {"Meta": Meta})
        setattr(class_, "__marshmallow__", schema_class)