comparison piecrust/admin/views/create.py @ 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 0d699f04968c
children
comparison
equal deleted inserted replaced
1183:62900c42d6dd 1184:386744f74c4b
40 'name': f.name, 40 'name': f.name,
41 'display_name': f.name, 41 'display_name': f.name,
42 'type': f.field_type, 42 'type': f.field_type,
43 'value': f.default_value}) 43 'value': f.default_value})
44 44
45 tpl_names = []
46 pcapp = g.site.piecrust_app
47 for ext in pcapp.getCommandExtensions('prepare'):
48 try:
49 tpl_names += list(ext.getTemplateNames(pcapp))
50 except AttributeError:
51 pass # For extensions that don't define `getTemplateNames`.
52 data['content_templates'] = tpl_names
53
45 with_menu_context(data) 54 with_menu_context(data)
46 return render_template('create_page.html', **data) 55 return render_template('create_page.html', **data)
47 56
48 57
49 def _submit_page_form(pcapp, source): 58 def _submit_page_form(pcapp, source):
52 metadata[f.name] = f.default_value 61 metadata[f.name] = f.default_value
53 for fk, fv in request.form.items(): 62 for fk, fv in request.form.items():
54 if fk.startswith('meta-'): 63 if fk.startswith('meta-'):
55 metadata[fk[5:]] = fv 64 metadata[fk[5:]] = fv
56 65
57 logger.debug("Creating item with metadata: %s" % metadata) 66 tpl_name = request.form['content-template']
58 content_item = source.createContent(metadata)
59 if content_item is None:
60 logger.error("Can't create item for: %s" % metadata)
61 abort(500)
62 67
63 logger.debug("Creating content: %s" % content_item.spec) 68 logger.debug("Creating content with template '%s' and metadata: %s" %
64 with source.openItem(content_item, 'w') as fp: 69 (tpl_name, str(metadata)))
65 fp.write('---\n') 70 from piecrust.commands.builtin.scaffolding import build_content
66 fp.write('draft: true\n') 71 content_item = build_content(source, metadata, tpl_name)
67 fp.write('---\n')
68 fp.write('\n')
69 fp.write("Start writing!\n")
70 flash("'%s' was created." % content_item.spec) 72 flash("'%s' was created." % content_item.spec)
71 73
72 page = Page(source, content_item) 74 page = Page(source, content_item)
73 uri = page.getUri() 75 uri = page.getUri()
74 logger.debug("Redirecting to: %s" % uri) 76 logger.debug("Redirecting to: %s" % uri)