from sqlalchemy import Column, Integer, String, Text, Table, ForeignKey
from sqlalchemy.orm import relationship
from app.database import Base

# many-many for issues and callees needs to be modeled like this :(
issue_callees = Table('issue_callees', Base.metadata,
	Column('issue_id', ForeignKey('issues.id'), primary_key=True),
	Column('callee_id', ForeignKey('callees.id'), primary_key=True)
)

# `Issue` is a single issue to call on
class Issue(Base):
	__tablename__ = 'issues'
	id = Column(Integer, primary_key=True)
	name = Column(String(255), unique=False)
	script = Column(Text)
	callees = relationship('Callee', secondary=issue_callees, back_populates='issues')

	def __init__(self, name, script):
		self.name = name
		self.script = script

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

	def as_dict(self):
		export_columns = []

		# remove columns that we don't want to export
		for col in self.__table__.columns:
			if col.name != "id":
				export_columns.append(col)

		return {c.name: getattr(self, c.name) for c in export_columns}

# `Callee` is a type representing a office or representative to call
class Callee(Base):
	__tablename__ = 'callees'
	id = Column(Integer, primary_key=True)
	name = Column(String(255), unique=False)
	phone = Column(String(12), unique=False)
	issues = relationship('Issue', secondary=issue_callees, back_populates='callees')

	def __init__(self, name, phone):
		self.name = name
		self.phone = phone

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


@@ -984,22 +984,29 @@
 col)%0A%0A%09%09
-return
+export_dict =
  %7Bc.name
@@ -1055,16 +1055,173 @@
 lumns%7D%0A%0A
+%09%09callee_array = %5B%5D%0A%09%09for callee in self.callees:%0A%09%09%09callee_array.append(callee.as_dict())%0A%09%09%0A%09%09export_dict%5B'callees'%5D = callee_array%0A%0A%09%09return export_dict%0A%0A
 # %60Calle
@@ -1624,32 +1624,32 @@
 __repr__(self):%0A
-
 %09%09return '%3CCalle
@@ -1648,28 +1648,279 @@
  '%3CCallee %25r%3E' %25 self.name%0A%0A
+%09def as_dict(self):%0A%09%09export_columns = %5B%5D%0A%0A%09%09# remove columns that we don't want to export%0A%09%09for col in self.__table__.columns:%0A%09%09%09if col.name != %22id%22:%0A%09%09%09%09export_columns.append(col)%0A%0A%09%09return %7Bc.name: getattr(self, c.name) for c in export_columns%7D%0A%0A%0A

Add some callees to those json representations.