forked from plone/bobtemplates.plone
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple bobtemplate for creating a View
The template would generate a simple View inheriting BrowserView and having a title
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from bobtemplates.plone.base import prepare_renderer_for_subtemplate | ||
from bobtemplates.plone.base import get_klass_name | ||
from bobtemplates.plone.base import get_normalized_name | ||
from bobtemplates.plone.base import create_file_if_not_exists | ||
from bobtemplates.plone.base import update_file | ||
from bobtemplates.plone.base import get_file_path | ||
from bobtemplates.plone.base import get_example_file_path | ||
from bobtemplates.plone.base import write_xml_tree_to_file | ||
from lxml import etree | ||
|
||
import os | ||
|
||
def _update_views_py(configurator): | ||
views_file_name = u'views.py' | ||
views_dir = u'browser' | ||
|
||
views_file_path = get_file_path(configurator, views_dir, views_file_name) | ||
views_example_file_path = get_example_file_path(configurator, views_dir, views_file_name) | ||
create_file_if_not_exists(views_file_path, views_example_file_path) | ||
|
||
match_str = '-*- Extra views go here -*-' | ||
insert_str = """ | ||
class {0}View(BrowserView): | ||
\"\"\" {1} \"\"\" | ||
def the_title(): | ||
return u'{2}' | ||
""" | ||
insert_str = insert_str.format( | ||
configurator.variables['view_name_klass'], | ||
configurator.variables['description'], | ||
configurator.variables['title'] | ||
) | ||
|
||
update_file(configurator, views_file_path, insert_str, match_str) | ||
return | ||
|
||
|
||
def _update_configure_zcml(configurator): | ||
file_name = u'configure.zcml' | ||
dir_name = u'browser' | ||
|
||
file_path = get_file_path(configurator, dir_name, file_name) | ||
example_file_path = get_example_file_path(configurator, dir_name, file_name) | ||
|
||
with open(file_path, 'r') as xml_file: | ||
tree = get_xml_tree(xml_file) | ||
configure_tag = tree.getroot() | ||
|
||
attributes = { | ||
'name' : configurator.variables['view_name'] , | ||
'for' : '*', | ||
'class' : \ | ||
'.views.' + configurator.variables['view_name_klass'] + 'View', | ||
'template' : 'templates/' + configurator.variables['view_name'], | ||
'permission' : 'zope2.View' | ||
} | ||
|
||
_ = etree.SubElement(configure_tag, | ||
'{http://namespaces.zope.org/browser}page', | ||
attrib=attributes) | ||
|
||
write_xml_tree_to_file(tree, file_path) | ||
return | ||
|
||
|
||
def prepare_renderer(configurator): | ||
configurator = prepare_renderer_for_subtemplate(configurator, | ||
subtemplate='view') | ||
view_name = configurator.variables['view_name'] | ||
configurator.variables['view_name_klass'] = get_klass_name(view_name) | ||
configurator.variables['view_name_normalized'] = \ | ||
get_normalized_name(view_name) | ||
return configurator | ||
|
||
def post_renderer(configurator): | ||
_update_views_py(configurator) | ||
_update_configure_zcml(configurator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[questions] | ||
subtemplate_warning.question = Still want to run this subtemplate? | ||
subtemplate_warning.required = True | ||
subtemplate_warning.default = No | ||
subtemplate_warning.pre_ask_question = bobtemplates.plone.base:subtemplate_warning | ||
subtemplate_warning.post_ask_question = mrbob.hooks:validate_choices | ||
bobtemplates.plone.base:subtemplate_warning_post_question | ||
subtemplate_warning.choices = Yes|No | ||
subtemplate_warning.choices_delimiter = | | ||
|
||
view_name.question = View Name | ||
view_name.required = True | ||
view_name.default = training | ||
view_name.help = Should be something like 'Training' | ||
|
||
title.question = Title | ||
title.default = Welcome | ||
title.required = True | ||
|
||
description.question = Description | ||
description.default = A generic webpage | ||
description.required = True | ||
|
||
[template] | ||
pre_render = bobtemplates.plone.view:prepare_renderer | ||
post_render = bobtemplates.plone.view:post_renderer | ||
post_ask = bobtemplates.plone.base:set_global_vars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<configure xmlns="http://namespaces.zope.org/zope" | ||
xmlns:browser="http://namespaces.zope.org/browser" | ||
i18n_domain="{{{ package.dottedname }}}"> | ||
|
||
</configure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" | ||
lang="en" | ||
metal:use-macro="context/main_template/macros/master" | ||
i18n:domain="{{{ package.dottedname }}}"> | ||
<body> | ||
<metal:content-core fill-slot="content-core"> | ||
<h2 tal:content="python: view.the_title()" /> | ||
</metal:content-core> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
from Products.Five.browser import BrowserView | ||
# -*- Extra imports go here -*- | ||
|
||
|
||
# -*- Extra views go here -*- |