Changeset 6207
- Timestamp:
- 28 May 2011, 07:19:57 (14 years ago)
- Location:
- main/waeup.sirp/trunk/src/waeup/sirp
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/catalog.py
r6116 r6207 15 15 16 16 class WAeUPQuery(Query): 17 18 19 17 """A hurry.query-like query that supports also ``apply``. 18 """ 19 grok.implements(IQuery) 20 20 21 22 23 24 21 def apply(self, query): 22 """Get a catalog's BTree set of intids conforming to a query. 23 """ 24 return query.apply() 25 25 26 27 28 29 30 31 32 33 26 def searchResults(self, query): 27 """Get a set of ZODB objects conforming to a query. 28 """ 29 results = self.apply(query) 30 if results is not None: 31 uidutil = getUtility(IIntIds) 32 results = ResultSet(results, uidutil) 33 return results 34 34 35 35 grok.global_utility(WAeUPQuery) 36 36 37 class StudentIndexes(grok.Indexes): 38 grok.site(University) 39 grok.name('students_catalog') 40 grok.context(IStudent) 37 41 38 class StudentIndexes(grok.Indexes): 39 grok.site(University) 40 grok.name('students_catalog') 41 grok.context(IStudent) 42 43 name = grok.index.Field(attribute='name') 42 name = grok.index.Field(attribute='name') 44 43 45 44 class QueryResultItem(object): 46 47 48 49 45 grok.implements(IQueryResultItem) 46 url = None 47 title = None 48 description = None 50 49 51 52 53 54 55 50 def __init__(self, context, view): 51 self.context = context 52 self.url = view.url(context) 53 self.title = context.title 54 self.description = '' 56 55 57 56 def search_context(query): 58 59 60 61 57 result = Query().searchResults( 58 Eq(('students_catalog', 'name'), query) 59 ) 60 return result -
main/waeup.sirp/trunk/src/waeup/sirp/university/catalog.py
r5992 r6207 3 3 import grok 4 4 from hurry.query import Eq 5 try: 6 from zope.catalog.interfaces import ICatalog 7 except ImportError: 8 # BBB 9 from zope.app.catalog.interfaces import ICatalog 5 from zope.catalog.interfaces import ICatalog 10 6 from zope.component import getUtility 11 7 from zope.component.interfaces import ComponentLookupError 12 try: 13 from zope.intid import IIntIds 14 except ImportError: 15 # BBB 16 from zope.app.intid.interfaces import IIntIds 8 from zope.intid import IIntIds 17 9 from waeup.sirp.interfaces import IUniversity 18 10 from waeup.sirp.catalog import QueryResultItem 19 11 from waeup.sirp.university.interfaces import ( 20 21 12 ICourse, ICertificateCourse, IDepartment, 13 ) 22 14 23 15 class CourseIndexes(grok.Indexes): 24 25 26 16 grok.site(IUniversity) 17 grok.name('courses_catalog') 18 grok.context(ICourse) 27 19 28 29 20 code = grok.index.Field(attribute='code') 21 title = grok.index.Text(attribute='title') 30 22 31 23 class CourseCertificatesIndexes(grok.Indexes): 32 33 34 24 grok.site(IUniversity) 25 grok.name('certcourses_catalog') 26 grok.context(ICertificateCourse) 35 27 36 28 course_code = grok.index.Field(attribute='getCourseCode') 37 29 38 30 @grok.subscribe(ICourse, grok.IObjectAddedEvent) 39 31 def handleCourseAdd(obj, event): 40 32 """Index an added course with the local catalog. 41 33 42 43 44 45 46 34 Courses are not indexed automatically, as they are not a 35 dictionary subitem of the accompanied site object 36 (`IUniversity`). I.e. one cannot get them by asking for 37 ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for 38 ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``. 47 39 48 49 50 51 52 53 54 55 56 57 58 59 40 Once, a course is indexed we can leave the further handling to 41 the default component architechture. At least removals will 42 be handled correctly then (and the course unindexed). 43 """ 44 try: 45 cat = getUtility(ICatalog, name='courses_catalog') 46 except ComponentLookupError: 47 # catalog not available. This might happen during tests. 48 return 49 intids = getUtility(IIntIds) 50 index = cat['code'] 51 index.index_doc(intids.getId(obj), obj) 60 52 61 53 @grok.subscribe(IDepartment, grok.IObjectRemovedEvent) 62 54 def handleDepartmentRemoval(obj, event): 63 64 65 66 67 55 """Clear courses and certificates when a department is killed. 56 """ 57 obj.courses.clear() 58 obj.certificates.clear() 59 return 68 60 69 61 class CourseQueryResultItem(QueryResultItem): 70 71 72 73 74 62 def __init__(self, context, view): 63 self.context = context 64 self.url = view.url(context) 65 self.title = "COURSE: " + context.title 66 self.description = 'code: %s' % context.code 75 67 76 68 def search(query=None, view=None): 77 if not query: 78 return [] 79 cat = getUtility(ICatalog, name='courses_catalog') 80 results = list(cat.searchResults(code=(query, query))) 81 82 hitlist = [] 83 results = Query().searchResults( 84 Eq(('courses_catalog', 'code'), query)) 85 for result in results: 86 hitlist.append(CourseQueryResultItem(result, view=view)) 69 if not query: 70 return [] 71 cat = getUtility(ICatalog, name='courses_catalog') 72 results = list(cat.searchResults(code=(query, query))) 87 73 88 results = Query().searchResults(89 Text(('courses_catalog', 'title'), query))90 91 92 74 hitlist = [] 75 results = Query().searchResults( 76 Eq(('courses_catalog', 'code'), query)) 77 for result in results: 78 hitlist.append(CourseQueryResultItem(result, view=view)) 93 79 94 return hitlist 80 results = Query().searchResults( 81 Text(('courses_catalog', 'title'), query)) 82 83 for result in results: 84 hitlist.append(CourseQueryResultItem(result, view=view)) 85 86 return hitlist
Note: See TracChangeset for help on using the changeset viewer.