Example #1
0
def delete_section(template_id, section_id):
    template = tp.get_single_template(template_id)
    if template.section_order and len(template.section_order) > 0:
        sc.reorder_sections(template, template.section_order, to_delete=section_id)

    sc.delete_section(section_id, template_id)

    flash('Section successfully deleted!', 'alert-success')
    return redirect(url_for('builder.edit_template', template_id=template_id))
Example #2
0
def delete_section(template_id, section_id):
    template = tp.get_single_template(template_id)
    if template.section_order and len(template.section_order) > 0:
        sc.reorder_sections(template,
                            template.section_order,
                            to_delete=section_id)

    sc.delete_section(section_id, template_id)

    flash('Section successfully deleted!', 'alert-success')
    return redirect(url_for('builder.edit_template', template_id=template_id))
Example #3
0
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()
    placeholders = ph.get_template_placeholders(template_base.id)

    # if the form is valid, go ahead and save everything
    if form.validate_on_submit():
        sc.update_section(current_section, placeholders, template_id, request.form)
        total_documents = str(dm.update_documents(template_id))
        flash('Successfully saved! ' + total_documents + ' updated', 'alert-success')
        return redirect(url_for(
            'builder.edit_template', template_id=template_id
        ))
    elif request.method == 'POST':
        if new_order and new_order != old_order:
            flash('Successfully saved!', 'alert-success')
        if section_id == 0:
            return redirect(url_for('builder.edit_template', template_id=template_id))
        else:
            return redirect(url_for('builder.edit_template',
                template_id=template_id, section_id=section_id
            ))

    # otherwise, we are doing a get request, so get the sections and placeholders
    sections = sc.get_template_sections(template_base)

    response = make_response(render_template(
        'builder/edit.html', template=template_base,
        sections=sections, placeholders=placeholders,
        form=form, new_section_form=new_section_form,
        current_section=current_section
    ))
    return response
Example #4
0
def new_document(template_id):
    '''
    View handling the creation of new documents from templates

    GET - Returns the new document form
    POST - Creates a new document from a template
    '''
    template = tp.get_single_template(template_id)
    form = DocumentBaseForm()
    if form.validate_on_submit():
        document_base_id = dm.create_new_document(template_id, request.form)
        return redirect(
            url_for('generator.edit_document_sections', document_id=document_base_id)
        )

    return render_template('generator/new.html', form=form, template=template)
Example #5
0
def edit_template_metadata(template_id):
    '''
    Route for managing individual template objects

    methods can be request-level or come from the request args
    GET - TODO
    PUT - TODO
    DELETE - Deletes the template (and cascades to delete
    template text and associated placeholders) and returns a 204
    or returns a 403
    '''
    template_base = tp.get_single_template(template_id)
    if request.args.get('method') == 'DELETE':
        if tp.delete_template(template_base):
            return redirect(url_for('builder.list_templates'))
        return abort(403)
Example #6
0
def new_document(template_id):
    '''
    View handling the creation of new documents from templates

    GET - Returns the new document form
    POST - Creates a new document from a template
    '''
    template = tp.get_single_template(template_id)
    form = DocumentBaseForm()
    if form.validate_on_submit():
        document_base_id = dm.create_new_document(template_id, request.form)
        return redirect(
            url_for('generator.edit_document_sections',
                    document_id=document_base_id))

    return render_template('generator/new.html', form=form, template=template)
Example #7
0
def edit_template_metadata(template_id):
    '''
    Route for managing individual template objects

    methods can be request-level or come from the request args
    GET - TODO
    PUT - TODO
    DELETE - Deletes the template (and cascades to delete
    template text and associated placeholders) and returns a 204
    or returns a 403
    '''
    template_base = tp.get_single_template(template_id)
    if request.args.get('method') == 'DELETE':
        if tp.delete_template(template_base):
            return redirect(url_for('builder.list_templates'))
        return abort(403)
Example #8
0
def publish_template(template_id):
    '''
    Route for taking documents from the BUILDER and turning them into TEMPLATES via the GENERATOR

    GET - Returns the preview for the template
    POST - Data contains sections and placeholders. Publish freezes the current
    version of the template into new database tables, allowing the builder documents
    to be edited and create new templates later on.
    '''
    template_base = tp.get_single_template(template_id)
    if template_base is None:
        return render_template('404.html')
    if request.method == 'GET':
        sections = sc.get_template_sections(template_base)
        return render_template('builder/preview.html', sections=sections, template=template_base, preview=True)
    elif request.method == 'POST':
        # set the publish flag to be true, set the section order
        template = tp.publish_template(template_id)
        sc.reorder_sections(template, request.form.getlist('id'))
        return redirect(url_for('builder.list_templates'))
Example #9
0
def publish_template(template_id):
    '''
    Route for taking documents from the BUILDER and turning them into TEMPLATES via the GENERATOR

    GET - Returns the preview for the template
    POST - Data contains sections and placeholders. Publish freezes the current
    version of the template into new database tables, allowing the builder documents
    to be edited and create new templates later on.
    '''
    template_base = tp.get_single_template(template_id)
    if template_base is None:
        return render_template('404.html')
    if request.method == 'GET':
        sections = sc.get_template_sections(template_base)
        return render_template('builder/preview.html',
                               sections=sections,
                               template=template_base,
                               preview=True)
    elif request.method == 'POST':
        # set the publish flag to be true, set the section order
        template = tp.publish_template(template_id)
        sc.reorder_sections(template, request.form.getlist('id'))
        return redirect(url_for('builder.list_templates'))
Example #10
0
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)

    class F(Form):
        pass

    if current_section.section_type == 'text':
        # if we have a text section, we need to prep the page for the rivets
        # two-way data binding
        current_section_text = current_section.text
        for placeholder in placeholders:
            # add a data_input value onto the placeholder
            placeholder.rv_data_input = 'placeholder_' + '_'.join(strip_tags(placeholder.display_name).split())
            # format the section text
            current_section_text = create_rivets_bindings(placeholder, current_section_text)
            # set up the form
            setattr(
                F, placeholder.display_name,
                TYPE_VARIABLES_MAP[placeholder.type](placeholder.display_name, validators=[validators.Optional()])
            )

    form = F()

    if form.validate_on_submit():
        dm.save_document_section(placeholders, request.form)
        flash('Changes successfully saved!', 'alert-success')
        return redirect(url_for(
            'generator.edit_document_sections', document_id=document_base.id, section_id=current_section.id)
        )
    for field in form.__iter__():
        # set the rv_data_input value on the form field as well as on the placeholder
        setattr(field, 'rv_data_input', 'template.placeholder_' + '_'.join(strip_tags(field.name).split()))
        setattr(field, 'label', field.name)

    return render_template('generator/build-document.html',
        document=document_base, template=template_base,
        sections=sections, placeholders=placeholders,
        current_section=current_section,
        current_section_text=current_section_text or None,
        form=form
    )
Example #11
0
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)

    class F(Form):
        pass

    if current_section.section_type == 'text':
        # if we have a text section, we need to prep the page for the rivets
        # two-way data binding
        current_section_text = current_section.text
        for placeholder in placeholders:
            # add a data_input value onto the placeholder
            placeholder.rv_data_input = 'placeholder_' + '_'.join(
                strip_tags(placeholder.display_name).split())
            # format the section text
            current_section_text = create_rivets_bindings(
                placeholder, current_section_text)
            # set up the form
            setattr(
                F, placeholder.display_name,
                TYPE_VARIABLES_MAP[placeholder.type](
                    placeholder.display_name,
                    validators=[validators.Optional()]))

    form = F()

    if form.validate_on_submit():
        dm.save_document_section(placeholders, request.form)
        flash('Changes successfully saved!', 'alert-success')
        return redirect(
            url_for('generator.edit_document_sections',
                    document_id=document_base.id,
                    section_id=current_section.id))
    for field in form.__iter__():
        # set the rv_data_input value on the form field as well as on the placeholder
        setattr(
            field, 'rv_data_input',
            'template.placeholder_' + '_'.join(strip_tags(field.name).split()))
        setattr(field, 'label', field.name)

    return render_template('generator/build-document.html',
                           document=document_base,
                           template=template_base,
                           sections=sections,
                           placeholders=placeholders,
                           current_section=current_section,
                           current_section_text=current_section_text or None,
                           form=form)
Example #12
0
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()
    placeholders = ph.get_template_placeholders(template_base.id)

    # if the form is valid, go ahead and save everything
    if form.validate_on_submit():
        sc.update_section(current_section, placeholders, template_id,
                          request.form)
        total_documents = str(dm.update_documents(template_id))
        flash('Successfully saved! ' + total_documents + ' updated',
              'alert-success')
        return redirect(
            url_for('builder.edit_template', template_id=template_id))
    elif request.method == 'POST':
        if new_order and new_order != old_order:
            flash('Successfully saved!', 'alert-success')
        if section_id == 0:
            return redirect(
                url_for('builder.edit_template', template_id=template_id))
        else:
            return redirect(
                url_for('builder.edit_template',
                        template_id=template_id,
                        section_id=section_id))

    # otherwise, we are doing a get request, so get the sections and placeholders
    sections = sc.get_template_sections(template_base)

    response = make_response(
        render_template('builder/edit.html',
                        template=template_base,
                        sections=sections,
                        placeholders=placeholders,
                        form=form,
                        new_section_form=new_section_form,
                        current_section=current_section))
    return response