# 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 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


from template_maker.database import db
from template_maker.builder.models import TemplatePlaceholders
from bs4 import BeautifulSoup
    current_placeholder_full_names = set([i.full_name for i in current_placeholders])
    new_placeholders = list(set(input_placeholders).difference(current_placeholder_full_names))
    to_delete_placeholders = list(set(current_placeholder_full_names).difference(input_placeholders))

    if len(to_delete_placeholders) > 0:
        delete_placeholders(to_delete_placeholders, template_id, section_id)

    if len(new_placeholders) > 0:
        create_placeholders(new_placeholders, template_id, section_id)

    return True

def delete_placeholders(to_delete_placeholders, template_id, section_id):
    TemplatePlaceholders.query.filter(
        TemplatePlaceholders.full_name.in_(to_delete_placeholders),
        TemplatePlaceholders.template_id==template_id,
        TemplatePlaceholders.section_id==section_id
    ).delete(synchronize_session=False)
    return True

def create_placeholders(new_placeholders, template_id, section_id):
    for placeholder in new_placeholders:
        _placeholder = TemplatePlaceholders()
        var_type, var_name = parse_placeholder_text(placeholder)
        _placeholder.full_name = placeholder
        _placeholder.display_name = var_name
        _placeholder.template_id = template_id
        _placeholder.section_id = section_id
        _placeholder.type = VARIABLE_TYPE_MAPS[var_type]
        db.session.add(_placeholder)

# 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 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
from template_maker.database import db
from template_maker.builder.models import TemplatePlaceholders
from bs4 import BeautifulSoup

    current_placeholder_full_names = set([i.full_name for i in current_placeholders])
    new_placeholders = list(set(input_placeholders).difference(current_placeholder_full_names))
    to_delete_placeholders = list(set(current_placeholder_full_names).difference(input_placeholders))

    if len(to_delete_placeholders) > 0:
        delete_placeholders(to_delete_placeholders, template_id, section_id)

    if len(new_placeholders) > 0:
        create_placeholders(new_placeholders, template_id, section_id)

    return True

def delete_placeholders(to_delete_placeholders, template_id, section_id):
    TemplatePlaceholders.query.filter(
        TemplatePlaceholders.full_name.in_(to_delete_placeholders),
        TemplatePlaceholders.template_id==template_id,
        TemplatePlaceholders.section_id==section_id
    ).delete(synchronize_session=False)
    return True

def create_placeholders(new_placeholders, template_id, section_id):
    for placeholder in new_placeholders:
        _placeholder = TemplatePlaceholders()
        var_type, var_name = parse_placeholder_text(placeholder)
        _placeholder.full_name = placeholder
        _placeholder.display_name = var_name
        _placeholder.template_id = template_id
        _placeholder.section_id = section_id
        _placeholder.type = VARIABLE_TYPE_MAPS[var_type]
        db.session.add(_placeholder)

    db.session.commit()