# HG changeset patch # User Ludovic Chabant # Date 1590124288 25200 # Node ID 386744f74c4bd2863753872b8b5a86f0d86f5b6f # Parent 62900c42d6dd104f8d134348b390708f97187e74 admin: Allow selecting a scaffolding template. Templates available to the prepare command are now also available in the admin panel. diff -r 62900c42d6dd -r 386744f74c4b piecrust/admin/templates/create_page.html --- a/piecrust/admin/templates/create_page.html Thu May 21 22:10:04 2020 -0700 +++ b/piecrust/admin/templates/create_page.html Thu May 21 22:11:28 2020 -0700 @@ -18,6 +18,15 @@ {% endfor %} + {% if content_templates %} + + + {% endif %} +
diff -r 62900c42d6dd -r 386744f74c4b piecrust/admin/views/create.py --- a/piecrust/admin/views/create.py Thu May 21 22:10:04 2020 -0700 +++ b/piecrust/admin/views/create.py Thu May 21 22:11:28 2020 -0700 @@ -42,6 +42,15 @@ 'type': f.field_type, 'value': f.default_value}) + tpl_names = [] + pcapp = g.site.piecrust_app + for ext in pcapp.getCommandExtensions('prepare'): + try: + tpl_names += list(ext.getTemplateNames(pcapp)) + except AttributeError: + pass # For extensions that don't define `getTemplateNames`. + data['content_templates'] = tpl_names + with_menu_context(data) return render_template('create_page.html', **data) @@ -54,19 +63,12 @@ if fk.startswith('meta-'): metadata[fk[5:]] = fv - logger.debug("Creating item with metadata: %s" % metadata) - content_item = source.createContent(metadata) - if content_item is None: - logger.error("Can't create item for: %s" % metadata) - abort(500) + tpl_name = request.form['content-template'] - logger.debug("Creating content: %s" % content_item.spec) - with source.openItem(content_item, 'w') as fp: - fp.write('---\n') - fp.write('draft: true\n') - fp.write('---\n') - fp.write('\n') - fp.write("Start writing!\n") + logger.debug("Creating content with template '%s' and metadata: %s" % + (tpl_name, str(metadata))) + from piecrust.commands.builtin.scaffolding import build_content + content_item = build_content(source, metadata, tpl_name) flash("'%s' was created." % content_item.spec) page = Page(source, content_item)