import functools
from datetime import datetime, date, time
from decimal import Decimal

from sqlalchemy import Column
from sqlalchemy.orm import Query, CompositeProperty
from sqlalchemy.orm.attributes import InstrumentedAttribute
from sqlalchemy.orm.relationships import RelationshipProperty
from sqlalchemy.ext.hybrid import HYBRID_PROPERTY
from sqlalchemy.ext.associationproxy import ASSOCIATION_PROXY
from sqlalchemy.inspection import inspect
from sqlalchemy_dict import DefaultFormatter


class BaseModel(object):
    __formatter__ = DefaultFormatter

    @classmethod
    def get_column(cls, column):
        if isinstance(column, str):
            mapper = inspect(cls)
            return mapper.columns[column]
        return column

    @classmethod
    def import_value(cls, column, v):
        c = cls.get_column(column)
        if isinstance(c, Column) or isinstance(c, InstrumentedAttribute):
            if c.type.python_type is bool and not isinstance(v, bool):
                return str(v).lower() == 'true'
        return v

    @classmethod
    def prepare_for_export(cls, column, v):
        param_name = column.info.get('dict_key') or cls.__formatter__.export_key(column.key)
        if hasattr(column, 'property') and isinstance(column.property,
                                                      RelationshipProperty) and column.property.uselist:
            result = [c.to_dict() for c in v]

        elif hasattr(column, 'property') and isinstance(column.property, CompositeProperty):
            result = v.__composite_values__()

        elif v is None:
            result = v

        elif isinstance(v, datetime):
            result = cls.__formatter__.export_datetime(v)

        elif isinstance(v, date):
            result = cls.__formatter__.export_date(v)

        elif isinstance(v, time):
            result = cls.__formatter__.export_time(v)

        elif hasattr(v, 'to_dict'):
            result = v.to_dict()

        elif isinstance(v, Decimal):
            result = str(v)

        else:
            result = v

        return param_name, result

    def update_from_dict(self, context):
        for column, value in self.extract_data_from_dict(context):
            setattr(
                self,
                column.key[1:] if column.key.startswith('_') else column.key,
                self.import_value(column, value)
            )

    @classmethod
    def iter_columns(cls, relationships=True, synonyms=True, composites=True, use_inspection=True, hybrids=True):
        if use_inspection:
            mapper = inspect(cls)
            for k, c in mapper.all_orm_descriptors.items():

                if k == '__mapper__':
                    continue

                if c.extension_type == ASSOCIATION_PROXY:
                    continue

                if (not hybrids and c.extension_type == HYBRID_PROPERTY) \
                        or (not relationships and k in mapper.relationships) \
                        or (not synonyms and k in mapper.synonyms) \
                        or (not composites and k in mapper.composites):
                    continue

                yield getattr(cls, k)

        else:
            # noinspection PyUnresolvedReferences
            for c in cls.__table__.c:
                yield c

    @classmethod
    def iter_dict_columns(cls, include_readonly_columns=True, include_protected_columns=False, **kw):
        for c in cls.iter_columns(**kw):
            if (not include_protected_columns and c.info.get('protected')) or \
                    (not include_readonly_columns and c.info.get('readonly')):
                continue

            yield c

    @classmethod
    def extract_data_from_dict(cls, context):
        for c in cls.iter_dict_columns(include_protected_columns=True, include_readonly_columns=False):
            param_name = c.info.get('dict_key', cls.__formatter__.export_key(c.key))

            if param_name in context:
                value = context[param_name]
                # Ensuring the python type, and ignoring silently if python type is not specified
                try:
                    c.type.python_type
                except NotImplementedError:
                    yield c, value
                    continue

                if c.type.python_type == datetime:
                    yield c, cls.__formatter__.import_datetime(value)

                elif c.type.python_type == date:
                    yield c, cls.__formatter__.import_date(value)

                elif c.type.python_type == time:
                    yield c, cls.__formatter__.import_time(value)

                else:
                    yield c, value

    def to_dict(self):
        result = {}
        for c in self.iter_dict_columns():
            result.setdefault(*self.prepare_for_export(c, getattr(self, c.key)))
        return result

    @classmethod
    def dump_query(cls, query=None):
        result = []
        for o in query:
            result.append(o.to_dict())
        return result

    @classmethod
    def expose(cls, func):

        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            result = func(*args, **kwargs)
            if result is None:
                raise ValueError('No result')
            if isinstance(result, Query):
                return cls.dump_query(result)
            return result

        return wrapper

@@ -2475,29 +2475,8 @@
 rue,
- use_inspection=True,
  hyb
@@ -2491,39 +2491,8 @@
 e):%0A
-        if use_inspection:%0A    
     
@@ -2509,36 +2509,32 @@
  = inspect(cls)%0A
-    
         for k, c
@@ -2586,20 +2586,16 @@
         
-    
 if k == 
@@ -2616,36 +2616,32 @@
                 
-   
- 
 continue%0A%0A      
@@ -2626,36 +2626,32 @@
       continue%0A%0A
-    
             if c
@@ -2696,36 +2696,32 @@
                 
-  
-  
 continue%0A%0A      
@@ -2706,36 +2706,32 @@
       continue%0A%0A
-    
             if (
@@ -2797,36 +2797,32 @@
                 
-    
 or (not relation
@@ -2856,28 +2856,24 @@
 ionships) %5C%0A
-    
             
@@ -2937,36 +2937,32 @@
                 
-    
 or (not composit
@@ -3001,36 +3001,32 @@
                 
-    
 continue%0A%0A      
@@ -3010,37 +3010,32 @@
        continue%0A
-%0A    
             yiel
@@ -3057,135 +3057,8 @@
 k)%0A%0A
-        else:%0A            # noinspection PyUnresolvedReferences%0A            for c in cls.__table__.c:%0A                yield c%0A%0A
     

Drop use_inspection from iter_columns, closes #3