from sqlobject import *
from turbogears.database import PackageHub
from mx import DateTime

hub = PackageHub("survey")
__connection__ = hub

class Survey(SQLObject):
  name = StringCol(length=200)
  description = StringCol()
  publish = BoolCol()
  questions = MultipleJoin('Question')
  answers = MultipleJoin('Answer')

class Question(SQLObject):
  text = StringCol()
  qType = EnumCol(enumValues=['text','textarea','radio','select','checkbox'])
  survey = ForeignKey('Survey')
  options = MultipleJoin('Option')

class Option(SQLObject):
  class sqlmeta:
    table = 'options_table'
  text = StringCol()
  question = ForeignKey('Question')

class Answer(SQLObject):
  answered = DateTimeCol(default=DateTime)
  survey = ForeignKey('Survey')
  values = MultipleJoin('AnswerValue')

class AnswerValue(SQLObject):
  value = StringCol()
  answer = ForeignKey('Answer')
  question = ForeignKey('Question')
 
Survey.createTable(ifNotExists=True)
Question.createTable(ifNotExists=True)
Option.createTable(ifNotExists=True)
Answer.createTable(ifNotExists=True)
AnswerValue.createTable(ifNotExists=True)

