from flask.ext.sqlalchemy import SQLAlchemy, iteritems
from sqlalchemy import Integer, String, Boolean
from sqlalchemy_utils import URLType, PasswordType

from server.app import app, trackPool, techriderPool, imagePool
from server.bands.forms import BandForm


db = SQLAlchemy(app)


class Band(db.Model):
    id = db.Column(Integer, primary_key=True)
    login = db.Column(String, unique=True)
    password = db.Column(PasswordType(schemes=['pbkdf2_sha512']))
    name = db.Column(String)
    email = db.Column(String)
    emailConfirmed = db.Column(Boolean, default=False)
    descp = db.Column(String)
    amount_members = db.Column(String)
    website = db.Column(URLType)
    youtube_id = db.Column(String)
    facebook_page = db.Column(String)
    phone = db.Column(String)
    city = db.Column(String)
    image = db.Column(String)
    techrider = db.Column(String)
    tracks = db.relationship('Track', backref='band', lazy='dynamic')

    def __init__(self, login, password):
        self.login = login
        self.name = login
        self.password = password

    def __repr__(self):
        return '<Band %r>' % (self.name)

    @property
    def is_tracks_valid(self):
        return 2 < self.tracks.count() < 6

    @property
    def is_profile_valid(self):
        bandForm = BandForm()
        bandForm.set_from_model(self)

        for name, field in iteritems(bandForm._fields):
            if not field.validate(self, []):
                return False
        return True


    @property
    def techrider_url(self):
        return techriderPool.url(self.techrider)

    @property
    def techrider_path(self):
        return techriderPool.path(self.techrider)
    
    @property
    def image_url(self):
        return imagePool.url(self.image)

    @property
    def image_path(self):
        return imagePool.path(self.image)
    



class Track(db.Model):
    id = db.Column(Integer, primary_key=True)
    band_id = db.Column(db.Integer, db.ForeignKey('band.id'))
    filename = db.Column(String)
    trackname = db.Column(String)

    @property
    def url(self):
        return trackPool.url(self.filename)

    @property
    def path(self):
        return trackPool.path(self.filename)


db.create_all()
@@ -95,17 +95,27 @@
  Boolean
+, DateTime
 %0A
-
 from sql
@@ -868,32 +868,105 @@
 .Column(String)%0A
+    state = db.Column(Integer)%0A    apply_timestamp = db.Column(DateTime)%0A
     tracks = db.
@@ -1757,21 +1757,17 @@
 hrider)%0A
-    
 %0A
+
     @pro
@@ -1871,32 +1871,32 @@
 age_path(self):%0A
+
         return i
@@ -1925,14 +1925,8 @@
 ge)%0A
-    %0A%0A
 %0A%0Acl

Add two fields to the data_structure we need later on