Esempio n. 1
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
Esempio n. 2
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
    )
Esempio n. 3
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)
Esempio n. 4
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