renamed cybertools.web to cybertools.view and rearranged package layout

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1524 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-11-26 14:52:23 +00:00
parent b01a1a17c6
commit 959dd2a56f
13 changed files with 331 additions and 0 deletions

27
view/README.txt Normal file
View file

@ -0,0 +1,27 @@
===============================
All about Views, Templates, ...
===============================
$Id$
Generic Views
=============
OK, there aren't really generic views. Already the first implementation we
want to look at is a specic one: It is based on Zope Page Templates and
uses the classic CMF/Zope 3 approach: The template belonging to a view
calls a `main` macro and fills a slot there. But at least the template
implementation is decoupled from the view, so we are able to put a lot of
generic functionality into the view.
In order to make a ZPT work we need a Zope-compatible request, so we use
the standard Zope 3 TestRequest.
>>> from zope.publisher.browser import TestRequest
>>> from cybertools.view.web.base import View
>>> view = View(None, TestRequest())
>>> view.render()
u'< html...>...<body...>...</body>...</html>...'

3
view/__init__.py Normal file
View file

@ -0,0 +1,3 @@
"""
$Id$
"""

36
view/base.py Normal file
View file

@ -0,0 +1,36 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Generic view base class.
$Id$
"""
from zope.cachedescriptors.property import Lazy
class View(object):
def __init__(self, context, request):
self.context = context
self.request = request
def render(self, *args, **kw):
return ''

30
view/tests.py Executable file
View file

@ -0,0 +1,30 @@
#! /usr/bin/python
"""
Tests for the 'cybertools.index' package.
$Id$
"""
import unittest, doctest
from zope.testing.doctestunit import DocFileSuite
from cybertools.view.base import View
class Test(unittest.TestCase):
"Basic tests for the index package."
def testBasicStuff(self):
pass
def test_suite():
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
return unittest.TestSuite((
DocFileSuite('README.txt', optionflags=flags),
unittest.makeSuite(Test),
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')

3
view/web/__init__.py Normal file
View file

@ -0,0 +1,3 @@
"""
$Id$
"""

44
view/web/base.py Normal file
View file

@ -0,0 +1,44 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Generic view base class.
$Id$
"""
from zope.cachedescriptors.property import Lazy
from cybertools.view import base
from cybertools.view.web.zpt.template import Template
class View(base.View):
templateFactory = Template
def __init__(self, context, request):
self.context = context
self.request = request
@Lazy
def template(self):
return self.templateFactory(self)
def render(self, *args, **kw):
return self.template.render(*args, **kw)

10
view/web/configure.zcml Normal file
View file

@ -0,0 +1,10 @@
<!-- $Id$ -->
<configure
xmlns:zope="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="zope">
<zope:include package=".zpt" />
</configure>

39
view/web/template.py Normal file
View file

@ -0,0 +1,39 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Generic template base class.
$Id$
"""
from zope.app.traversing.adapters import DefaultTraversable
from zope import component
class Template(object):
def __init__(self, view):
self.view = view
self.context = view.context
self.request = view.request
def render(self, *args, **kw):
return u''
component.provideAdapter(DefaultTraversable, (Template,))

3
view/web/zpt/__init__.py Normal file
View file

@ -0,0 +1,3 @@
"""
$Id$
"""

View file

@ -0,0 +1,16 @@
<!-- $Id$ -->
<configure
xmlns:zope="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="zope">
<browser:page
for="*"
name="dummy.html"
class="cybertools.web.view.View"
attribute="render"
permission="zope.View"
/>
</configure>

15
view/web/zpt/content.pt Normal file
View file

@ -0,0 +1,15 @@
<html metal:use-macro="options/template/main_macro"
i18n:domain="zope">
<head></head>
<body>
<metal:body fill-slot="body">
<div>
This is the body
</div>
</metal:body>
</body>
</html>

54
view/web/zpt/main.pt Normal file
View file

@ -0,0 +1,54 @@
<metal:block define-macro="page"><metal:block define-slot="doctype"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></metal:block>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
i18n:domain="zope">
<head metal:define-macro="head">
<title metal:define-slot="title"
tal:content="options/title|view/headTitle|view/title|context/title|default">
Powered by Zope 3
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<metal:block define-slot="ecmascript_slot" />
<link rel="icon" type="image/png"
tal:attributes="href string:${options/template/resourceBase}favicon.png" />
</head>
<body metal:define-macro="body">
<div id="global">
<div class="top" metal:define-slot="top">
<a href="#" name="top" metal:define-slot="logo"
tal:attributes="href string:request/URL/1"><img class="logo"
src="logo.gif" border="0" alt="Home"
tal:attributes="src string:logo.gif" /></a>
</div>
</div>
<div id="menu">
<metal:menu define-slot="navigators">
</metal:menu>
</div>
<div id="content">
<metal:message metal:define-slot="message" />
<metal:content define-slot="body">Here comes the body</metal:content>
</div>
<div id="sub-section">
<metal:sub define-slot="context_information"></metal:sub>
</div>
<div id="footer" class="footer">
<metal:footer define-slot="footer" />
</div>
</body>
</html>
</metal:block>

51
view/web/zpt/template.py Normal file
View file

@ -0,0 +1,51 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
ZPT-based template base class.
$Id$
"""
from zope.app.pagetemplate import ViewPageTemplateFile
from zope.cachedescriptors.property import Lazy
from cybertools.view.web import template
class Template(template.Template):
zpt = ViewPageTemplateFile('content.pt')
macroTemplate = ViewPageTemplateFile('main.pt')
skin = None
@Lazy
def main_macro(self):
return self.macroTemplate.macros['page']
@Lazy
def resourceBase(self):
skinSetter = self.skin and ('/++skin++' + self.skin.__name__) or ''
# TODO: put '/@@' etc after path to site instead of directly after URL0
return self.request.URL[0] + skinSetter + '/@@/'
def render(self, *args, **kw):
kw['template'] = self
return self.zpt(*args, **kw)