| 1 | from Products.CMFPlone.utils import _createObjectByType |
|---|
| 2 | from Products.CMFCore.utils import getToolByName |
|---|
| 3 | from zope.component import getUtility, getMultiAdapter |
|---|
| 4 | from plone.portlets.interfaces import IPortletManager, IPortletAssignmentMapping |
|---|
| 5 | from enpraxis.educommons import portlet |
|---|
| 6 | from utilities.interfaces import IECUtility |
|---|
| 7 | from utilities.utils import eduCommonsUtility |
|---|
| 8 | from zope.app.component.interfaces import ISite |
|---|
| 9 | from zope.app.component.hooks import setSite |
|---|
| 10 | from Products.Five.site.localsite import enableLocalSiteHook |
|---|
| 11 | from zope.component import getSiteManager |
|---|
| 12 | from eventHandlers import syndicateFolderishObject |
|---|
| 13 | from Products.CMFPlone.CatalogTool import registerIndexableAttribute |
|---|
| 14 | from zope.component.interfaces import ComponentLookupError |
|---|
| 15 | from zope.annotation.interfaces import IAnnotations |
|---|
| 16 | from Products.ProxyIndex import ProxyIndex |
|---|
| 17 | from enpraxis.educommons import eduCommonsMessageFactory as _ |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | # setup handlers for eduCommons |
|---|
| 21 | |
|---|
| 22 | def importFinalSteps(context): |
|---|
| 23 | site = context.getSite() |
|---|
| 24 | setupDefaultPortlets(site) |
|---|
| 25 | defaultSettings(site) |
|---|
| 26 | setupUtilities(site) |
|---|
| 27 | setupControlPanel(site) |
|---|
| 28 | setupTransforms(site) |
|---|
| 29 | setupKupu(site) |
|---|
| 30 | customizeAddOnProducts(site) |
|---|
| 31 | addCopyrightIndexToCatalog(site) |
|---|
| 32 | addCopyrightMetadataToCatalog(site) |
|---|
| 33 | addAccessibilityIndexToCatalog(site) |
|---|
| 34 | addAccessibilityMetadataToCatalog(site) |
|---|
| 35 | addObjPositionIndexToCatalog(site) |
|---|
| 36 | addObjPositionMetadataToCatalog(site) |
|---|
| 37 | |
|---|
| 38 | def importContent(context): |
|---|
| 39 | site = context.getSite() |
|---|
| 40 | if getattr(site, 'REQUEST', None): |
|---|
| 41 | if site.REQUEST.has_key('title'): |
|---|
| 42 | site.setTitle(site.REQUEST['title']) |
|---|
| 43 | setupPortalContent(site) |
|---|
| 44 | |
|---|
| 45 | def setupUtilities(site): |
|---|
| 46 | """ Register a local utility """ |
|---|
| 47 | |
|---|
| 48 | if not ISite.providedBy(site): |
|---|
| 49 | enableLocalSiteHook(site) |
|---|
| 50 | |
|---|
| 51 | setSite(site) |
|---|
| 52 | |
|---|
| 53 | sm = getSiteManager() |
|---|
| 54 | if not sm.queryUtility(IECUtility): |
|---|
| 55 | sm.registerUtility(eduCommonsUtility('educommonsutility'), |
|---|
| 56 | IECUtility) |
|---|
| 57 | |
|---|
| 58 | def setupControlPanel(site): |
|---|
| 59 | """ Tweak Control Panel conditions """ |
|---|
| 60 | from Products.CMFCore.Expression import Expression |
|---|
| 61 | control_panel = site.portal_controlpanel |
|---|
| 62 | |
|---|
| 63 | panels = ['ZMI', 'NavigationSettings', 'QuickInstaller', 'TypesSettings', 'portal_atct', 'errorLog'] |
|---|
| 64 | actions = control_panel.listActions() |
|---|
| 65 | |
|---|
| 66 | for panel in panels: |
|---|
| 67 | for action in actions: |
|---|
| 68 | if panel == action.id: |
|---|
| 69 | action.condition = Expression('python:"Manager" in here.portal_membership.getAuthenticatedMember().getRoles()') |
|---|
| 70 | |
|---|
| 71 | def setupTransforms(portal): |
|---|
| 72 | from Products.CMFDefault.utils import VALID_TAGS |
|---|
| 73 | from Products.CMFDefault.utils import NASTY_TAGS |
|---|
| 74 | |
|---|
| 75 | valid_tags = VALID_TAGS.copy() |
|---|
| 76 | nasty_tags = NASTY_TAGS.copy() |
|---|
| 77 | |
|---|
| 78 | nasty_tags.pop('applet') |
|---|
| 79 | nasty_tags.pop('embed') |
|---|
| 80 | nasty_tags.pop('object') |
|---|
| 81 | nasty_tags.pop('script') |
|---|
| 82 | |
|---|
| 83 | valid_tags['applet'] = 1 |
|---|
| 84 | valid_tags['embed'] = 1 |
|---|
| 85 | valid_tags['object'] = 1 |
|---|
| 86 | valid_tags['thead'] = 1 |
|---|
| 87 | valid_tags['tfoot'] = 1 |
|---|
| 88 | valid_tags['param'] = 0 |
|---|
| 89 | |
|---|
| 90 | kwargs = {'nasty_tags': nasty_tags, |
|---|
| 91 | 'valid_tags': valid_tags, |
|---|
| 92 | 'remove_javascript': 0} |
|---|
| 93 | |
|---|
| 94 | transform = getattr(getToolByName(portal, 'portal_transforms'), 'safe_html') |
|---|
| 95 | |
|---|
| 96 | for k in list(kwargs): |
|---|
| 97 | if isinstance(kwargs[k], dict): |
|---|
| 98 | v = kwargs[k] |
|---|
| 99 | kwargs[k+'_key'] = v.keys() |
|---|
| 100 | kwargs[k+'_value'] = [str(s) for s in v.values()] |
|---|
| 101 | del kwargs[k] |
|---|
| 102 | |
|---|
| 103 | transform.set_parameters(**kwargs) |
|---|
| 104 | |
|---|
| 105 | def setupKupu(portal): |
|---|
| 106 | kupu_tool = getToolByName(portal, 'kupu_library_tool') |
|---|
| 107 | |
|---|
| 108 | combos = [] |
|---|
| 109 | |
|---|
| 110 | #enable original size images |
|---|
| 111 | kupu_tool.allowOriginalImageSize = True |
|---|
| 112 | |
|---|
| 113 | #remove default stripped combo in kupu |
|---|
| 114 | kupu_tool.set_stripped_combinations(combos) |
|---|
| 115 | |
|---|
| 116 | #Remove image_preview class from embedded images |
|---|
| 117 | kupu_tool.updatePreviewActions([{'classes': '', 'defscale': '', 'expression': 'string:${object_url}/image_thumb', 'marker': 'x', 'mediatype': 'image', 'normal': '', 'portal_type': 'Image', 'scalefield': 'image'}, |
|---|
| 118 | {'classes': '', 'defscale': 'image_preview', 'expression': 'string:${object_url}/image_thumb', 'marker': 'x', 'mediatype': 'image', 'normal': '', 'portal_type': 'News Item', 'scalefield': 'image'}, |
|---|
| 119 | {'classes': '', 'defscale': 'image_preview', 'expression': '', 'marker': '-', 'mediatype': 'image', 'normal': '', 'portal_type': '', 'scalefield': 'image'}]) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | def setupPortalContent(portal): |
|---|
| 124 | """ Setup default eduCommons content """ |
|---|
| 125 | |
|---|
| 126 | existing = portal.objectIds() |
|---|
| 127 | wftool = getToolByName(portal, 'portal_workflow') |
|---|
| 128 | |
|---|
| 129 | syndicateFolderishObject(portal, event=None) |
|---|
| 130 | |
|---|
| 131 | # If Members, news, and/or event objects exist, remove them |
|---|
| 132 | delobjs = [] |
|---|
| 133 | if 'Members' in existing: |
|---|
| 134 | delobjs.append('Members') |
|---|
| 135 | if 'news' in existing: |
|---|
| 136 | delobjs.append('news') |
|---|
| 137 | if 'events' in existing: |
|---|
| 138 | delobjs.append('events') |
|---|
| 139 | if delobjs: |
|---|
| 140 | portal.manage_delObjects(delobjs) |
|---|
| 141 | |
|---|
| 142 | # Add the Course List |
|---|
| 143 | if 'courselist' not in existing: |
|---|
| 144 | # Create a new course list |
|---|
| 145 | _createObjectByType('CoursesTopic', portal, id='courselist', title='Courses', |
|---|
| 146 | description='A list of courses on this site.') |
|---|
| 147 | courselist = portal.courselist |
|---|
| 148 | # Set the criterion for the course list smart folder |
|---|
| 149 | crit = courselist.addCriterion('Type', 'ATPortalTypeCriterion') |
|---|
| 150 | crit.setValue('Division') |
|---|
| 151 | courselist.setSortCriterion('sortable_title', reversed=False) |
|---|
| 152 | courselist.setLayout('courses_listing') |
|---|
| 153 | # publish it |
|---|
| 154 | if wftool.getInfoFor(courselist, 'review_state') != 'published': |
|---|
| 155 | wftool.doActionFor(courselist, 'publish') |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | fptitle = 'Welcome to eduCommons' |
|---|
| 159 | fpdesc = 'eduCommons provides access to educational materials more commonly known as OpenCourseWare.' |
|---|
| 160 | if 'front-page' not in existing: |
|---|
| 161 | _createObjectByType('Document', portal,id='front-page', title=fptitle, |
|---|
| 162 | description=fpdesc) |
|---|
| 163 | context = portal.get('front-page') |
|---|
| 164 | context.setTitle(fptitle) |
|---|
| 165 | context.setDescription(fpdesc) |
|---|
| 166 | template = '@@frontpage_view' |
|---|
| 167 | # Need try/except for QuickInstaller installations of 3rd party products |
|---|
| 168 | try: |
|---|
| 169 | template = context.restrictedTraverse(str(template)) |
|---|
| 170 | text = template(context) |
|---|
| 171 | context.setText(text) |
|---|
| 172 | publishObject(context) |
|---|
| 173 | except AttributeError: |
|---|
| 174 | pass |
|---|
| 175 | |
|---|
| 176 | # Add About |
|---|
| 177 | if 'about' not in existing: |
|---|
| 178 | _createObjectByType('Folder', portal, id='about', title='About OCW', |
|---|
| 179 | description='Current information about the eduCommons install on this web site.') |
|---|
| 180 | about = portal.about |
|---|
| 181 | publishObject(about) |
|---|
| 182 | _createObjectByType('Document',about,id='abouttext_text',title='About OCW',description='About') |
|---|
| 183 | context = portal.about.abouttext_text |
|---|
| 184 | |
|---|
| 185 | publishObject(about.abouttext_text) |
|---|
| 186 | template = '@@abouttext_view' |
|---|
| 187 | template = context.restrictedTraverse(str(template)) |
|---|
| 188 | text = template(context) |
|---|
| 189 | context.setText(text) |
|---|
| 190 | context.setPresentation(True) |
|---|
| 191 | |
|---|
| 192 | about.setDefaultPage('abouttext_text') |
|---|
| 193 | |
|---|
| 194 | #Terms of Use |
|---|
| 195 | _createObjectByType('Document', about, id="terms-of-use", title="Terms of Use", |
|---|
| 196 | description='Terms of use for this web site.') |
|---|
| 197 | context = getattr(portal.about, 'terms-of-use') |
|---|
| 198 | publishObject(context) |
|---|
| 199 | template = '@@tou_view' |
|---|
| 200 | template = context.restrictedTraverse(str(template)) |
|---|
| 201 | text = template(context) |
|---|
| 202 | context.setText(text) |
|---|
| 203 | |
|---|
| 204 | #Privacy Policy |
|---|
| 205 | _createObjectByType('Document', about, id="privacy-policy", title="Privacy Policy", |
|---|
| 206 | description='The privacy policy for this web site.') |
|---|
| 207 | context = getattr(portal.about, 'privacy-policy') |
|---|
| 208 | publishObject(context) |
|---|
| 209 | template = '@@privacypolicy_view' |
|---|
| 210 | template = context.restrictedTraverse(str(template)) |
|---|
| 211 | text = template(context) |
|---|
| 212 | context.setText(text) |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | # Add Help |
|---|
| 217 | if 'help' not in existing: |
|---|
| 218 | _createObjectByType('Folder', portal, id='help', title='Help', |
|---|
| 219 | description='Help') |
|---|
| 220 | help = portal.help |
|---|
| 221 | publishObject(help) |
|---|
| 222 | |
|---|
| 223 | _createObjectByType('Document',help,id='help_text',title='Help',description='Help') |
|---|
| 224 | context = portal.help.help_text |
|---|
| 225 | |
|---|
| 226 | publishObject(context) |
|---|
| 227 | template = '@@faq_view' |
|---|
| 228 | template = context.restrictedTraverse(str(template)) |
|---|
| 229 | text = template(context) |
|---|
| 230 | context.setText(text) |
|---|
| 231 | context.setTableContents(True) |
|---|
| 232 | |
|---|
| 233 | help.setDefaultPage('help_text') |
|---|
| 234 | |
|---|
| 235 | #Accessibility Guidelines |
|---|
| 236 | _createObjectByType('Document', help, id="accessibility-guidelines", |
|---|
| 237 | title="Accessibility Guidelines", |
|---|
| 238 | description='Guidelines to help determine if content meets accessibility standards.') |
|---|
| 239 | context = getattr(portal.help, 'accessibility-guidelines') |
|---|
| 240 | publishObject(context) |
|---|
| 241 | template = '@@accessibilityguidelines_view' |
|---|
| 242 | template = context.restrictedTraverse(str(template)) |
|---|
| 243 | text = template(context) |
|---|
| 244 | context.setText(text) |
|---|
| 245 | |
|---|
| 246 | # Add Feedback |
|---|
| 247 | if 'feedback' not in existing: |
|---|
| 248 | _createObjectByType('Feedback', portal, id='feedback', title='Feedback', |
|---|
| 249 | description='Feedback') |
|---|
| 250 | feedback = portal.feedback |
|---|
| 251 | publishObject(feedback) |
|---|
| 252 | feedback.setLayout('feedback_view') |
|---|
| 253 | |
|---|
| 254 | _createObjectByType('Document', feedback, id='thanks', title='Thank You', |
|---|
| 255 | description='') |
|---|
| 256 | feedback.thanks.setText('Thank you for your feedback.') |
|---|
| 257 | feedback.thanks.setExcludeFromNav(True) |
|---|
| 258 | publishObject(feedback.thanks) |
|---|
| 259 | feedback.thanks.reindexObject() |
|---|
| 260 | |
|---|
| 261 | def publishObject(context): |
|---|
| 262 | """ Move an object into the published state """ |
|---|
| 263 | wftool = getToolByName(context, 'portal_workflow') |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | if wftool.getInfoFor(context, 'review_state') != 'Published': |
|---|
| 267 | wftool.doActionFor(context, 'submit') |
|---|
| 268 | wftool.doActionFor(context, 'release') |
|---|
| 269 | wftool.doActionFor(context, 'publish') |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | def setupDefaultPortlets(portal): |
|---|
| 274 | """ Setup default portlets for eduCommons """ |
|---|
| 275 | |
|---|
| 276 | leftColumn = getUtility(IPortletManager, name=u'plone.leftcolumn', context=portal) |
|---|
| 277 | left = getMultiAdapter((portal, leftColumn), IPortletAssignmentMapping, context=portal) |
|---|
| 278 | |
|---|
| 279 | # Add the eduCommons custom simple navigation portlet |
|---|
| 280 | if u'Simple Nav Portlet' not in left: |
|---|
| 281 | left[u'Simple Nav Portlet'] = portlet.simplenavportlet.Assignment() |
|---|
| 282 | |
|---|
| 283 | # Turn off other left hand portlets |
|---|
| 284 | if u'navigation' in left: |
|---|
| 285 | del left[u'navigation'] |
|---|
| 286 | if u'login' in left: |
|---|
| 287 | del left[u'login'] |
|---|
| 288 | if u'recent' in left: |
|---|
| 289 | del left[u'recent'] |
|---|
| 290 | |
|---|
| 291 | rightColumn = getUtility(IPortletManager, name=u'plone.rightcolumn', context=portal) |
|---|
| 292 | right = getMultiAdapter((portal, rightColumn), IPortletAssignmentMapping, context=portal) |
|---|
| 293 | |
|---|
| 294 | if u'Course Builder Portlet' not in right: |
|---|
| 295 | right[u'Course Builder Portlet'] = portlet.coursebuilder.Assignment() |
|---|
| 296 | |
|---|
| 297 | # Turn off right hand portlets |
|---|
| 298 | if u'review' in right: |
|---|
| 299 | del right[u'review'] |
|---|
| 300 | |
|---|
| 301 | if u'news' in right: |
|---|
| 302 | del right[u'news'] |
|---|
| 303 | |
|---|
| 304 | if u'events' in right: |
|---|
| 305 | del right[u'events'] |
|---|
| 306 | |
|---|
| 307 | if u'calendar' in right: |
|---|
| 308 | del right[u'calendar'] |
|---|
| 309 | |
|---|
| 310 | def defaultSettings(portal): |
|---|
| 311 | """ Miscellaneous settings """ |
|---|
| 312 | |
|---|
| 313 | # Change allow_content_types for Site |
|---|
| 314 | pt = portal.portal_types |
|---|
| 315 | site = getattr(pt, 'Plone Site') |
|---|
| 316 | site.filter_content_types = 1 |
|---|
| 317 | site.allowed_content_types = ('Topic', 'Division', 'Folder', 'Document', 'Link', 'File', 'Image', 'CoursesTopic', 'Feedback') |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | # Give the MailHost a Title |
|---|
| 321 | mailhost = getattr(site, 'MailHost') |
|---|
| 322 | mailhost.title = 'Plone Mail Host' |
|---|
| 323 | |
|---|
| 324 | # Change allow_content_types for Folder |
|---|
| 325 | # Modifying it in setuphandlers allows us to maintain one less content type |
|---|
| 326 | folder = getattr(pt, 'Folder') |
|---|
| 327 | folder.filter_content_types = 1 |
|---|
| 328 | folder.allowed_content_types = ('FSSFile','Folder', 'Document', 'Link', 'File', 'Image') |
|---|
| 329 | |
|---|
| 330 | # Update default types for Wiki Behaviour |
|---|
| 331 | import plone |
|---|
| 332 | wicked_type_regs = {'Page': plone.app.controlpanel.markup.wicked_type_regs['Page']} |
|---|
| 333 | plone.app.controlpanel.markup.wicked_type_regs = wicked_type_regs |
|---|
| 334 | |
|---|
| 335 | portal.portal_properties.educommons_properties.division_descriptor = _(u'Departments') |
|---|
| 336 | |
|---|
| 337 | #Update Sitewide Default Syndication Properties |
|---|
| 338 | portal.portal_syndication.max_items = 999 |
|---|
| 339 | |
|---|
| 340 | def customizeAddOnProducts(portal): |
|---|
| 341 | """ Customizations to dependent products """ |
|---|
| 342 | |
|---|
| 343 | #Move PloneBookmarklets document actions to bottom of the list |
|---|
| 344 | dactions = portal.portal_actions.document_actions |
|---|
| 345 | if 'bookmarklets' in dactions: |
|---|
| 346 | dactions.moveObjectsToBottom(('bookmarklets',)) |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | def copyrightClearedOnObj_value(object, portal, **kwargs): |
|---|
| 350 | try: |
|---|
| 351 | copyright = IAnnotations(object) |
|---|
| 352 | return copyright['eduCommons.clearcopyright'] |
|---|
| 353 | except (ComponentLookupError, TypeError, ValueError, KeyError): |
|---|
| 354 | # The catalog expects AttributeErrors when a value can't be found |
|---|
| 355 | raise AttributeError |
|---|
| 356 | |
|---|
| 357 | registerIndexableAttribute('copyrightClearedOnObj', copyrightClearedOnObj_value) |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | def addCopyrightIndexToCatalog(portal, |
|---|
| 361 | indexes=('copyrightClearedOnObj',), |
|---|
| 362 | catalog='portal_catalog'): |
|---|
| 363 | """Adds the specified indices as FieldIndexes to the catalog specified, |
|---|
| 364 | which must inherit from CMFPlone.CatalogTool.CatalogTool, or otherwise |
|---|
| 365 | use the Plone ExtensibleIndexableObjectWrapper.""" |
|---|
| 366 | cat = getToolByName(portal, catalog, None) |
|---|
| 367 | reindex = [] |
|---|
| 368 | if cat is not None: |
|---|
| 369 | for index in indexes: |
|---|
| 370 | if index in cat.indexes(): |
|---|
| 371 | continue |
|---|
| 372 | ProxyIndex.manage_addProxyIndex(portal.portal_catalog, |
|---|
| 373 | index, |
|---|
| 374 | idx_type='FieldIndex', |
|---|
| 375 | value_expr='object/search_view/copyrightClearedOnObj') |
|---|
| 376 | reindex.append(index) |
|---|
| 377 | if reindex: |
|---|
| 378 | cat.manage_reindexIndex(reindex) |
|---|
| 379 | |
|---|
| 380 | def addCopyrightMetadataToCatalog(portal, |
|---|
| 381 | metadata=('copyrightClearedOnObj',), |
|---|
| 382 | catalog='portal_catalog'): |
|---|
| 383 | """Adds the specified columns to the catalog specified, |
|---|
| 384 | which must inherit from CMFPlone.CatalogTool.CatalogTool, or otherwise |
|---|
| 385 | use the Plone ExtensibleIndexableObjectWrapper.""" |
|---|
| 386 | cat = getToolByName(portal, catalog, None) |
|---|
| 387 | reindex = [] |
|---|
| 388 | if cat is not None: |
|---|
| 389 | for column in metadata: |
|---|
| 390 | if column in cat.schema(): |
|---|
| 391 | continue |
|---|
| 392 | cat.addColumn(column) |
|---|
| 393 | reindex.append(column) |
|---|
| 394 | if reindex: |
|---|
| 395 | cat.refreshCatalog() |
|---|
| 396 | |
|---|
| 397 | def addAccessibilityIndexToCatalog(portal, |
|---|
| 398 | indexes=('AccessibilitySetOnObj',), |
|---|
| 399 | catalog='portal_catalog'): |
|---|
| 400 | """Adds the specified indices as FieldIndexes to the catalog specified, |
|---|
| 401 | which must inherit from CMFPlone.CatalogTool.CatalogTool, or otherwise |
|---|
| 402 | use the Plone ExtensibleIndexableObjectWrapper.""" |
|---|
| 403 | cat = getToolByName(portal, catalog, None) |
|---|
| 404 | reindex = [] |
|---|
| 405 | if cat is not None: |
|---|
| 406 | for index in indexes: |
|---|
| 407 | if index in cat.indexes(): |
|---|
| 408 | continue |
|---|
| 409 | ProxyIndex.manage_addProxyIndex(portal.portal_catalog, |
|---|
| 410 | index, |
|---|
| 411 | idx_type='FieldIndex', |
|---|
| 412 | value_expr='object/search_view/AccessibilitySetOnObj') |
|---|
| 413 | reindex.append(index) |
|---|
| 414 | if reindex: |
|---|
| 415 | cat.manage_reindexIndex(reindex) |
|---|
| 416 | |
|---|
| 417 | def addAccessibilityMetadataToCatalog(portal, |
|---|
| 418 | metadata=('AccessibilitySetOnObj',), |
|---|
| 419 | catalog='portal_catalog'): |
|---|
| 420 | """Adds the specified columns to the catalog specified, |
|---|
| 421 | which must inherit from CMFPlone.CatalogTool.CatalogTool, or otherwise |
|---|
| 422 | use the Plone ExtensibleIndexableObjectWrapper.""" |
|---|
| 423 | cat = getToolByName(portal, catalog, None) |
|---|
| 424 | reindex = [] |
|---|
| 425 | if cat is not None: |
|---|
| 426 | for column in metadata: |
|---|
| 427 | if column in cat.schema(): |
|---|
| 428 | continue |
|---|
| 429 | cat.addColumn(column) |
|---|
| 430 | reindex.append(column) |
|---|
| 431 | if reindex: |
|---|
| 432 | cat.refreshCatalog() |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | def getObjPositionInCourse_value(object, portal, **kwargs): |
|---|
| 436 | try: |
|---|
| 437 | copyright = IAnnotations(object) |
|---|
| 438 | return copyright['eduCommons.objPositionInCourse'] |
|---|
| 439 | except (ComponentLookupError, TypeError, ValueError, KeyError): |
|---|
| 440 | # The catalog expects AttributeErrors when a value can't be found |
|---|
| 441 | raise AttributeError |
|---|
| 442 | |
|---|
| 443 | registerIndexableAttribute('getObjPositionInCourse', getObjPositionInCourse_value) |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | def addObjPositionIndexToCatalog(portal, |
|---|
| 447 | indexes=('getObjPositionInCourse',), |
|---|
| 448 | catalog='portal_catalog'): |
|---|
| 449 | """Adds the specified indices as FieldIndexes to the catalog specified, |
|---|
| 450 | which must inherit from CMFPlone.CatalogTool.CatalogTool, or otherwise |
|---|
| 451 | use the Plone ExtensibleIndexableObjectWrapper.""" |
|---|
| 452 | cat = getToolByName(portal, catalog, None) |
|---|
| 453 | reindex = [] |
|---|
| 454 | if cat is not None: |
|---|
| 455 | for index in indexes: |
|---|
| 456 | if index in cat.indexes(): |
|---|
| 457 | continue |
|---|
| 458 | ProxyIndex.manage_addProxyIndex(portal.portal_catalog, |
|---|
| 459 | index, |
|---|
| 460 | idx_type='FieldIndex', |
|---|
| 461 | value_expr='object/search_view/getObjPositionInCourse') |
|---|
| 462 | |
|---|
| 463 | reindex.append(index) |
|---|
| 464 | if reindex: |
|---|
| 465 | cat.manage_reindexIndex(reindex) |
|---|
| 466 | |
|---|
| 467 | def addObjPositionMetadataToCatalog(portal, |
|---|
| 468 | metadata=('getObjPositionInCourse',), |
|---|
| 469 | catalog='portal_catalog'): |
|---|
| 470 | """Adds the specified columns to the catalog specified, |
|---|
| 471 | which must inherit from CMFPlone.CatalogTool.CatalogTool, or otherwise |
|---|
| 472 | use the Plone ExtensibleIndexableObjectWrapper.""" |
|---|
| 473 | cat = getToolByName(portal, catalog, None) |
|---|
| 474 | reindex = [] |
|---|
| 475 | if cat is not None: |
|---|
| 476 | for column in metadata: |
|---|
| 477 | if column in cat.schema(): |
|---|
| 478 | continue |
|---|
| 479 | cat.addColumn(column) |
|---|
| 480 | reindex.append(column) |
|---|
| 481 | if reindex: |
|---|
| 482 | cat.refreshCatalog() |
|---|
| 483 | |
|---|
| 484 | |
|---|