# 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/builder/models.py
# class TemplateSection(Model):
#     '''
#     TemplateSection is a superclass of section information and maps to section types
# 
#     Fields:
#     - title: The name of the section
#     - description: The description of the section
#     - position: Where in the template the section lies
#     - template_id: Foreign Key relationship back to the TemplateBase
#     - section_type: The type of section to be used. This uses "joined
#     table inheritance": http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html
#     '''
# 
#     __tablename__ = 'template_section'
#     id = Column(db.Integer, primary_key=True)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_id = ReferenceCol('template_base')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     section_type = Column('section_type', db.String(50))
#     __mapper_args__ = {'polymorphic_on': section_type}
# 
#     def __init__(self, template_id=None, title=None, description=None):
#         self.template_id = template_id
#         self.title = title
#         self.description = description
# 
# class TextSection(TemplateSection):
#     '''
#     TextSection is a class of TemplateSection; it supports large text input with
#     placeholder interpolation
# 
#     Fields:
#     - id: 1:1 Relationship from TemplateSection
#     - text: Text of the section
#     '''
#     __tablename__ = 'text_section'
#     section_id = Column(db.Integer, db.ForeignKey('template_section.id'), primary_key=True)
#     __mapper_args__ = {
#         'polymorphic_identity': 'text',
#         'inherit_condition': (section_id == TemplateSection.id)
#     }
#     text = Column(db.Text)
#     choice_type = ('text', 'Text Section')
# 
#     def __init__(self, title, description, template_id):
#         super(TextSection, self).__init__(template_id=template_id, title=title, description=description)
# 
# class FixedTextSection(Model):
#     '''
#     FixedTextSection is a class of TemplateSection. It does not allow document generators
#     to interpolate placeholders.
# 
#     - id: 1:1 Relationship from TemplateSection
#     - text: Text of the section
#     '''
#     __tablename__ = 'fixed_text_section'
#     section_id = Column(db.Integer, db.ForeignKey('template_section.id'), primary_key=True)
#     __mapper_args__ = {
#         'polymorphic_identity': 'fixed_text',
#         'inherit_condition': (section_id == TemplateSection.id)
#     }
#     text = Column(db.Text)
#     choice_type = ('fixed_text', 'Fixed Text Section')
# 
#     def __init__(self, title, description, template_id):
#         super(FixedTextSection, self).__init__(template_id=template_id, title=title, description=description)
# 
# Path: template_maker/data/placeholders.py
# VARIABLE_TYPE_MAPS = {
#     'text': 1, 'date': 2,'number': 3, 'float': 4
# }
# def get_all_placeholders(html):
# def get_template_placeholders(template_id):
# def get_section_placeholders(section_id):
# def parse_placeholder_text(placeholder):
# def dedupe_placeholders(input_placeholders):
# def update_placeholders(input_placeholders, current_placeholders, template_id, section_id):
# def delete_placeholders(to_delete_placeholders, template_id, section_id):
# def create_placeholders(new_placeholders, template_id, section_id):


from sqlalchemy.dialects.postgresql import array
from template_maker.database import db
from template_maker.builder.models import TemplateSection, TextSection, FixedTextSection
from template_maker.data import placeholders as ph
    '''
    if to_delete:
        if to_delete in template.section_order:
            template.section_order.remove(to_delete)

            # cast it to a sqlalchemy array type to ensure
            # the commit works properly
            new_order = array(template.section_order)
            template.section_order = new_order if len(new_order) > 0 else None
            db.session.commit()

    else:
        template.section_order = [int(i) for i in section_order]
        db.session.commit()

    return template.section_order

def update_section(section, placeholders, template_id, form_input):
    '''
    Updates TemplateSection and TemplatePlaceholders models associated with
    a particular template_id
    '''
    section.title = form_input.get('title')

    if section.section_type == 'text':
        html = form_input.get('widget')
        section.text = html
        # save the text
        db.session.commit()
        # find all placeholders, using beautiful soup

# 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/builder/models.py
# class TemplateSection(Model):
#     '''
#     TemplateSection is a superclass of section information and maps to section types
# 
#     Fields:
#     - title: The name of the section
#     - description: The description of the section
#     - position: Where in the template the section lies
#     - template_id: Foreign Key relationship back to the TemplateBase
#     - section_type: The type of section to be used. This uses "joined
#     table inheritance": http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html
#     '''
# 
#     __tablename__ = 'template_section'
#     id = Column(db.Integer, primary_key=True)
#     title = Column(db.String(255))
#     description = Column(db.Text)
#     template_id = ReferenceCol('template_base')
#     template_placeholders = db.relationship('TemplatePlaceholders', cascade='all,delete', lazy='dynamic')
#     section_type = Column('section_type', db.String(50))
#     __mapper_args__ = {'polymorphic_on': section_type}
# 
#     def __init__(self, template_id=None, title=None, description=None):
#         self.template_id = template_id
#         self.title = title
#         self.description = description
# 
# class TextSection(TemplateSection):
#     '''
#     TextSection is a class of TemplateSection; it supports large text input with
#     placeholder interpolation
# 
#     Fields:
#     - id: 1:1 Relationship from TemplateSection
#     - text: Text of the section
#     '''
#     __tablename__ = 'text_section'
#     section_id = Column(db.Integer, db.ForeignKey('template_section.id'), primary_key=True)
#     __mapper_args__ = {
#         'polymorphic_identity': 'text',
#         'inherit_condition': (section_id == TemplateSection.id)
#     }
#     text = Column(db.Text)
#     choice_type = ('text', 'Text Section')
# 
#     def __init__(self, title, description, template_id):
#         super(TextSection, self).__init__(template_id=template_id, title=title, description=description)
# 
# class FixedTextSection(Model):
#     '''
#     FixedTextSection is a class of TemplateSection. It does not allow document generators
#     to interpolate placeholders.
# 
#     - id: 1:1 Relationship from TemplateSection
#     - text: Text of the section
#     '''
#     __tablename__ = 'fixed_text_section'
#     section_id = Column(db.Integer, db.ForeignKey('template_section.id'), primary_key=True)
#     __mapper_args__ = {
#         'polymorphic_identity': 'fixed_text',
#         'inherit_condition': (section_id == TemplateSection.id)
#     }
#     text = Column(db.Text)
#     choice_type = ('fixed_text', 'Fixed Text Section')
# 
#     def __init__(self, title, description, template_id):
#         super(FixedTextSection, self).__init__(template_id=template_id, title=title, description=description)
# 
# Path: template_maker/data/placeholders.py
# VARIABLE_TYPE_MAPS = {
#     'text': 1, 'date': 2,'number': 3, 'float': 4
# }
# def get_all_placeholders(html):
# def get_template_placeholders(template_id):
# def get_section_placeholders(section_id):
# def parse_placeholder_text(placeholder):
# def dedupe_placeholders(input_placeholders):
# def update_placeholders(input_placeholders, current_placeholders, template_id, section_id):
# def delete_placeholders(to_delete_placeholders, template_id, section_id):
# def create_placeholders(new_placeholders, template_id, section_id):


# Path: template_maker/data/sections.py
from sqlalchemy.dialects.postgresql import array
from template_maker.database import db
from template_maker.builder.models import TemplateSection, TextSection, FixedTextSection
from template_maker.data import placeholders as ph

    '''
    if to_delete:
        if to_delete in template.section_order:
            template.section_order.remove(to_delete)

            # cast it to a sqlalchemy array type to ensure
            # the commit works properly
            new_order = array(template.section_order)
            template.section_order = new_order if len(new_order) > 0 else None
            db.session.commit()

    else:
        template.section_order = [int(i) for i in section_order]
        db.session.commit()

    return template.section_order

def update_section(section, placeholders, template_id, form_input):
    '''
    Updates TemplateSection and TemplatePlaceholders models associated with
    a particular template_id
    '''
    section.title = form_input.get('title')

    if section.section_type == 'text':
        html = form_input.get('widget')
        section.text = html
        # save the text
        db.session.commit()
        # find all placeholders, using beautiful soup

        input_placeholders = ph.dedupe_placeholders(ph.get_all_placeholders(html))