import re
import uuid
import datetime

from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, DateTime
from sqlalchemy.orm import relationship
import sqlalchemy.types as types
from sqlalchemy.types import String
from flask import g


_camelcase_re = re.compile(r'([A-Z]+)(?=[a-z0-9])')

class FileColumn(types.TypeDecorator):
    """
    Extends SQLA to support a File Column
    usefull to auto form generation.
    
    """
    impl = types.Text
    

class ImageColumn(types.TypeDecorator):
    impl = types.Text



class BaseMixin(object):
    __table_args__ = {'extend_existing': True}

    def __new__(self):
        if not self.__tablename__:
            
            self.__tablename__ = _camelcase_re.sub(_join, self.__class__.__name__).lstrip('_')
            print "NEW", self.__tablename__

class AuditMixin(BaseMixin):
    """
    AuditMixin
    Mixin for models, add 4 cols to stamp, time and user on
    creation and modification
    """
    created_on = Column(DateTime, default=datetime.datetime.now,nullable=False)
    changed_on = Column(DateTime, default=datetime.datetime.now,
                            onupdate=datetime.datetime.now,nullable=False)

    @declared_attr
    def created_by_fk(cls):
        return Column(Integer, ForeignKey('ab_user.id'),
                default=cls.get_user_id,nullable=False)

    @declared_attr
    def created_by(cls):
        return relationship("User", primaryjoin='%s.created_by_fk == User.id'%cls.__name__)

    @declared_attr
    def changed_by_fk(cls):
        return Column(Integer, ForeignKey('ab_user.id'),
                default=cls.get_user_id,onupdate=cls.get_user_id,nullable=False)

    @declared_attr
    def changed_by(cls):
        return relationship("User", primaryjoin='%s.changed_by_fk == User.id'%cls.__name__)


    @classmethod
    def get_user_id(cls):
        return g.user.id

@@ -668,107 +668,264 @@
     
-def __new__(self):%0A        if not self.__tablename__:%0A            %0A            self.__tablename__ =
+@declared_attr%0A    def __tablename__(cls):%0A        def _join(match):%0A            word = match.group()%0A            if len(word) %3E 1:%0A                return ('_%25s_%25s' %25 (word%5B:-1%5D, word%5B-1%5D)).lower()%0A            return '_' + word.lower()%0A        print %22NEW%22,
  _ca
@@ -950,22 +950,11 @@
 in, 
-self.__class__
+cls
 .__n
@@ -984,43 +984,78 @@
     
-    print %22NEW%22, self.__tablename__
+return _camelcase_re.sub(_join, cls.__name__).lstrip('_')%0A            
 %0A%0Acl

Update mixins.py