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_get_template_placeholders(self): template = util.insert_new_template() self.assertEquals(template.section_order, None) # insert some sections and some placeholders section1 = util.insert_new_section() section2 = util.insert_new_section() util.insert_new_placeholder(section1.id) util.insert_new_placeholder(section2.id) placeholders = ph.get_template_placeholders(template.id) self.assertEquals(len(placeholders), 2)
def set_document_placeholders(template_id, document_base): # create the placeholders for the document placeholders = get_template_placeholders(template_id) for placeholder in placeholders: _placeholder = DocumentPlaceholder.query.filter( DocumentPlaceholder.placeholder_id==placeholder.id ).filter( DocumentPlaceholder.document_id==document_base.id ).first() # if we already have this placeholder, pass if _placeholder: continue new_placeholder = DocumentPlaceholder( document_id=document_base.id, placeholder_id=placeholder.id, ) db.session.add(new_placeholder) db.session.commit()
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