# Path: template_maker/database.py
# class CRUDMixin(object):
# class Model(CRUDMixin, db.Model):
# class SurrogatePK(object):
#     def create(cls, **kwargs):
#     def update(self, commit=True, **kwargs):
#     def save(self, commit=True):
#     def delete(self, commit=True):
#     def get_by_id(cls, id):
# def ReferenceCol(tablename, nullable=False, ondelete=None, pk_name='id', **kwargs):
# 
# Path: template_maker/generator/models.py
# class DocumentBase(Model):
#     '''
#     Metadata about a new document, relates back to
#     a template
#     '''
# 
#     __tablename__ = 'document_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     name = Column(db.String(255))
#     # published = Column(db.Boolean, default=False)
#     template_id = ReferenceCol('template_base', ondelete='CASCADE')
# 
#     def __init__(self, created_at, updated_at, name, template_id):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.name = name
#         self.template_id = template_id
# 
# class DocumentPlaceholder(Model):
#     '''
#     A document's placeholder's value. Relates to the
#     document and the placeholder
#     '''
# 
#     __tablename__ = 'document_placeholder'
#     id = Column(db.Integer, primary_key=True)
#     document_id = ReferenceCol('document_base')
#     placeholder_id = ReferenceCol('template_placeholders', ondelete='CASCADE')
#     value = Column(db.Text)
# 
#     def __init__(self, document_id, placeholder_id):
#         self.document_id = document_id
#         self.placeholder_id = placeholder_id
# 
# Path: template_maker/builder/models.py
# class TemplateBase(Model):
#     '''
#     Basic metadata about a template
# 
#     Fields:
#     - created_at: when the template was created
#     - updated_at: when the template metadata was last updated
#     - title: the displayed title or name of the template
#     - description: a freetext description of the template
#     '''
# 
#     __tablename__ = 'template_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_text = db.relationship('TemplateSection', cascade='all,delete', lazy='dynamic')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     published = Column(db.Boolean, default=False)
#     section_order = Column(ARRAY(db.Integer))
#     # created_by = ReferenceCol('users')
# 
#     def __init__(self, created_at, updated_at, title, description):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.title = title
#         self.description = description
# 
# class TemplatePlaceholders(Model):
#     '''
#     The placeholders associated with each section
# 
#     Fields:
#     - name: The actual name of the placeholder
#     - template_id: Foreign Key back to the TemplateBase model
#     - section_id: Foreign Key back to the TemplateSection model
#     '''
# 
#     __tablename__ = 'template_placeholders'
#     id = Column(db.Integer, primary_key=True)
#     full_name = Column(db.Text)
#     display_name = Column(db.Text)
#     type = Column(db.Integer, db.ForeignKey('placeholder_types.id'))
#     template_id = ReferenceCol('template_base')
#     section_id = ReferenceCol('template_section')
# 
#     def __init__(self, name=None, template_id=None, section_id=None):
#         self.name = name
#         self.template_id = template_id
#         self.section_id = section_id
# 
# Path: template_maker/data/placeholders.py
# def get_template_placeholders(template_id):
#     '''
#     Gets the placeholders associated with each template
# 
#     Returns a list of placeholders associated with the template
#     along with the sections that they are tied to
#     '''
#     return TemplatePlaceholders.query.filter(
#         TemplatePlaceholders.template_id==template_id
#     ).order_by(TemplatePlaceholders.id).all()


import datetime
from template_maker.database import db
from template_maker.generator.models import DocumentBase, DocumentPlaceholder
from template_maker.builder.models import TemplateBase, TemplatePlaceholders
from template_maker.data.placeholders import get_template_placeholders
def get_all_documents():
    '''
    Returns all documents currently being edited
    '''
    return DocumentBase.query.all()

def get_documents_and_parent_templates():
    return db.session.query(
        DocumentBase.id, DocumentBase.name, TemplateBase.title
    ).filter(DocumentBase.template_id==TemplateBase.id).all()

def get_document_placeholders(document_id):
    '''
    Gets all the placeholders associated with a document
    '''
    return db.session.query(
        DocumentPlaceholder.id, TemplatePlaceholders.full_name, TemplatePlaceholders.type,
        TemplatePlaceholders.display_name, DocumentPlaceholder.value
    ).filter(DocumentPlaceholder.document_id==document_id).filter(
        DocumentPlaceholder.placeholder_id==TemplatePlaceholders.id
    ).all()

def get_single_document(document_id):
    '''
    Returns a single document from a template_id
    '''
    return DocumentBase.query.get(document_id)

def get_single_document_and_parent_template(document_id):
    return db.session.query(

# Path: template_maker/database.py
# class CRUDMixin(object):
# class Model(CRUDMixin, db.Model):
# class SurrogatePK(object):
#     def create(cls, **kwargs):
#     def update(self, commit=True, **kwargs):
#     def save(self, commit=True):
#     def delete(self, commit=True):
#     def get_by_id(cls, id):
# def ReferenceCol(tablename, nullable=False, ondelete=None, pk_name='id', **kwargs):
# 
# Path: template_maker/generator/models.py
# class DocumentBase(Model):
#     '''
#     Metadata about a new document, relates back to
#     a template
#     '''
# 
#     __tablename__ = 'document_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     name = Column(db.String(255))
#     # published = Column(db.Boolean, default=False)
#     template_id = ReferenceCol('template_base', ondelete='CASCADE')
# 
#     def __init__(self, created_at, updated_at, name, template_id):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.name = name
#         self.template_id = template_id
# 
# class DocumentPlaceholder(Model):
#     '''
#     A document's placeholder's value. Relates to the
#     document and the placeholder
#     '''
# 
#     __tablename__ = 'document_placeholder'
#     id = Column(db.Integer, primary_key=True)
#     document_id = ReferenceCol('document_base')
#     placeholder_id = ReferenceCol('template_placeholders', ondelete='CASCADE')
#     value = Column(db.Text)
# 
#     def __init__(self, document_id, placeholder_id):
#         self.document_id = document_id
#         self.placeholder_id = placeholder_id
# 
# Path: template_maker/builder/models.py
# class TemplateBase(Model):
#     '''
#     Basic metadata about a template
# 
#     Fields:
#     - created_at: when the template was created
#     - updated_at: when the template metadata was last updated
#     - title: the displayed title or name of the template
#     - description: a freetext description of the template
#     '''
# 
#     __tablename__ = 'template_base'
#     id = Column(db.Integer, primary_key=True)
#     created_at = Column(db.DateTime)
#     updated_at = Column(db.DateTime)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_text = db.relationship('TemplateSection', cascade='all,delete', lazy='dynamic')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     published = Column(db.Boolean, default=False)
#     section_order = Column(ARRAY(db.Integer))
#     # created_by = ReferenceCol('users')
# 
#     def __init__(self, created_at, updated_at, title, description):
#         self.created_at = created_at
#         self.updated_at = updated_at
#         self.title = title
#         self.description = description
# 
# class TemplatePlaceholders(Model):
#     '''
#     The placeholders associated with each section
# 
#     Fields:
#     - name: The actual name of the placeholder
#     - template_id: Foreign Key back to the TemplateBase model
#     - section_id: Foreign Key back to the TemplateSection model
#     '''
# 
#     __tablename__ = 'template_placeholders'
#     id = Column(db.Integer, primary_key=True)
#     full_name = Column(db.Text)
#     display_name = Column(db.Text)
#     type = Column(db.Integer, db.ForeignKey('placeholder_types.id'))
#     template_id = ReferenceCol('template_base')
#     section_id = ReferenceCol('template_section')
# 
#     def __init__(self, name=None, template_id=None, section_id=None):
#         self.name = name
#         self.template_id = template_id
#         self.section_id = section_id
# 
# Path: template_maker/data/placeholders.py
# def get_template_placeholders(template_id):
#     '''
#     Gets the placeholders associated with each template
# 
#     Returns a list of placeholders associated with the template
#     along with the sections that they are tied to
#     '''
#     return TemplatePlaceholders.query.filter(
#         TemplatePlaceholders.template_id==template_id
#     ).order_by(TemplatePlaceholders.id).all()


# Path: template_maker/data/documents.py
import datetime
from template_maker.database import db
from template_maker.generator.models import DocumentBase, DocumentPlaceholder
from template_maker.builder.models import TemplateBase, TemplatePlaceholders
from template_maker.data.placeholders import get_template_placeholders

def get_all_documents():
    '''
    Returns all documents currently being edited
    '''
    return DocumentBase.query.all()

def get_documents_and_parent_templates():
    return db.session.query(
        DocumentBase.id, DocumentBase.name, TemplateBase.title
    ).filter(DocumentBase.template_id==TemplateBase.id).all()

def get_document_placeholders(document_id):
    '''
    Gets all the placeholders associated with a document
    '''
    return db.session.query(
        DocumentPlaceholder.id, TemplatePlaceholders.full_name, TemplatePlaceholders.type,
        TemplatePlaceholders.display_name, DocumentPlaceholder.value
    ).filter(DocumentPlaceholder.document_id==document_id).filter(
        DocumentPlaceholder.placeholder_id==TemplatePlaceholders.id
    ).all()

def get_single_document(document_id):
    '''
    Returns a single document from a template_id
    '''
    return DocumentBase.query.get(document_id)

def get_single_document_and_parent_template(document_id):
    return db.session.query(

        DocumentBase.id, DocumentBase.name, TemplateBase.title