Example #1
0
 def resolve_model_name(self):
     """Returns the model class corresponding to the remote object."""
     if "_metadata_novabase_classname" in self.wrapped_dict:
         return self.wrapped_dict["_metadata_novabase_classname"]
     elif self.wrapped_value is not None:
         return models.get_model_classname_from_tablename(self.wrapped_value.base)
     else:
         return "None"
Example #2
0
 def resolve_model_name(self):
     """Returns the model class corresponding to the remote object."""
     if "_metadata_novabase_classname" in self.wrapped_dict:
         return self.wrapped_dict["_metadata_novabase_classname"]
     elif self.wrapped_value is not None:
         return models.get_model_classname_from_tablename(
             self.wrapped_value.base)
     else:
         return "None"
Example #3
0
def get_relationships(obj, foreignkey_mode=False):
    from sqlalchemy.sql.expression import BinaryExpression, BooleanClauseList, BindParameter

    def filter_matching_column(clause, tablename):
        # return clause
        if tablename in str(clause.left):
            value = getattr(obj, clause.left.description)
            if value is None and clause.left.default is not None:
                value = clause.left.default.arg
            clause.left = BindParameter(key="totoo", value=value)
        if tablename in str(clause.right):
            value = getattr(obj, clause.right.description)
            if value is None and clause.right.default is not None:
                value = clause.right.default.arg
            clause.right = BindParameter(key="totoo", value=value)
        return clause

    import models

    result = []

    state = obj._sa_instance_state

    for field in obj._sa_class_manager:
        field_object = obj._sa_class_manager[field]
        field_column = state.mapper._props[field]

        contain_comparator = hasattr(field_object, "comparator")
        is_relationship = ("relationship" in str(field_object.comparator)
                           if contain_comparator else False
                           )
        if is_relationship:
            remote_local_pair = field_object.property.local_remote_pairs[0]

            local_fk_field = remote_local_pair[0].name
            local_fk_value = getattr(obj, local_fk_field)
            local_tablename = obj.__tablename__
            local_object_field = field
            local_object_type = None
            try:
                local_object_type = remote_local_pair[0].type
            except:
                pass
            local_object_value = getattr(obj, local_object_field)
            remote_object_field = remote_local_pair[1].name
            remote_object_tablename = str(remote_local_pair[1].table)
            is_list = field_object.property.uselist

            local_table_name = obj.__tablename__

            remote_class = models.get_model_class_from_name(models.get_model_classname_from_tablename(remote_object_tablename))
            expression = field_object.property.primaryjoin
            initial_expression=expression
            direction = str(field_object.property.direction).split("'")[1]

            if type(expression) == BinaryExpression:
                expression = [expression]

            if foreignkey_mode:
                corrected_expression = map(lambda  x: filter_matching_column(x, local_table_name), expression)
                expression = corrected_expression

            # to_many="TOMANY" in str(field_object.property.direction)
            to_many = is_list

            result += [RelationshipModel(
                local_fk_field,
                local_fk_value,
                local_object_field,
                local_object_value,
                local_object_type,
                local_tablename,
                remote_object_field,
                remote_object_tablename,
                is_list,
                remote_class=remote_class,
                expression=expression,
                initial_expression=initial_expression,
                to_many=to_many,
                obj=obj,
                direction=direction
            )]
    return result
Example #4
0
def get_relationships(obj, foreignkey_mode=False):
    from sqlalchemy.sql.expression import BinaryExpression, BooleanClauseList, BindParameter

    def filter_matching_column(clause, tablename):
        # return clause
        if tablename in str(clause.left):
            value = getattr(obj, clause.left.description)
            if value is None and clause.left.default is not None:
                value = clause.left.default.arg
            clause.left = BindParameter(key="totoo", value=value)
        if tablename in str(clause.right):
            value = getattr(obj, clause.right.description)
            if value is None and clause.right.default is not None:
                value = clause.right.default.arg
            clause.right = BindParameter(key="totoo", value=value)
        return clause

    import models

    result = []

    state = obj._sa_instance_state

    for field in obj._sa_class_manager:
        field_object = obj._sa_class_manager[field]
        field_column = state.mapper._props[field]

        contain_comparator = hasattr(field_object, "comparator")
        is_relationship = ("relationship" in str(field_object.comparator)
                           if contain_comparator else False)
        if is_relationship:
            remote_local_pair = field_object.property.local_remote_pairs[0]

            local_fk_field = remote_local_pair[0].name
            local_fk_value = getattr(obj, local_fk_field)
            local_tablename = obj.__tablename__
            local_object_field = field
            local_object_type = None
            try:
                local_object_type = remote_local_pair[0].type
            except:
                pass
            local_object_value = getattr(obj, local_object_field)
            remote_object_field = remote_local_pair[1].name
            remote_object_tablename = str(remote_local_pair[1].table)
            is_list = field_object.property.uselist

            local_table_name = obj.__tablename__

            remote_class = models.get_model_class_from_name(
                models.get_model_classname_from_tablename(
                    remote_object_tablename))
            expression = field_object.property.primaryjoin
            initial_expression = expression
            direction = str(field_object.property.direction).split("'")[1]

            if type(expression) == BinaryExpression:
                expression = [expression]

            if foreignkey_mode:
                corrected_expression = map(
                    lambda x: filter_matching_column(x, local_table_name),
                    expression)
                expression = corrected_expression

            # to_many="TOMANY" in str(field_object.property.direction)
            to_many = is_list

            result += [
                RelationshipModel(local_fk_field,
                                  local_fk_value,
                                  local_object_field,
                                  local_object_value,
                                  local_object_type,
                                  local_tablename,
                                  remote_object_field,
                                  remote_object_tablename,
                                  is_list,
                                  remote_class=remote_class,
                                  expression=expression,
                                  initial_expression=initial_expression,
                                  to_many=to_many,
                                  obj=obj,
                                  direction=direction)
            ]
    return result
Example #5
0
 def resolve_model_name(self):
     """Returns the model class corresponding to the remote object."""
     return models.get_model_classname_from_tablename(self.base)
Example #6
0
 def resolve_model_name(self):
     """Returns the model class corresponding to the remote object."""
     return models.get_model_classname_from_tablename(self.base)