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')
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 )
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)