# Path: template_maker/extensions.py

# 
# Path: template_maker/users/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     email = Column(db.String(80), unique=True, nullable=False)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, email, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({email!r})>'.format(email=self.email)
# 
# Path: template_maker/builder/forms.py
# class TemplateBaseForm(Form):
#     '''
#     Simple form for creating a new template
#     '''
#     title = StringField('Title', validators=[DataRequired()])
#     description = TextAreaField('Description', validators=[DataRequired()])
# 
# class TemplateSectionForm(Form):
#     '''
#     Simple form for creating adding a new template section
#     '''
#     title = StringField('Title')
#     type = SelectField('Type', choices=IMPLEMENTED_SECTIONS)
# 
# class TemplateSectionTextForm(Form):
#     '''
#     WYSIWYG Editor for the TextSection and FixedTextSection Models
#     '''
#     title = StringField('Title')
#     widget = FroalaField()
# 
# Path: template_maker/data/templates.py
# def get_all_templates():
# def get_published_templates():
# def get_single_template(template_id):
# def create_new_template(data):
# def delete_template(template):
# def publish_template(template_id):
# 
# Path: template_maker/data/sections.py
# SECTION_TYPE_MAPS = {
#     'text': TextSection, 'fixed_text': FixedTextSection,
# }
# def get_template_sections(template):
# def get_single_section(section_id, template_id):
# def create_new_section(section, template_id):
# def reorder_sections(template, section_order, to_delete=None):
# def update_section(section, placeholders, template_id, form_input):
# def delete_section(section_id, template_id):
# 
# 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/documents.py
# def get_all_documents():
# def get_documents_and_parent_templates():
# def get_document_placeholders(document_id):
# def get_single_document(document_id):
# def get_single_document_and_parent_template(document_id):
# def set_document_placeholders(template_id, document_base):
# def update_documents(template_id):
# def create_new_document(template_id, data):
# def save_document_section(placeholders, data):
# def delete_document(document):


from flask import (
    Blueprint, request, make_response,
    render_template, redirect, abort, url_for,
    flash, current_app
)
from flask.ext.login import current_user
from template_maker.extensions import login_manager
from template_maker.users.models import User
from template_maker.builder.forms import (
    TemplateBaseForm, TemplateSectionForm,
    TemplateSectionTextForm
)
from template_maker.data import (
    templates as tp, sections as sc,
    placeholders as ph, documents as dm
)
from template_maker.builder.boilerplate import boilerplate as html_boilerplate
        return abort(403)

@blueprint.route('/<int:template_id>/')
def redirect_to_section(template_id):
    return redirect(url_for('builder.edit_template', template_id=template_id))

@blueprint.route('/<int:template_id>/section/', methods=['GET', 'POST'])
@blueprint.route('/<int:template_id>/section/<int:section_id>', methods=['GET', 'POST'])
def edit_template(template_id, section_id=None, section_type=None):
    '''
    Route for interacting with individual sections

    GET - Gets the template and renders out the editing for that particular section
    POST - Updates a section
    '''
    template_base = tp.get_single_template(template_id)
    current_section = sc.get_single_section(section_id, template_id)
    if template_base is None or (current_section and current_section.template_id != template_id):
        return render_template('404.html')

    # handle re-ordering
    old_order = template_base.section_order
    if request.method == 'POST':
        request_sections = request.form.getlist('id')
        new_order = sc.reorder_sections(template_base, request_sections) if len(request_sections) > 0 else None
    else:
        new_order = None

    # initialize the forms
    form = SECTION_FORM_MAP[current_section.section_type]()

# Path: template_maker/extensions.py

# 
# Path: template_maker/users/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     email = Column(db.String(80), unique=True, nullable=False)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, email, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({email!r})>'.format(email=self.email)
# 
# Path: template_maker/builder/forms.py
# class TemplateBaseForm(Form):
#     '''
#     Simple form for creating a new template
#     '''
#     title = StringField('Title', validators=[DataRequired()])
#     description = TextAreaField('Description', validators=[DataRequired()])
# 
# class TemplateSectionForm(Form):
#     '''
#     Simple form for creating adding a new template section
#     '''
#     title = StringField('Title')
#     type = SelectField('Type', choices=IMPLEMENTED_SECTIONS)
# 
# class TemplateSectionTextForm(Form):
#     '''
#     WYSIWYG Editor for the TextSection and FixedTextSection Models
#     '''
#     title = StringField('Title')
#     widget = FroalaField()
# 
# Path: template_maker/data/templates.py
# def get_all_templates():
# def get_published_templates():
# def get_single_template(template_id):
# def create_new_template(data):
# def delete_template(template):
# def publish_template(template_id):
# 
# Path: template_maker/data/sections.py
# SECTION_TYPE_MAPS = {
#     'text': TextSection, 'fixed_text': FixedTextSection,
# }
# def get_template_sections(template):
# def get_single_section(section_id, template_id):
# def create_new_section(section, template_id):
# def reorder_sections(template, section_order, to_delete=None):
# def update_section(section, placeholders, template_id, form_input):
# def delete_section(section_id, template_id):
# 
# 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/documents.py
# def get_all_documents():
# def get_documents_and_parent_templates():
# def get_document_placeholders(document_id):
# def get_single_document(document_id):
# def get_single_document_and_parent_template(document_id):
# def set_document_placeholders(template_id, document_base):
# def update_documents(template_id):
# def create_new_document(template_id, data):
# def save_document_section(placeholders, data):
# def delete_document(document):


# Path: template_maker/builder/views.py
from flask import (
    Blueprint, request, make_response,
    render_template, redirect, abort, url_for,
    flash, current_app
)
from flask.ext.login import current_user
from template_maker.extensions import login_manager
from template_maker.users.models import User
from template_maker.builder.forms import (
    TemplateBaseForm, TemplateSectionForm,
    TemplateSectionTextForm
)
from template_maker.data import (
    templates as tp, sections as sc,
    placeholders as ph, documents as dm
)
from template_maker.builder.boilerplate import boilerplate as html_boilerplate

        return abort(403)

@blueprint.route('/<int:template_id>/')
def redirect_to_section(template_id):
    return redirect(url_for('builder.edit_template', template_id=template_id))

@blueprint.route('/<int:template_id>/section/', methods=['GET', 'POST'])
@blueprint.route('/<int:template_id>/section/<int:section_id>', methods=['GET', 'POST'])
def edit_template(template_id, section_id=None, section_type=None):
    '''
    Route for interacting with individual sections

    GET - Gets the template and renders out the editing for that particular section
    POST - Updates a section
    '''
    template_base = tp.get_single_template(template_id)
    current_section = sc.get_single_section(section_id, template_id)
    if template_base is None or (current_section and current_section.template_id != template_id):
        return render_template('404.html')

    # handle re-ordering
    old_order = template_base.section_order
    if request.method == 'POST':
        request_sections = request.form.getlist('id')
        new_order = sc.reorder_sections(template_base, request_sections) if len(request_sections) > 0 else None
    else:
        new_order = None

    # initialize the forms
    form = SECTION_FORM_MAP[current_section.section_type]()

    new_section_form = TemplateSectionForm()