[Zope3-checkins] CVS: Zope3/src/zope/app/index/browser/field - __init__.py:1.1 configure.zcml:1.1 control.pt:1.1 control.py:1.1 tests.py:1.1

Philipp von Weitershausen philikon at philikon.de
Tue Mar 2 09:40:08 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/index/browser/field
In directory cvs.zope.org:/tmp/cvs-serv24867/index/browser/field

Added Files:
	__init__.py configure.zcml control.pt control.py tests.py 
Log Message:
Moved index interfaces and browser views to zope.app.index.


=== Added File Zope3/src/zope/app/index/browser/field/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/index/browser/field/configure.zcml ===
<zope:configure 
   xmlns:zope="http://namespaces.zope.org/zope"
   xmlns="http://namespaces.zope.org/browser"
   >

  <addform
      name="AddFieldIndex"
      menu="add_component" title="Field Index"
      schema="zope.app.index.interfaces.field.IUIFieldIndex"
      permission="zope.ManageServices"
      content_factory="zope.app.index.field.index.FieldIndex"
      arguments="field_name"
      keyword_arguments="interface"
      />

  <page
      for="zope.app.index.interfaces.field.IUIFieldIndex"
      name="index.html" 
      menu="zmi_views" title="Control"
      permission="zope.ManageServices"
      class=".control.ControlView"
      template="control.pt"
      />

  <editform
      name="edit.html"
      schema="zope.app.index.interfaces.field.IUIFieldCatalogIndex"
      label="Edit Field Index"
      permission="zope.ManageContent"
      menu="zmi_views" title="Edit"
      />

</zope:configure>


=== Added File Zope3/src/zope/app/index/browser/field/control.pt ===
<html metal:use-macro="views/standard_macros/page">
<head>
  <title metal:fill-slot="title" i18n:translate="">
    FieldIndex Control Page
  </title>
</head>

<body>
<div metal:fill-slot="body">

  <br />
  <h1 i18n:translate="">FieldIndex</h1>

  <p class="form-help" i18n:translate="">
      This page lets you control a field index, which is used to
      provide a single field searching facility.  The search box here is
      only for debugging.  Subscription status: A "subscribed" index
      will update itself whenever objects are added, deleted or
      modified; an "unsubscribed" index will retain the indexing
      information but not update itself further.
  </p>

  <!-- XXX: Too much logic for a template -->
  <span tal:condition="request/callSubscribe|nothing" tal:omit-tag="">
    <span tal:define="dummy context/subscribe" tal:omit-tag=""/>
    <span i18n:translate="">Successfully subscribed.</span>
  </span>

  <span tal:condition="request/callUnsubscribe|nothing" tal:omit-tag="">
    <span tal:define="dummy context/unsubscribe" tal:omit-tag=""/>
    <span i18n:translate="">Successfully unsubscribed.</span>
  </span>

  <div tal:condition="context/interface" i18n:translate="">
    Adapting objects to: 
    <span tal:replace="view/interface_name" i18n:name="iface_name" />
  </div>

  <div i18n:translate="">
    Indexing on attribute: 
   <span tal:replace="context/field_name"  i18n:name="field_name"/>
  </div>

  <div i18n:translate="">
    Documents: 
    <span tal:replace="context/documentCount" i18n:name="doc_count"/>
  </div>

  <form method="POST">
    <span tal:condition="context/isSubscribed" tal:omit-tag="">
      <span i18n:translate="">Subscription state: ON</span>
      <input type="submit" value="Unsubscribe" name="callUnsubscribe" 
             i18n:attributes="value unsubscribe-button"/>
     </span>
     <span tal:condition="not:context/isSubscribed" tal:omit-tag="">
       <span i18n:translate="">Subscription state: OFF</span>
       <input type="submit" value="Subscribe" name="callSubscribe"
              i18n:attributes="value subscribe-button"/>
     </span>
     <input type="hidden" name="queryText" value=""
            tal:attributes="value request/queryText|nothing" />
  </form>

  <form method="GET">
    <input type="text" name="queryText" value=""
           tal:attributes="value request/queryText|nothing" />
    <input type="submit" value="Query" />
  </form>

  <div tal:condition="request/queryText|nothing" tal:omit-tag="">
    <div tal:define="result view/query" tal:omit-tag="">

      <div tal:condition="not:result/total" i18n:translate="">
        No hits.  Please try another query.
      </div>

      <div tal:condition="result/total">
        <span tal:omit-tag="" i18n:translate="">
          Hits 
          <span tal:omit-tag="" tal:content="result/first" i18n:name="first"/>
          -
          <span tal:omit-tag="" tal:content="result/last" i18n:name="last"/>
          of
          <span tal:omit-tag="" tal:content="result/total" i18n:name="total"/>
        </span>:

        <div tal:repeat="info result/results" i18n:translate="">
          title=<span tal:content="info/title" i18n:name="title"/>;
          url=<a href="location"
                 tal:attributes="href info/location"
                 tal:content="info/location"
                 i18n:name="title">url</a>
        </div>
      </div>

      <span tal:condition="exists:result/prev">
        <a href="next"
            tal:attributes="href
              string:?queryText=${request/queryText}&start=${view/prevBatch}"
            i18n:translate="">
          &lt;-- PREVIOUS BATCH
        </a>
      </span>
      <span tal:condition="exists:result/next">
        <a href="next"
           tal:attributes="href
             string:?queryText=${request/queryText}&start=${view/nextBatch}"
           i18n:translate="">
          NEXT BATCH --&gt;
        </a>
      </span>
    </div>
  </div>

</div>
</body>
</html>


=== Added File Zope3/src/zope/app/index/browser/field/control.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Control view for the text index.

$Id: control.py,v 1.1 2004/03/02 14:40:05 philikon Exp $
"""
from zope.app.introspector import interfaceToName
from zope.app.dublincore.interfaces import IZopeDublinCore
from zope.app.index.interfaces.text import IQueryView
from zope.app.services.servicenames import HubIds
from zope.app.traversing import canonicalPath
from zope.component import getService, queryAdapter
from zope.exceptions import NotFoundError
from zope.interface import implements
from zope.publisher.browser import BrowserView

class ControlView(BrowserView):

    implements(IQueryView)

    def __init__(self, context, request):
        super(ControlView, self).__init__(context, request)
        self.hub = getService(context, HubIds)

    def interface_name(self):
        return interfaceToName(self.context, self.context.interface)

    def query(self):
        queryText = self.request.get('queryText', '')
        results = self.context.search(queryText)
        nresults = len(results)
        first = 1
        last = first + len(results) - 1
        result = {
            'results': list(self._resultIterator(results)),
            'nresults': nresults,
            'first': first,
            'last': last,
            'total': nresults,
            }
        return result

    def _resultIterator(self, results):
        for hubid in results:
            yield self._cookInfo(hubid)

    def _cookInfo(self, hubid):
        location = canonicalPath(self.hub.getPath(hubid))
        # XXX `location` is later used as a URL in a page template, using a
        #     physical path instead of absolute_url will break virtual hosting.
        result = {
            'location': location,
            }
        try:
            object = self.hub.getObject(hubid)
        except NotFoundError:
            pass
        else:
            dc = queryAdapter(object, IZopeDublinCore, context=self.context)
            if dc is not None:
                title = dc.title
                result['title'] = title
        return result


=== Added File Zope3/src/zope/app/index/browser/field/tests.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Unit test for field index browser views

$Id: tests.py,v 1.1 2004/03/02 14:40:05 philikon Exp $
"""

import unittest
from zope.interface import implements
from zope.publisher.browser import TestRequest
from zope.component import getServiceManager
from zope.exceptions import NotFoundError

from zope.app.tests.placelesssetup import PlacelessSetup
from zope.app.services.servicenames import HubIds
from zope.app.interfaces.services.hub import IObjectHub
from zope.app.dublincore.interfaces import IZopeDublinCore
from zope.app.index.browser.field.control import ControlView

class DublinCoreStub:
    implements(IZopeDublinCore)

    def __init__(self, title):
        self.title = title

class ObjectHubStub:
    implements(IObjectHub)

    def getPath(self, id):
        return {101: '/a', 102: '/b'}[id]

    def getObject(self, id):
        if id == 102:
            return DublinCoreStub(title='Foo')
        else:
            raise NotFoundError

class FieldIndexStub:

    def search(self, values):
        assert values == 'xyzzy'
        return [101, 102]

class TestControlView(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        super(TestControlView, self).setUp()
        service_manager = getServiceManager(None)
        service_manager.defineService(HubIds, IObjectHub)
        service_manager.provideService(HubIds, ObjectHubStub())

    def test_query(self):
        index = FieldIndexStub()
        request = TestRequest()
        view = ControlView(index, request)
        request.form['queryText'] = 'xyzzy'
        results = view.query()
        self.assertEquals(results, {'results': [
                                            {'location': '/a'},
                                            {'location': '/b', 'title': 'Foo'}
                                        ],
                                    'nresults': 2,
                                    'first': 1,
                                    'last': 2,
                                    'total': 2})


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestControlView))
    return suite


if __name__ == '__main__':
    unittest.main()




More information about the Zope3-Checkins mailing list