# -*- coding: utf-8 -*-

from sqlalchemy.orm import validates

from . import db
from ..utils.validators import validate_cuit, validate_cbu

class Bank(db.Model):
    __tablename__ = 'bank'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True, nullable=False)
    bcra_code = db.Column(db.Unicode(8))
    cuit = db.Column(db.Unicode(11))
    # TODO: Add bank logo, to quickly identify

    @validates('cuit')
    def cuit_is_valid(self, key, cuit):
        if not validate_cuit(cuit):
            raise ValueError('CUIT Invalid')
        return cuit

class BankAccount(db.Model):
    __tablename__ = 'bank_account'

    id = db.Column(db.Integer, primary_key=True)
    branch = db.Column(db.Unicode)
    acc_type = db.Column(db.Unicode)

    number = db.Column(db.Unicode)
    owner = db.Column(db.Unicode)
    cbu = db.Column(db.Unicode)

    bank_id = db.Column(db.Integer, db.ForeignKey('bank.id'), nullable=False)
    bank = db.relationship(Bank, backref='accounts')

    entity_id = db.Column(db.Integer, db.ForeignKey('entity.id'), nullable=False)
    entity = db.relationship('Entity', backref='bank_accounts', lazy='joined')

    @validates('cbu')
    def cbu_is_valid(self, key, cbu):
        if not validate_cbu(cbu):
            raise ValueError('CBU invalid')
        return cbu

@@ -333,16 +333,29 @@
 icode(8)
+, unique=True
 )%0A    cu
@@ -383,16 +383,29 @@
 code(11)
+, unique=True
 )%0A    # 
@@ -466,155 +466,674 @@
 es('
-cuit')%0A    def cuit_is_valid(self, key, cuit):%0A        if not validate_cuit(cuit):%0A            raise ValueError('CUIT Invalid')%0A        return cuit
+name')%0A    def name_is_unique(self, key, name):%0A        if self.query.filter_by(name=name).first():%0A            raise ValueError(%22Name must be unique%22)%0A        return name%0A%0A    @validates('cuit')%0A    def cuit_is_valid(self, key, cuit):%0A        if not validate_cuit(cuit):%0A            raise ValueError('CUIT Invalid')%0A        if self.query.filter_by(cuit=cuit).first():%0A            raise ValueError('CUIT must be unique')%0A        return cuit%0A%0A    @validates('bcra_code')%0A    def bcra_code_is_unique(self, key, bcra_code):%0A        if self.query.filter_by(bcra_code=bcra_code).first():%0A            raise ValueError(%22'bcra_code' must be unique%22)%0A        return bcra_code
 %0A%0Acl

Add aditional restrictions and validations to Bank model