changeset 1184:386744f74c4b

admin: Allow selecting a scaffolding template. Templates available to the prepare command are now also available in the admin panel.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 21 May 2020 22:11:28 -0700
parents 62900c42d6dd
children 24413a2963b9
files piecrust/admin/templates/create_page.html piecrust/admin/views/create.py
diffstat 2 files changed, 23 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- 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 %}
+    <label for="content-template">Content Template:</label>
+    <select name="content-template" id="content-template">
+        {% for ctpl in content_templates %}
+        <option value="{{ctpl}}">{{ctpl}}</option>
+        {% endfor %}
+    </select>
+    {% endif %}
+
     <input type="hidden" name="source_name" value="{{source_name}}" />
 
     <div class="row">
--- 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)