from flask.ext.wtf import Form, TextField, FloatField, Required, SelectField
from flask.ext.wtf import AnyOf
from wtforms.ext.dateutil.fields import DateField
from .models import EventType

univals = [Required()]


def _get_choices(a_class, value_field, *args):
	result = a_class.query.order_by('name').all()
	return [(getattr(x, value_field), '%s' % ', '.join(args).title())
		for x in result]


def _get_validators(a_class, value_field):
	result = a_class.query.order_by('name').all()
	values = [getattr(x, value_field) for x in result]
	values = sorted(values)
	return [Required(), AnyOf(values, message=u'Invalid value, must be one of:'
		'%(values)s')]


class CommodityForm(Form):
	cusip = TextField('CUSIP', description='Stock CUSIP', validators=univals)
	symbol = TextField('Ticker Symbol', description='Usually 3 or 4 letters',
		validators=univals)
	name = TextField('Stock Name', description='Stock Name', validators=univals)


class EventTypeForm(Form):
	name = TextField('Type Name', description='Type of event',
		validators=univals)
	unit = TextField('Unit', description='Unit of measurement',
		validators=univals)


class EventForm(Form):
	symbol = TextField('Symbol', description='Stock ticker symbol',
		validators=univals)
	event_type_id = SelectField('Event Type', description='Type of event',
		coerce=int)
	value = FloatField('Value', description='Amount the event was worth',
		validators=univals)
	date = DateField('Date', description='Date the event happened',
		validators=univals)

	@classmethod
	def new(cls):
		form = cls()
		form.event_type_id.choices = _get_choices(EventType, 'id', 'name',
			'unit')

		form.event_type_id.validators = _get_validators(EventType, 'id')
		return form


class PriceForm(Form):
	commodity_id = SelectField('Stock', description='Stock', coerce=int)
	currency_id = SelectField('Currency',
		description='Currency the price is in', coerce=int)
	close = FloatField('Close', description='End of day closing price',
		validators=univals)
	date = DateField('Date', description='Closing date',
		validators=univals)

	@classmethod
	def new(cls):
		form = cls()
		form.commodity_id.choices = _get_choices(Commodity, 'id', 'Symbol')
		form.commodity_id.validators = _get_validators(Commodity, 'id')
		form.currency_id.choices = _get_choices(Commodity, 'id', 'Symbol')
		form.currency_id.validators = _get_validators(Commodity, 'id')
		return form

@@ -1,28 +1,63 @@
+from itertools import imap, repeat%0A
 from flask.ext.wtf import Fo
@@ -101,24 +101,24 @@
 SelectField%0A
-
 from flask.e
@@ -216,16 +216,27 @@
 ventType
+, Commodity
 %0A%0Aunival
@@ -326,38 +326,39 @@
 .query.order_by(
-'name'
+args%5B0%5D
 ).all()%0A%09return 
@@ -350,22 +350,25 @@
 .all()%0A%09
-return
+choices =
  %5B(getat
@@ -386,17 +386,19 @@
 _field),
- 
+%0A%09%09
 '%25s' %25 '
@@ -410,41 +410,73 @@
 oin(
-args).title())%0A%09%09for x in result%5D
+imap(getattr, repeat(x), args))) for x in result%5D%0A%09return choices
 %0A%0A%0Ad
@@ -2246,33 +2246,33 @@
 mmodity, 'id', '
-S
+s
 ymbol')%0A%09%09form.c
@@ -2389,17 +2389,17 @@
  'id', '
-S
+s
 ymbol')%0A

Fix choices in select fields