Skip to content

Commit

Permalink
Moving over to official docs area
Browse files Browse the repository at this point in the history
  • Loading branch information
pauleveritt committed Nov 24, 2011
1 parent 76a9807 commit 5b58ef1
Show file tree
Hide file tree
Showing 267 changed files with 6,568 additions and 0 deletions.
29 changes: 29 additions & 0 deletions humans/allnose.sh
@@ -0,0 +1,29 @@

export TOP_DIR=/Users/paul/projects/scratchpad/tutorial
cd $TOP_DIR

cd creatingux
cd step01; nosetests; cd ..
cd step02; nosetests; cd ..
cd step03; nosetests; cd ..
cd step04; nosetests; cd ..
cd step05; nosetests; cd ..
cd step06; nosetests; cd ..
cd step06; nosetests; cd ..
cd step08; nosetests; cd ..
cd step09; nosetests; cd ..
cd step10; nosetests; cd ..

cd $TOP_DIR
cd resources
cd step01; nosetests; cd ..
cd step02; nosetests; cd ..
cd step03; nosetests; cd ..
cd step04; nosetests; cd ..
cd step05; nosetests; cd ..

cd $TOP_DIR
cd forms_schemas
cd step01; nosetests; cd ..
cd step02; nosetests; cd ..
cd step03; nosetests; cd ..
22 changes: 22 additions & 0 deletions humans/catalog/application.py
@@ -0,0 +1,22 @@
from pyramid.config import Configurator
from paste.httpserver import serve
from pyramid_zodbconn import get_connection
from resources import bootstrap

def root_factory(request):
conn = get_connection(request)
return bootstrap(conn.root())

def main():
settings = {"zodbconn.uri": "file://Data.fs"}
config = Configurator(root_factory=root_factory, settings=settings)
config.include("pyramid_zodbconn")
config.include("pyramid_tm")
config.add_static_view('static', 'deform:static')
config.scan("views")
app = config.make_wsgi_app()
return app

if __name__ == '__main__':
app = main()
serve(app, host='0.0.0.0')
116 changes: 116 additions & 0 deletions humans/catalog/index.rst
@@ -0,0 +1,116 @@
====================
Using repoze.catalog
====================

Goals
=====

- Index and and search for content using ``repoze.catalog``, a Python
indexing system tool based on ZODB.

.. warning::

Caveat: this will likely only work on systems that have C compilation tools
installed (XCode, Linux) *or* on Windows systems. If you can't get
``repoze.catalog`` installed properly you may need to pair up with someone
who can.

Objectives
==========

- Install ``repoze.catalog``.

- Index the ``title`` and ``content`` attributes of content we add to the
system into fulltext indices.

- Search for, and find, content we've added to the system using fulltext
queries.

Steps
=====

#. ``easy_install repoze.catalog``

#. ``mkdir catalog``

#. Copy the following into ``zodb/application.py``:

.. literalinclude:: application.py
:linenos:

#. Copy the following into ``zodb/views.py``:

.. literalinclude:: views.py
:linenos:

#. Copy the following into ``zodb/resources.py``:

.. literalinclude:: resources.py
:linenos:

#. ``$ python application.py``

#. Open ``http://127.0.0.1:8080/`` in your browser.

#. Add folders and documents.

Extra Credit
============

- Add another attribute to documents and folders named ``age`` (an integer)
and use a ``repoze.catalog.FieldIndex`` to index and search the age of new
documents. See http://docs.repoze.org/catalog/

- Change the ``query = `` line in folder_view to not care about what's in
``title`` (instead, only care about what's in ``content``).

- Unindex a document.

Analysis
========

We made no changes to ``application.py``.

resources.py
------------

Note the imports of ``catalog`` and ``index`` from ``repoze.catalog``.

We create a catalog and two text indexes for title and content attributes.

We add the catalog *inside* the site folder. We also add a document map, which
helps us map actual content to catalog ids.

views.py
--------

On the add_folder and add_content views, we now index the document and add it
to the document map. We use the content ittem's path on the site to make the
map.

To obtain a nice docid for the catalog, we use document_map.new_docid().

The path is obtained using the pyramid.traversal.resource_path() call.

After that we can index with catalog.index_doc().

The search view makes a query for all content with the search term either in
the title or the content of all catalogued items.

The [results] dance afterwards is to get the actual objects from the doc_id via
the document map.

Note the use of render_to_response to use the search template and not the one
configured for this view.

Discussion
==========

- ``repoze.catalog`` uses ZODB under the hood but isn't only for applications
that use ZODB for business data storage. Can be used like Lucene or
Xapian.

- ``query`` value ``'foo' in title or 'foo' in content`` is a "CQE" (catalog
query expression). This is a declarative query system, not unlike SQL (but
less expressive).

38 changes: 38 additions & 0 deletions humans/catalog/resources.py
@@ -0,0 +1,38 @@
from persistent import Persistent
from persistent.mapping import PersistentMapping

from repoze.catalog.indexes.text import CatalogTextIndex
from repoze.catalog.catalog import Catalog
from repoze.catalog.document import DocumentMap


class Folder(PersistentMapping):
def __init__(self, title):
super(Folder, self).__init__()
self.title = title


class SiteFolder(Folder):
__name__ = None
__parent__ = None


class Document(Persistent):
def __init__(self, title, content):
self.title = title
self.content = content


def bootstrap(zodb_root):
if not 'projector' in zodb_root:
# add site folder
root = SiteFolder('Projector Site')
zodb_root['projector'] = root
# add catalog and document map
catalog = Catalog()
catalog['title'] = CatalogTextIndex('title')
catalog['content'] = CatalogTextIndex('content')
root.catalog = catalog
document_map = DocumentMap()
root.document_map = document_map
return zodb_root['projector']
9 changes: 9 additions & 0 deletions humans/catalog/templates/document_view.pt
@@ -0,0 +1,9 @@
<div>
<p tal:condition="context.__parent__">
<a href="${request.resource_url(context.__parent__)}">
Up to ${context.__parent__.title}
</a>
</p>
<h2>${context.title}</h2>
<p>${context.content}</p>
</div>
27 changes: 27 additions & 0 deletions humans/catalog/templates/folder_view.pt
@@ -0,0 +1,27 @@
<html>
<head>
<link rel="stylesheet" href="/static/css/form.css" type="text/css" />
<!-- JavaScript -->
<script type="text/javascript"
src="/static/scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript"
src="/static/scripts/deform.js"></script>
</head>
<body>
<h2>Folder Title: ${context.title}</h2>
<p tal:condition="context.__parent__">
<a href="${request.resource_url(context.__parent__)}">
Up to ${context.__parent__.title}
</a>
</p>
<ul>
<li tal:repeat="child context.values()">
<a href="${request.resource_url(child)}">${child.title}</a>
</li>
</ul>
<p><a href="add_folder">Add folder</a><br/>
<a href="add_document">Add document</a></p>
<h3>Search</h3>
<p tal:replace="structure search_form"></p>
</body>
</html>
16 changes: 16 additions & 0 deletions humans/catalog/templates/form.pt
@@ -0,0 +1,16 @@
<html>
<head>
<link rel="stylesheet" href="/static/css/form.css" type="text/css" />
<!-- JavaScript -->
<script type="text/javascript"
src="/static/scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript"
src="/static/scripts/deform.js"></script>
</head>
<body>
<div>
<h2>Add Content</h2>
<span tal:replace="structure form" />
</div>
</body>
</html>
13 changes: 13 additions & 0 deletions humans/catalog/templates/search.pt
@@ -0,0 +1,13 @@
<div>
<h2>Search for ${term}</h2>
<p>
<a href="/">
Back to site root
</a>
</p>
<ol>
<li tal:repeat="result results">
<a href="${request.resource_url(result)}">${result.title}</a>
</li>
</ol>
</div>
20 changes: 20 additions & 0 deletions humans/catalog/templates/site_view.pt
@@ -0,0 +1,20 @@
<html>
<head>
<link rel="stylesheet" href="/static/css/form.css" type="text/css" />
<!-- JavaScript -->
<script type="text/javascript"
src="/static/scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript"
src="/static/scripts/deform.js"></script>
</head>
<body>
<div>
<h2>SiteFolder Title: ${context.title}</h2>
<ul>
<li tal:repeat="child children">
<a href="${child.__name__}">${child.title}</a>
</li>
</ul>
</div>
</body>
</html>

0 comments on commit 5b58ef1

Please sign in to comment.