def test_get_template_sections(self):
        template = util.insert_new_template()
        self.assertEquals(template.section_order, None)

        # insert some sections
        util.insert_new_section()
        util.insert_new_section()
        util.insert_new_section()

        # assert that we get the sections
        sections = sc.get_template_sections(template)
        self.assertEquals(len(sections), 3)

        # assert that if we have an order, the sections
        # are returned in that order
        new_order = [3,1,2]
        sc.reorder_sections(template, new_order)
        sections = sc.get_template_sections(template)
        self.assertEquals(len(sections), 3)
        self.assertEquals([i.id for i in sections], new_order)
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
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'))
Beispiel #4
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'))
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
    )
Beispiel #6
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)
Beispiel #7
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