# from __future__ import print_function
import savalidation.validators as val

from datetime import datetime as dt, date as d

from app import db
from savalidation import ValidationMixin
from flask.ext.sqlalchemy import SQLAlchemy
# from sqlalchemy.schema import UniqueConstraint

class EventType(db.Model, ValidationMixin):
	__table_args__ = (db.UniqueConstraint('name', 'unit'), {})
	id = db.Column(db.Integer, primary_key=True)
	utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
	utc_updated = db.Column(db.DateTime, nullable=False, default=dt.utcnow(),
		onupdate=dt.utcnow())

	name = db.Column(db.String(64), nullable=False, unique=True)
	unit = db.Column(db.String(32), nullable=False, default='USD')

	# validation
	val.validates_constraints()

	def __init__(self, name=None, unit=None):
		self.name = name
		self.unit = unit

	def __repr__(self):
		return '<Type(%r, %r)>' % (self.name, self.unit)

class Event(db.Model, ValidationMixin):
	__table_args__ = (db.UniqueConstraint('symbol', 'date', 'event_type_id',
		'value'), {})

	id = db.Column(db.Integer, primary_key=True)
	utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
	utc_updated = db.Column(db.DateTime, nullable=False, default=dt.utcnow(),
		onupdate=dt.utcnow())

	symbol = db.Column(db.String(12), nullable=False)
	value = db.Column(db.Float, nullable=False)
	event_type_id = db.Column(db.Integer, db.ForeignKey('event_type.id'), nullable=False)
	type = db.relationship('EventType', backref='events', lazy='joined')
	date = db.Column(db.Date, nullable=False, default=d.today())

	# validation
	val.validates_constraints()

	def __init__(self, symbol=None, value=None, type=None, event_type_id=None,
		date=None):

		self.symbol = symbol
		self.value = value
		self.event_type_id = event_type_id
		self.type = type
		self.date = (date or d.today())

	def __repr__(self):
		return ('<Event(%r, %r, %r, %r)>'
			% (self.symbol, self.value, self.event_type_id, self.date))

class Price(db.Model, ValidationMixin):
	__table_args__ = (db.UniqueConstraint('commodity_id', 'currency_id',
		'date'), {})

	id = db.Column(db.Integer, primary_key=True)
	utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
	utc_updated = db.Column(db.DateTime, nullable=False, default=dt.utcnow(),
		onupdate=dt.utcnow())

	commodity_id = db.Column(db.Integer, db.ForeignKey('commodity.id'), nullable=False)
	commodity = db.relationship('Commodity', primaryjoin='Commodity.id==Price.commodity_id', backref='commodity_prices', lazy='joined')
	currency_id = db.Column(db.Integer, db.ForeignKey('commodity.id'), nullable=False)
	currency = db.relationship('Commodity', primaryjoin='Commodity.id==Price.currency_id', backref='currency_prices', lazy='joined')
	date = db.Column(db.Date, nullable=False, default=d.today())
	close = db.Column(db.Float, nullable=False)

	# validation
	val.validates_constraints()

	def __repr__(self):
		return ('<Price(%r, %r, %r, %r)>'
			% (self.close, self.commodity_id, self.currency_id, self.date))

class Commodity(db.Model, ValidationMixin):
	id = db.Column(db.Integer, primary_key=True)
	utc_created = db.Column(db.DateTime, nullable=False, default=dt.utcnow())
	utc_updated = db.Column(db.DateTime, nullable=False, default=dt.utcnow(),
		onupdate=dt.utcnow())

	cusip = db.Column(db.String(16), unique=True, nullable=False)
	symbol = db.Column(db.String(12), unique=True, nullable=False)
	name = db.Column(db.String(64), nullable=False, unique=True)

	# validation
	val.validates_constraints()

	def __repr__(self):
		return ('<Commodity(%r, %r)>' % (self.symbol, self.name))

@@ -274,16 +274,17 @@
 traint%0A%0A
+%0A
 class Ev
@@ -926,16 +926,17 @@
 .unit)%0A%0A
+%0A
 class Ev
@@ -1441,25 +1441,27 @@
 t_type.id'),
- 
+%0A%09%09
 nullable=Fal
@@ -1984,24 +1984,25 @@
 elf.date))%0A%0A
+%0A
 class Price(
@@ -2396,33 +2396,35 @@
 'commodity.id'),
- 
+%0A%09%09
 nullable=False)%0A
@@ -2456,32 +2456,77 @@
 hip('Commodity',
+ backref='commodity_prices',%0A%09%09lazy='joined',
  primaryjoin='Co
@@ -2560,51 +2560,8 @@
 _id'
-, backref='commodity_prices', lazy='joined'
 )%0A%09c
@@ -2625,17 +2625,19 @@
 ty.id'),
- 
+%0A%09%09
 nullable
@@ -2684,16 +2684,60 @@
 modity',
+ backref='currency_prices',%0A%09%09lazy='joined',
  primary
@@ -2778,50 +2778,8 @@
 _id'
-, backref='currency_prices', lazy='joined'
 )%0A%09d
@@ -2977,32 +2977,32 @@
 , %25r, %25r, %25r)%3E'%0A
-
 %09%09%09%25 (self.close
@@ -3053,16 +3053,17 @@
 date))%0A%0A
+%0A
 class Co

Fix pep8 errors