# 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/extensions.py

# 
# Path: template_maker/generator/forms.py
# class DatePickerField(DateField):
#     widget = DatePickerWidget()
#     validators=(Optional(),)
# 
# class DocumentBaseForm(Form):
#     name = StringField('Title', validators=[DataRequired()])
# 
# 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/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):


import re
import string
from flask import (
    Blueprint, request, render_template,
    redirect, url_for, abort, flash, current_app
)
from flask.ext.wtf import Form
from flask.ext.login import current_user
from wtforms import TextField, IntegerField, FloatField, validators
from template_maker.users.models import User
from template_maker.extensions import login_manager
from template_maker.generator.forms import DatePickerField, DocumentBaseForm
from template_maker.data import (
    templates as tp, sections as sc,
    documents as dm
)
    ' class="' + generate_class(placeholder) + '" rv-value="template.placeholder_{idcombo}"'.format(
        idcombo='_'.join(strip_tags(placeholder.display_name).split())
    ) + \
    'value="{placeholder_value}">'.format(placeholder_value=placeholder.value)
    new_text = re.sub(re.escape(placeholder.full_name), repl_text, section_text)
    return new_text

@blueprint.route('/<int:document_id>/edit', methods=['GET', 'POST'])
@blueprint.route('/<int:document_id>/edit/<int:section_id>', methods=['GET', 'POST'])
def edit_document_sections(document_id, section_id=None):
    '''
    View to handle building a new RFP document

    GET - Returns a new document generator based on the template
    POST - TODO
    '''
    document_base = dm.get_single_document(document_id)
    template_base = tp.get_single_template(document_base.template_id)

    if template_base is None or document_base is None:
        return render_template('404.html')

    if section_id is None:
        return redirect(url_for(
            'generator.edit_document_sections', document_id=document_id,
            section_id=template_base.section_order[0]
        ))

    sections = sc.get_template_sections(template_base)
    current_section = sc.get_single_section(section_id, template_base.id)

# 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/extensions.py

# 
# Path: template_maker/generator/forms.py
# class DatePickerField(DateField):
#     widget = DatePickerWidget()
#     validators=(Optional(),)
# 
# class DocumentBaseForm(Form):
#     name = StringField('Title', validators=[DataRequired()])
# 
# 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/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/generator/views.py
import re
import string
from flask import (
    Blueprint, request, render_template,
    redirect, url_for, abort, flash, current_app
)
from flask.ext.wtf import Form
from flask.ext.login import current_user
from wtforms import TextField, IntegerField, FloatField, validators
from template_maker.users.models import User
from template_maker.extensions import login_manager
from template_maker.generator.forms import DatePickerField, DocumentBaseForm
from template_maker.data import (
    templates as tp, sections as sc,
    documents as dm
)

    ' class="' + generate_class(placeholder) + '" rv-value="template.placeholder_{idcombo}"'.format(
        idcombo='_'.join(strip_tags(placeholder.display_name).split())
    ) + \
    'value="{placeholder_value}">'.format(placeholder_value=placeholder.value)
    new_text = re.sub(re.escape(placeholder.full_name), repl_text, section_text)
    return new_text

@blueprint.route('/<int:document_id>/edit', methods=['GET', 'POST'])
@blueprint.route('/<int:document_id>/edit/<int:section_id>', methods=['GET', 'POST'])
def edit_document_sections(document_id, section_id=None):
    '''
    View to handle building a new RFP document

    GET - Returns a new document generator based on the template
    POST - TODO
    '''
    document_base = dm.get_single_document(document_id)
    template_base = tp.get_single_template(document_base.template_id)

    if template_base is None or document_base is None:
        return render_template('404.html')

    if section_id is None:
        return redirect(url_for(
            'generator.edit_document_sections', document_id=document_id,
            section_id=template_base.section_order[0]
        ))

    sections = sc.get_template_sections(template_base)
    current_section = sc.get_single_section(section_id, template_base.id)

    placeholders = dm.get_document_placeholders(document_base.id)