added web sub-package for generic view support and other web- and browser-related stuff
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1520 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
522fd5168a
commit
335c15f17a
6 changed files with 160 additions and 0 deletions
27
web/README.txt
Normal file
27
web/README.txt
Normal 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.
|
||||||
|
|
||||||
|
But 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.web.view import View
|
||||||
|
>>> view = View(None, TestRequest())
|
||||||
|
>>> view.render()
|
||||||
|
u'<html...<body>...</body>...</html>...'
|
||||||
|
|
3
web/__init__.py
Normal file
3
web/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
"""
|
||||||
|
$Id$
|
||||||
|
"""
|
15
web/content.pt
Normal file
15
web/content.pt
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<html x_metal:use-macro="context/@@skin_macros/page"
|
||||||
|
i18n:domain="zope">
|
||||||
|
<head></head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
|
||||||
|
<x_metal:body fill-slot="body">
|
||||||
|
<div>
|
||||||
|
This is the body
|
||||||
|
</div>
|
||||||
|
</x_metal:body>
|
||||||
|
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
39
web/template.py
Normal file
39
web/template.py
Normal 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 (if fact it is the ZPT-based type...).
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
"""
|
||||||
|
|
||||||
|
from zope.app.pagetemplate import ViewPageTemplateFile
|
||||||
|
|
||||||
|
|
||||||
|
class Template(object):
|
||||||
|
|
||||||
|
zpt = ViewPageTemplateFile('content.pt')
|
||||||
|
|
||||||
|
def __init__(self, view):
|
||||||
|
self.view = view
|
||||||
|
self.context = view.context
|
||||||
|
self.request = view.request
|
||||||
|
|
||||||
|
def render(self, *args, **kw):
|
||||||
|
return self.zpt(*args, **kw)
|
||||||
|
|
30
web/tests.py
Executable file
30
web/tests.py
Executable 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.web 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')
|
46
web/view.py
Normal file
46
web/view.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
#
|
||||||
|
# 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 cybertools.web.template import Template
|
||||||
|
|
||||||
|
|
||||||
|
class View(object):
|
||||||
|
|
||||||
|
templateFactory = Template
|
||||||
|
|
||||||
|
_template = None
|
||||||
|
|
||||||
|
def __init__(self, context, request):
|
||||||
|
self.context = context
|
||||||
|
self.request = request
|
||||||
|
|
||||||
|
@property
|
||||||
|
def template(self):
|
||||||
|
if self._template is None and self.templateFactory:
|
||||||
|
self._template = self.templateFactory(self)
|
||||||
|
return self._template
|
||||||
|
|
||||||
|
def render(self, *args, **kw):
|
||||||
|
return self.template.render(*args, **kw)
|
||||||
|
|
Loading…
Add table
Reference in a new issue