예제 #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))
예제 #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))
예제 #3
0
    def test_reorder_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()

        new_order = [3,1,2]

        sc.reorder_sections(template, new_order)
        self.assertEquals(template.section_order, new_order)
예제 #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'))
예제 #5
0
    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)
예제 #6
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
예제 #7
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'))
예제 #8
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