import datetime
import math
from sqlalchemy import Column, Integer, String, ForeignKey, Date, Float
from sqlalchemy.orm import relationship
from flask.ext.appbuilder.models.mixins import AuditMixin, BaseMixin, FileColumn, ImageColumn
from flask.ext.appbuilder import Model


class Group(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)

    def __repr__(self):
        return self.name


class ProductManufacturer(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)

    def __repr__(self):
        return self.name


class ProductModel(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)
    product_manufacturer_id = Column(Integer, ForeignKey('product_manufacturer.id'), nullable=False)
    product_manufacturer = relationship("ProductManufacturer")

    def __repr__(self):
        return self.name


class Product(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)
    product_manufacturer_id = Column(Integer, ForeignKey('product_manufacturer.id'), nullable=False)
    product_manufacturer = relationship("ProductManufacturer")
    product_model_id = Column(Integer, ForeignKey('product_model.id'), nullable=False)
    product_model = relationship("ProductModel")

    def __repr__(self):
        return self.name


class Gender(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)

    def __repr__(self):
        return self.name


def test():
        return math.pi - 1.0


class FloatModel(Model):
    id = Column(Integer, primary_key=True)
    value = Column(Float, nullable = False, default=test)


    def __repr__(self):
        return self.value


class Contact(Model):
    id = Column(Integer, primary_key=True)
    name =  Column(String(150), unique = True, nullable=False)
    address = Column(String(564))
    birthday = Column(Date, nullable=True)
    personal_phone = Column(String(20))
    personal_celphone = Column(String(20))
    group_id = Column(Integer, ForeignKey('group.id'), nullable=False)
    group = relationship("Group")
    gender_id = Column(Integer, ForeignKey('gender.id'), nullable=False)
    gender = relationship("Gender")

    def __repr__(self):
        return self.name

    def month_year(self):
        return datetime.datetime(self.birthday.year, self.birthday.month, 1)

    def year(self):
        return datetime.datetime(self.birthday.year, 1, 1)

import datetime
import math
from sqlalchemy import Column, Integer, String, ForeignKey, Date, Float
from sqlalchemy.orm import relationship
from flask.ext.appbuilder.models.mixins import AuditMixin, BaseMixin, FileColumn, ImageColumn
from flask.ext.appbuilder import Model

mindate = datetime.date(datetime.MINYEAR, 1, 1)

class Group(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)

    def __repr__(self):
        return self.name


class ProductManufacturer(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)

    def __repr__(self):
        return self.name


class ProductModel(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)
    product_manufacturer_id = Column(Integer, ForeignKey('product_manufacturer.id'), nullable=False)
    product_manufacturer = relationship("ProductManufacturer")

    def __repr__(self):
        return self.name


class Product(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)
    product_manufacturer_id = Column(Integer, ForeignKey('product_manufacturer.id'), nullable=False)
    product_manufacturer = relationship("ProductManufacturer")
    product_model_id = Column(Integer, ForeignKey('product_model.id'), nullable=False)
    product_model = relationship("ProductModel")

    def __repr__(self):
        return self.name


class Gender(Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique = True, nullable=False)

    def __repr__(self):
        return self.name


def test():
        return math.pi - 1.0


class FloatModel(Model):
    id = Column(Integer, primary_key=True)
    value = Column(Float, nullable = False, default=test)


    def __repr__(self):
        return self.value


class Contact(Model):
    id = Column(Integer, primary_key=True)
    name =  Column(String(150), unique = True, nullable=False)
    address = Column(String(564))
    birthday = Column(Date, nullable=True)
    personal_phone = Column(String(20))
    personal_celphone = Column(String(20))
    group_id = Column(Integer, ForeignKey('group.id'), nullable=False)
    group = relationship("Group")
    gender_id = Column(Integer, ForeignKey('gender.id'), nullable=False)
    gender = relationship("Gender")

    def __repr__(self):
        return self.name

    def month_year(self):
        date = self.birthday or mindate
        return datetime.datetime(date.year, date.month, 1) or mindate

    def year(self):
        date = self.birthday or mindate
        return datetime.datetime(date.year, 1, 1)
        

Update models.py