Example #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
    def test_update_section(self):
        # add the unicode placeholder type
        db.session.execute('''INSERT INTO placeholder_types VALUES (1, 'unicode')''')

        # generate a template, section, and document
        template = util.insert_new_template()
        section = util.insert_new_section()
        document_id = dm.create_new_document(template.id, {'name': 'foobar'})

        # add content to the section
        new_content = {
            'widget': 'this is a <span class="js-fr-placeholder">[[Text||foo]]</span>' +
            '<span class="js-fr-placeholder">[[Text||bar]]</span>' +
            '<span class="js-fr-placeholder">[[Text||baz]]</span>'
        }

        # update the section and document with our new content
        sc.update_section(section, [], template.id, new_content)
        dm.update_documents(template.id)

        # test that the placeholders made it in ok
        placeholders = TemplatePlaceholders.query.all()
        self.assertEquals(len(placeholders), 3)
        self.assertEquals(section.text, new_content.get('widget'))

        # add values to each of the placeholders
        dm.save_document_section(placeholders, {'[[foo]]': 'foo', '[[bar]]': 'foo', '[[baz]]': 'foo'})

        # test that the values were set properly
        document_placeholders = dm.get_document_placeholders(document_id)
        self.assertEquals(len(document_placeholders), 3)
        for placeholder in document_placeholders:
            self.assertEquals(placeholder.value, 'foo')

        # new content, deleting old placeholders
        new_content2 = {
            'widget': 'this is a section with fewer placeholders <span class="js-fr-placeholder">[[Text||foo]]</span>'
        }

        # update the section, documents
        sc.update_section(section, placeholders, template.id, new_content2)
        dm.update_documents(template.id)

        # test that everything is correct with the section
        self.assertEquals(len(TemplatePlaceholders.query.all()), 1)
        self.assertEquals(section.text, new_content2.get('widget'))

        # test that the documents were updated properly
        placeholders = TemplatePlaceholders.query.all()
        document_placeholders = dm.get_document_placeholders(document_id)
        self.assertEquals(len(document_placeholders), 1)
        self.assertEquals(document_placeholders[0].value, 'foo')

        # update the section, additional content
        # add content to the section
        new_content = {
            'widget': 'this is a <span class="js-fr-placeholder">[[Text||foo]]</span>' +
            '<span class="js-fr-placeholder">[[Text||bar]]</span>'
        }

        # update the section and document with our new content
        sc.update_section(section, placeholders, template.id, new_content)
        dm.update_documents(template.id)

        # test that the old value is still set
        document_placeholders = dm.get_document_placeholders(document_id)
        self.assertEquals(len(document_placeholders), 2)
        for placeholder in document_placeholders:
            if placeholder.display_name == '[[foo]]':
                self.assertEquals(placeholder.value, 'foo')
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