import os import grok from grok import index from hurry.query.query import Query, Text from hurry.query import Eq from waeup.sirp.app import University from waeup.sirp.interfaces import ICourse, ICertificateCourse from waeup.sirp.student.interfaces import IStudent from zope.app.catalog.interfaces import ICatalog from zope.app.intid.interfaces import IIntIds from zope.component import getUtility from zope.component.interfaces import ComponentLookupError class StudentIndexes(grok.Indexes): grok.site(University) grok.name('students_catalog') grok.context(IStudent) name = index.Field(attribute='name') class CourseIndexes(grok.Indexes): grok.site(University) grok.name('courses_catalog') grok.context(ICourse) code = index.Field(attribute='code') title = index.Text(attribute='title') class CourseCertificatesIndexes(grok.Indexes): grok.site(University) grok.name('certcourses_catalog') grok.context(ICertificateCourse) course_code = index.Field(attribute='getCourseCode') @grok.subscribe(ICourse, grok.IObjectAddedEvent) def handleCourseAdd(obj, event): """Index an added course with the local catalog. Courses are not indexed automatically, as they are not a dictionary subitem of the accompanied site object (`IUniversity`). I.e. one cannot get them by asking for ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``. Once, a course is indexed we can leave the further handling to the default component architechture. At least removals will be handled correctly then (and the course unindexed). """ try: cat = getUtility(ICatalog, name='courses_catalog') except ComponentLookupError: # catalog not available. This might happen during tests. return intids = getUtility(IIntIds) index = cat['code'] index.index_doc(intids.getId(obj), obj) from zope.interface import Interface from zope import schema class IQueryResultItem(Interface): url = schema.TextLine( title = u'URL that links to the found item') title = schema.TextLine( title = u'Title displayed in search results.') description = schema.Text( title = u'Longer description of the item found.') class QueryResultItem(object): grok.implements(IQueryResultItem) url = None title = None description = None def __init__(self, context, view): self.context = context self.url = view.url(context) self.title = context.title self.description = '' class CourseQueryResultItem(QueryResultItem): def __init__(self, context, view): self.context = context self.url = view.url(context) self.title = "COURSE: " + context.title self.description = 'code: %s' % context.code def search(query=None, view=None): if not query: return [] cat = getUtility(ICatalog, name='courses_catalog') results = list(cat.searchResults(code=(query, query))) hitlist = [] results = Query().searchResults( Eq(('courses_catalog', 'code'), query)) for result in results: hitlist.append(CourseQueryResultItem(result, view=view)) results = Query().searchResults( Text(('courses_catalog', 'title'), query)) for result in results: hitlist.append(CourseQueryResultItem(result, view=view)) return hitlist def search_context(query): result = Query().searchResults( Eq(('students_catalog', 'name'), query) ) return result