Changeset 6786 for main


Ignore:
Timestamp:
18 Sep 2011, 00:04:33 (13 years ago)
Author:
uli
Message:

Brush up and add a 'simple', programmatic students catalog search (for
what it's worth).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/catalog.py

    r6778 r6786  
    55from hurry.query import Eq, Text
    66from hurry.query.query import Query
    7 from zope.index.text.parsetree import ParseError
     7from zope.catalog.interfaces import ICatalog
     8from zope.component import queryUtility
    89from waeup.sirp.interfaces import IUniversity, IQueryResultItem
    910from waeup.sirp.students.interfaces import IStudent
     
    2627
    2728    title = u'Student Query Item'
    28     description = u'Some students found in a search'
     29    description = u'Some student found in a search'
    2930
    3031    def __init__(self, context, view):
     
    3940def search(query=None, searchtype=None, view=None):
    4041    hitlist = []
    41     #if not query:
    42     #    view.flash('Empty search string.')
    43     #    return
    4442    if searchtype in ('name',):
    4543        results = Query().searchResults(
     
    4846        # Temporary solution to display all students added
    4947        if query == '*':
    50             from zope.component import queryUtility
    51             from zope.catalog.interfaces import ICatalog
    5248            cat = queryUtility(ICatalog, name='students_catalog')
    5349            results = cat.searchResults(student_id=(None, None))
     
    5854        hitlist.append(StudentQueryResultItem(result, view=view))
    5955    return hitlist
     56
     57class SimpleFieldSearch(object):
     58    """A programmatic (no UI required) search.
     59
     60    Looks up a given field attribute of the students catalog for a
     61    single value. So normally you would call an instance of this
     62    search like this:
     63
     64      >>> SimpleFieldSearch()(reg_number='somevalue')
     65
     66    """
     67    catalog_name = 'students_catalog'
     68    def __call__(self, **kw):
     69        """Search students catalog programmatically.
     70        """
     71        if len(kw) != 1:
     72            raise ValueError('must give exactly one index name to search')
     73        cat = queryUtility(ICatalog, name=self.catalog_name)
     74        index_name, query_term = kw.items()[0]
     75        results = cat.searchResults(index_name=(query_term, query_term))
     76        return results
     77
     78#: an instance of `SimpleFieldSearch` looking up students catalog.
     79simple_search = SimpleFieldSearch()
Note: See TracChangeset for help on using the changeset viewer.