# Path: common/utilities.py
# def camel_to_delimiter_separated(name, glue='_'):
# def is_valid_email_address(address):
# def is_common_password(passwd):
# 
# Path: ourmarshmallow/fields.py
# class Relationship(marshmallow_jsonapi.fields.Relationship, marshmallow_sqlalchemy.fields.Related):
#     """
#     Combine the marshmallow-jsonapi.fields.Relationship with marshmallow-sqlalchemy.fields.Related.
#     """
#     def __init__(self, parent_self_url='', relationship_name='', parent_model=None, **kwargs):
#         """
#         :param str parent_self_url: Used to calculate self_url and related_url from a parent schema.
#         :param str relationship_name: Name of this relationship for self_url and related_url.
#         :param models.bases.BaseModel parent_model: Model Class of Schema containing this field.
#         """
#         # Calculate our relationship URLs beased on the parent schema's self_url
#         if parent_self_url and relationship_name and kwargs['self_url_kwargs']:
#             kwargs['self_url'] = '{0}/relationships/{1}'.format(parent_self_url, relationship_name)
#             kwargs['related_url'] = '{0}/{1}'.format(parent_self_url, relationship_name)
#             kwargs['related_url_kwargs'] = kwargs['self_url_kwargs']
# 
#         # Set the class of the model for the schema containing this field.
#         self.parent_model = parent_model
# 
#         super().__init__(**kwargs)


    from builtins import *  # pylint: disable=unused-wildcard-import,redefined-builtin,wildcard-import
from common import utilities
from .fields import Relationship
    import sys
import marshmallow_sqlalchemy



class ModelConverter(marshmallow_sqlalchemy.ModelConverter):
    """ Customize SQLAlchemy Model Converter to use JSON API Relationships. """

    # property2field will convert the Relationship to a List field if the prop.direction.name in
    # this mapping returns True. Hack to work around that since this mapping is only used there.
    # https://github.com/marshmallow-code/marshmallow-sqlalchemy/blob/af8304a33bfc11468d9ddb6c96e183964806d637/marshmallow_sqlalchemy/convert.py#L131
    DIRECTION_MAPPING = {
        'MANYTOONE': False,
        'MANYTOMANY': False,
        'ONETOMANY': False,
    }

    def _get_field_class_for_property(self, prop):
        """ Use our Relationship field type for SQLAlchemy relations instead. """
        if hasattr(prop, 'direction'):
            return Relationship
        return super()._get_field_class_for_property(prop)

    def _add_relationship_kwargs(self, kwargs, prop):
        """ Customize the kwargs for Relationship field based on prop. """
        # super()._add_relationship_kwargs(kwargs, prop)

        # All Schema names should be based on Model name.
        kwargs['schema'] = prop.mapper.class_.__name__ + 'Schema'
        # If the relation uses a list then the Relationship is many
        kwargs['many'] = prop.uselist
        # JSONAPI type is calculated from Model name to kebab-case.

# Path: common/utilities.py
# def camel_to_delimiter_separated(name, glue='_'):
# def is_valid_email_address(address):
# def is_common_password(passwd):
# 
# Path: ourmarshmallow/fields.py
# class Relationship(marshmallow_jsonapi.fields.Relationship, marshmallow_sqlalchemy.fields.Related):
#     """
#     Combine the marshmallow-jsonapi.fields.Relationship with marshmallow-sqlalchemy.fields.Related.
#     """
#     def __init__(self, parent_self_url='', relationship_name='', parent_model=None, **kwargs):
#         """
#         :param str parent_self_url: Used to calculate self_url and related_url from a parent schema.
#         :param str relationship_name: Name of this relationship for self_url and related_url.
#         :param models.bases.BaseModel parent_model: Model Class of Schema containing this field.
#         """
#         # Calculate our relationship URLs beased on the parent schema's self_url
#         if parent_self_url and relationship_name and kwargs['self_url_kwargs']:
#             kwargs['self_url'] = '{0}/relationships/{1}'.format(parent_self_url, relationship_name)
#             kwargs['related_url'] = '{0}/{1}'.format(parent_self_url, relationship_name)
#             kwargs['related_url_kwargs'] = kwargs['self_url_kwargs']
# 
#         # Set the class of the model for the schema containing this field.
#         self.parent_model = parent_model
# 
#         super().__init__(**kwargs)


# Path: ourmarshmallow/convert.py
    from builtins import *  # pylint: disable=unused-wildcard-import,redefined-builtin,wildcard-import
from common import utilities
from .fields import Relationship
    import sys
import marshmallow_sqlalchemy




class ModelConverter(marshmallow_sqlalchemy.ModelConverter):
    """ Customize SQLAlchemy Model Converter to use JSON API Relationships. """

    # property2field will convert the Relationship to a List field if the prop.direction.name in
    # this mapping returns True. Hack to work around that since this mapping is only used there.
    # https://github.com/marshmallow-code/marshmallow-sqlalchemy/blob/af8304a33bfc11468d9ddb6c96e183964806d637/marshmallow_sqlalchemy/convert.py#L131
    DIRECTION_MAPPING = {
        'MANYTOONE': False,
        'MANYTOMANY': False,
        'ONETOMANY': False,
    }

    def _get_field_class_for_property(self, prop):
        """ Use our Relationship field type for SQLAlchemy relations instead. """
        if hasattr(prop, 'direction'):
            return Relationship
        return super()._get_field_class_for_property(prop)

    def _add_relationship_kwargs(self, kwargs, prop):
        """ Customize the kwargs for Relationship field based on prop. """
        # super()._add_relationship_kwargs(kwargs, prop)

        # All Schema names should be based on Model name.
        kwargs['schema'] = prop.mapper.class_.__name__ + 'Schema'
        # If the relation uses a list then the Relationship is many
        kwargs['many'] = prop.uselist
        # JSONAPI type is calculated from Model name to kebab-case.

        kwargs['type_'] = utilities.camel_to_delimiter_separated(prop.mapper.class_.__name__,