| 1 | ################################################################################## |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2004-2006 Utah State University, All rights reserved. |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | # |
|---|
| 19 | ################################################################################## |
|---|
| 20 | |
|---|
| 21 | __author__ = '''Brent Lambert, David Ray, Jon Thomas''' |
|---|
| 22 | __docformat__ = 'plaintext' |
|---|
| 23 | __version__ = "$Revision: 3026 $"[11:-2] |
|---|
| 24 | |
|---|
| 25 | # eventHandlers.py |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | from enpraxis.educommons.interfaces import ICourse, IDivision |
|---|
| 29 | from collective.imstransport.ManifestHandlers import LOM_namespace |
|---|
| 30 | from collective.imstransport.IMS_exceptions import ManifestError |
|---|
| 31 | from Products.CMFDefault.SyndicationTool import SyndicationTool |
|---|
| 32 | from Products.CMFDefault.SyndicationInfo import SyndicationInformation |
|---|
| 33 | from collective.contentlicensing.utilities.interfaces import IContentLicensingUtility |
|---|
| 34 | from collective.zipfiletransport.utilities.interfaces import IZipFileTransportUtility |
|---|
| 35 | from zope.annotation.interfaces import IAnnotations |
|---|
| 36 | from zope.app.container.interfaces import IContainerModifiedEvent |
|---|
| 37 | |
|---|
| 38 | from zope.schema.interfaces import IVocabularyFactory |
|---|
| 39 | from zope.formlib.form import action |
|---|
| 40 | from collective.imstransport.browser.imstransportform import ImportForm |
|---|
| 41 | from utilities.interfaces import IECUtility |
|---|
| 42 | from zope.component import getUtility, queryUtility |
|---|
| 43 | import transaction |
|---|
| 44 | from xml.dom import minidom |
|---|
| 45 | import mimetypes |
|---|
| 46 | import re |
|---|
| 47 | from App.config import getConfiguration |
|---|
| 48 | import os |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | RE_BODY = re.compile('<body[^>]*?>(.*)</body>', re.DOTALL ) |
|---|
| 52 | |
|---|
| 53 | eduCommons_version = 'eduCommonsv1.2' |
|---|
| 54 | ec_namespace = 'http://cosl.usu.edu/xsd/eduCommonsv1.2' |
|---|
| 55 | |
|---|
| 56 | def setNameSpaces(event): |
|---|
| 57 | """ Set up namespaces in manifest file """ |
|---|
| 58 | event.writer.addNamespace(('xmlns:eduCommons', 'http://cosl.usu.edu/xsd/eduCommonsv1.2'), |
|---|
| 59 | 'http://cosl.usu.edu/xsd/eduCommonsv1.2 eduCommonsv1.2.xsd', |
|---|
| 60 | ('enpraxis.educommons/enpraxis/educommons', 'eduCommonsv1.2.xsd')) |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | def writeECMetadata(event): |
|---|
| 65 | """ Handle write IMS metadata event, and add custom eduCommons metadata """ |
|---|
| 66 | |
|---|
| 67 | event.writer.addNamespace(('xmlns:eduCommons', 'http://cosl.usu.edu/xsd/eduCommonsv1.2'), |
|---|
| 68 | ('http://cosl.usu.edu/xsd/eduCommonsv1.2 eduCommonsv1.2.xsd')) |
|---|
| 69 | eduCommons_node = event.writer._createNode(event.node, |
|---|
| 70 | ec_namespace, |
|---|
| 71 | 'eduCommons', |
|---|
| 72 | attrs=[('xmlns', ec_namespace)]) |
|---|
| 73 | |
|---|
| 74 | objType = event.object.Type() |
|---|
| 75 | if 'Page' == objType: |
|---|
| 76 | objType = 'Document' |
|---|
| 77 | if 'Link' == objType: |
|---|
| 78 | objType = 'Link' |
|---|
| 79 | event.writer._createNode(eduCommons_node, ec_namespace, 'objectType', objType) |
|---|
| 80 | copyright = event.object.Rights() |
|---|
| 81 | if copyright: |
|---|
| 82 | event.writer._createNode(eduCommons_node, ec_namespace, 'copyright', copyright) |
|---|
| 83 | |
|---|
| 84 | cltool = getUtility(IContentLicensingUtility) |
|---|
| 85 | linfo = cltool.getLicenseAndHolderFromObject(event.object) |
|---|
| 86 | if linfo and linfo[1]: |
|---|
| 87 | license = linfo[1] |
|---|
| 88 | license_node = event.writer._createNode(eduCommons_node, |
|---|
| 89 | ec_namespace, |
|---|
| 90 | 'license', |
|---|
| 91 | attrs=[('category', license[0])]) |
|---|
| 92 | if license[1] and license[1] != 'None': |
|---|
| 93 | event.writer._createNode(license_node, ec_namespace, 'licenseName', license[1]) |
|---|
| 94 | if license[2] and license[2] != 'None': |
|---|
| 95 | event.writer._createNode(license_node, ec_namespace, 'licenseUrl', license[2]) |
|---|
| 96 | if license[3] and license[3] != 'None': |
|---|
| 97 | event.writer._createNode(license_node, ec_namespace, 'licenseIconUrl', license[3]) |
|---|
| 98 | |
|---|
| 99 | if IAnnotations(event.object).has_key('eduCommons.clearcopyright'): |
|---|
| 100 | if IAnnotations(event.object)['eduCommons.clearcopyright']: |
|---|
| 101 | event.writer._createNode(eduCommons_node, ec_namespace, 'clearedCopyright', 'true') |
|---|
| 102 | else: |
|---|
| 103 | event.writer._createNode(eduCommons_node, ec_namespace, 'clearedCopyright', 'false') |
|---|
| 104 | else: |
|---|
| 105 | event.writer._createNode(eduCommons_node, ec_namespace, 'clearedCopyright', 'false') |
|---|
| 106 | |
|---|
| 107 | #access = IAnnotations(event.object)['eduCommons.accessible'] |
|---|
| 108 | #if access: |
|---|
| 109 | # event.writer._createNode(eduCommons_node, ec_namespace, 'accessible', 'true') |
|---|
| 110 | #else: |
|---|
| 111 | # event.writer._createNode(eduCommons_node, ec_namespace, 'accessible', 'false') |
|---|
| 112 | |
|---|
| 113 | if 'Course' == event.object.Type(): |
|---|
| 114 | courseId = event.object.getCourseId() |
|---|
| 115 | if courseId: |
|---|
| 116 | event.writer._createNode(eduCommons_node, ec_namespace, 'courseId', courseId) |
|---|
| 117 | term = event.object.getTerm() |
|---|
| 118 | if term: |
|---|
| 119 | event.writer._createNode(eduCommons_node, ec_namespace, 'term', term) |
|---|
| 120 | if True == event.object.getDisplayInstEmail(): |
|---|
| 121 | event.writer._createNode(eduCommons_node, |
|---|
| 122 | ec_namespace, |
|---|
| 123 | 'displayInstEmail', |
|---|
| 124 | 'true') |
|---|
| 125 | else: |
|---|
| 126 | event.writer._createNode(eduCommons_node, |
|---|
| 127 | ec_namespace, |
|---|
| 128 | 'displayInstEmail', |
|---|
| 129 | 'false') |
|---|
| 130 | |
|---|
| 131 | if True == event.object.getInstructorAsCreator(): |
|---|
| 132 | event.writer._createNode(eduCommons_node, |
|---|
| 133 | ec_namespace, |
|---|
| 134 | 'instructorAsCreator', |
|---|
| 135 | 'true') |
|---|
| 136 | else: |
|---|
| 137 | event.writer._createNode(eduCommons_node, |
|---|
| 138 | ec_namespace, |
|---|
| 139 | 'instructorAsCreator', |
|---|
| 140 | 'false') |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | def readECMetadata(event): |
|---|
| 144 | """ Handle read IMS metadata event, and add custom eduCommons metadata """ |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | if event.node.nodeName in ['eduCommons', 'eduCommons:eduCommons']: |
|---|
| 148 | |
|---|
| 149 | event.mdSections.append(eduCommons_version) |
|---|
| 150 | |
|---|
| 151 | objectType_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'objectType') |
|---|
| 152 | if objectType_nodes: |
|---|
| 153 | ot = event.reader.getTextValue(objectType_nodes[0]) |
|---|
| 154 | if ot: |
|---|
| 155 | if ot not in ['Course', 'FSSFile', 'Document', 'File', 'Image', 'Link']: |
|---|
| 156 | raise ManifestError, '"%s" is not a recognized object type.' %ot |
|---|
| 157 | event.metadata['Type'] = '%s' %ot |
|---|
| 158 | |
|---|
| 159 | copyright_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'copyright') |
|---|
| 160 | if copyright_nodes: |
|---|
| 161 | cn = event.reader.getTextValue(copyright_nodes[0]) |
|---|
| 162 | if cn: |
|---|
| 163 | event.metadata['rights'] = cn |
|---|
| 164 | |
|---|
| 165 | license_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'license') |
|---|
| 166 | if license_nodes: |
|---|
| 167 | license_node = license_nodes[0] |
|---|
| 168 | license = [str(license_node.getAttribute('category'))] |
|---|
| 169 | licenseName_nodes = license_node.getElementsByTagNameNS(ec_namespace, 'licenseName') |
|---|
| 170 | if licenseName_nodes: |
|---|
| 171 | license.append(str(event.reader.getTextValue(licenseName_nodes[0]))) |
|---|
| 172 | else: |
|---|
| 173 | license.append('None') |
|---|
| 174 | licenseUrl_nodes = license_node.getElementsByTagNameNS(ec_namespace, 'licenseUrl') |
|---|
| 175 | if licenseUrl_nodes: |
|---|
| 176 | license.append(str(event.reader.getTextValue(licenseUrl_nodes[0]))) |
|---|
| 177 | else: |
|---|
| 178 | license.append('None') |
|---|
| 179 | licenseIconUrl_nodes = license_node.getElementsByTagNameNS(ec_namespace, 'licenseIconUrl') |
|---|
| 180 | if licenseIconUrl_nodes: |
|---|
| 181 | license.append(str(event.reader.getTextValue(licenseIconUrl_nodes[0]))) |
|---|
| 182 | else: |
|---|
| 183 | license.append('None') |
|---|
| 184 | event.metadata['license'] = license |
|---|
| 185 | |
|---|
| 186 | clearedCopyright_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'clearedCopyright') |
|---|
| 187 | if clearedCopyright_nodes: |
|---|
| 188 | cc = event.reader.getTextValue(clearedCopyright_nodes[0]) |
|---|
| 189 | if 'true' == cc: |
|---|
| 190 | event.metadata['clearedCopyright'] = True |
|---|
| 191 | else: |
|---|
| 192 | event.metadata['clearedCopyright'] = False |
|---|
| 193 | |
|---|
| 194 | accessibility_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'accessible') |
|---|
| 195 | if accessibility_nodes: |
|---|
| 196 | access = event.reader.getTextValue(accessibility_nodes[0]) |
|---|
| 197 | if 'true' == access: |
|---|
| 198 | event.metadata['accessible'] = True |
|---|
| 199 | else: |
|---|
| 200 | event.metadata['accessible'] = False |
|---|
| 201 | |
|---|
| 202 | courseId_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'courseId') |
|---|
| 203 | if courseId_nodes: |
|---|
| 204 | cin = event.reader.getTextValue(courseId_nodes[0]) |
|---|
| 205 | if cin: |
|---|
| 206 | event.metadata['courseId'] = cin |
|---|
| 207 | |
|---|
| 208 | term_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'term') |
|---|
| 209 | if term_nodes: |
|---|
| 210 | tm = event.reader.getTextValue(term_nodes[0]) |
|---|
| 211 | if tm: |
|---|
| 212 | event.metadata['term'] = tm |
|---|
| 213 | |
|---|
| 214 | displayInsEmail_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'displayInstEmail') |
|---|
| 215 | if displayInsEmail_nodes: |
|---|
| 216 | die = event.reader.getTextValue(displayInsEmail_nodes[0]) |
|---|
| 217 | if 'true' == die: |
|---|
| 218 | event.metadata['displayInstEmail'] = True |
|---|
| 219 | else: |
|---|
| 220 | event.metadata['displayInstEmail'] = False |
|---|
| 221 | |
|---|
| 222 | instrIsPrincipal_nodes = event.node.getElementsByTagNameNS(ec_namespace, 'instructorAsCreator') |
|---|
| 223 | if instrIsPrincipal_nodes: |
|---|
| 224 | iis = event.reader.getTextValue(instrIsPrincipal_nodes[0]) |
|---|
| 225 | if 'true' == iis: |
|---|
| 226 | event.metadata['instructorAsCreator'] = True |
|---|
| 227 | else: |
|---|
| 228 | event.metadata['instructorAsCreator'] = False |
|---|
| 229 | |
|---|
| 230 | excludeFromNav_nodes = event.node.getElementsByTagNameNS(ec_namespace,'excludeFromNav') |
|---|
| 231 | if excludeFromNav_nodes: |
|---|
| 232 | efn = event.reader.getTextValue(excludeFromNav_nodes[0]) |
|---|
| 233 | if 'true' == efn: |
|---|
| 234 | event.metadata['excludeFromNav'] = True |
|---|
| 235 | else: |
|---|
| 236 | event.metadata['excludeFromNav'] = False |
|---|
| 237 | |
|---|
| 238 | homePagePath_nodes = event.node.getElementsByTagNameNS(ec_namespace,'homePagePath') |
|---|
| 239 | if homePagePath_nodes: |
|---|
| 240 | event.metadata['homePagePath'] = event.reader.getTextValue(homePagePath_nodes[0]) |
|---|
| 241 | |
|---|
| 242 | node = event.node |
|---|
| 243 | manNode = None |
|---|
| 244 | while node.parentNode: |
|---|
| 245 | manNode = node |
|---|
| 246 | node = node.parentNode |
|---|
| 247 | |
|---|
| 248 | cwsp = manNode.getAttribute("xmlns:cwsp") |
|---|
| 249 | if cwsp == "http://www.dspace.org/xmlns/cwspace_imscp": |
|---|
| 250 | event.metadata['license'] = ["MIT OCW License","MIT OCW License","http://ocw.mit.edu/OcwWeb/Global/terms-of-use.htm","http://ocw.mit.edu/NR/rdonlyres/B2A8B934-D6FD-4481-A702-3C9C6E56A355/0/cc_button.jpg"] |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | def writeContributeNode(event): |
|---|
| 254 | """ Handle a write contributor node event. """ |
|---|
| 255 | |
|---|
| 256 | cltool = getUtility(IContentLicensingUtility) |
|---|
| 257 | linfo = cltool.getLicenseAndHolderFromObject(event.object) |
|---|
| 258 | if linfo and linfo[0]: |
|---|
| 259 | event.mwriter.createContributeElement(event.writer, |
|---|
| 260 | LOM_namespace, |
|---|
| 261 | event.node, |
|---|
| 262 | 'eduCommonsv1.2', |
|---|
| 263 | 'rights holder', |
|---|
| 264 | linfo[0], |
|---|
| 265 | event.object.ModificationDate()) |
|---|
| 266 | |
|---|
| 267 | if hasattr(event.object.aq_explicit, 'getInstructorName'): |
|---|
| 268 | instructorName = event.object.getInstructorName() |
|---|
| 269 | else: |
|---|
| 270 | instructorName = None |
|---|
| 271 | |
|---|
| 272 | if hasattr(event.object.aq_explicit, 'getInstructorEmail'): |
|---|
| 273 | instructorEmail = event.object.getInstructorEmail() |
|---|
| 274 | else: |
|---|
| 275 | instructorEmail = None |
|---|
| 276 | |
|---|
| 277 | if instructorName: |
|---|
| 278 | event.mwriter.createContributeElement(event.writer, |
|---|
| 279 | LOM_namespace, |
|---|
| 280 | event.node, |
|---|
| 281 | 'eduCommonsv1.2', |
|---|
| 282 | 'Instructor', |
|---|
| 283 | instructorName, |
|---|
| 284 | event.object.ModificationDate(), |
|---|
| 285 | instructorEmail) |
|---|
| 286 | |
|---|
| 287 | def readContributeNode(event): |
|---|
| 288 | """ Handle a read contributor node event. """ |
|---|
| 289 | |
|---|
| 290 | if eduCommons_version == event.source: |
|---|
| 291 | if 'Instructor' == event.value and event.vlist: |
|---|
| 292 | instructorName, instructorEmail = event.vlist[0] |
|---|
| 293 | if instructorName: |
|---|
| 294 | event.metadata['instructorName'] = instructorName |
|---|
| 295 | if instructorEmail: |
|---|
| 296 | event.metadata['instructorEmail'] = instructorEmail |
|---|
| 297 | if 'rights holder' == event.value and event.vlist: |
|---|
| 298 | holderName, holderEmail = event.vlist[0] |
|---|
| 299 | if holderName: |
|---|
| 300 | event.metadata['rightsHolder'] = holderName |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | def writeOrganizations(event): |
|---|
| 304 | """ Handle write organizations event. """ |
|---|
| 305 | |
|---|
| 306 | objects = [obj.getObject() for obj in \ |
|---|
| 307 | event.object.portal_catalog.searchResults( |
|---|
| 308 | path={'query':'/'.join(event.object.getPhysicalPath())+'/',})] |
|---|
| 309 | |
|---|
| 310 | orgId = event.writer._createPathId(event.object.virtual_url_path(), 'ORG') |
|---|
| 311 | org_node = event.writer._createNode(event.node, |
|---|
| 312 | '', |
|---|
| 313 | 'organization', |
|---|
| 314 | attrs=[('identifier', orgId)]) |
|---|
| 315 | |
|---|
| 316 | for obj in objects: |
|---|
| 317 | if not obj.getExcludeFromNav() and 'Course' != obj.portal_type: |
|---|
| 318 | itemId = event.writer._createPathId(obj.virtual_url_path(), 'ITM') |
|---|
| 319 | pathId = event.writer._createPathId(obj.virtual_url_path(), 'RES') |
|---|
| 320 | item_node = event.writer._createNode(org_node, |
|---|
| 321 | '', |
|---|
| 322 | 'item', |
|---|
| 323 | attrs=[('identifier', itemId), |
|---|
| 324 | ('identifierref', pathId), |
|---|
| 325 | ('isvisible', 'true'),]) |
|---|
| 326 | event.writer._createNode(item_node, '', 'title', obj.title) |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | def readOrganizations(event): |
|---|
| 330 | """ Handle read organizations event. """ |
|---|
| 331 | |
|---|
| 332 | default = event.node.getAttribute('default') |
|---|
| 333 | organization_nodes = event.node.getElementsByTagName('organization') |
|---|
| 334 | if organization_nodes: |
|---|
| 335 | if default: |
|---|
| 336 | for org_node in organization_nodes: |
|---|
| 337 | if org_node.getAttribute('identifier') == default: |
|---|
| 338 | organization_node = org_node |
|---|
| 339 | break |
|---|
| 340 | else: |
|---|
| 341 | organization_node = organization_nodes[0] |
|---|
| 342 | |
|---|
| 343 | item_nodes = organization_nodes[0].getElementsByTagName('item') |
|---|
| 344 | itemnum = 1 |
|---|
| 345 | for item in item_nodes: |
|---|
| 346 | if 'true' == item.getAttribute('isvisible'): |
|---|
| 347 | idref = item.getAttribute('identifierref') |
|---|
| 348 | event.org[idref] = itemnum |
|---|
| 349 | itemnum += 1 |
|---|
| 350 | |
|---|
| 351 | node = event.node |
|---|
| 352 | manNode = None |
|---|
| 353 | while node.parentNode: |
|---|
| 354 | manNode = node |
|---|
| 355 | node = node.parentNode |
|---|
| 356 | |
|---|
| 357 | cwsp = manNode.getAttribute("xmlns:cwsp") |
|---|
| 358 | if cwsp == "http://www.dspace.org/xmlns/cwspace_imscp": |
|---|
| 359 | obj = event.object |
|---|
| 360 | clutil = getUtility(IContentLicensingUtility) |
|---|
| 361 | licprops = obj.portal_properties.content_licensing_properties |
|---|
| 362 | |
|---|
| 363 | if "license_mit" not in clutil.getAvailableLicenses(obj): |
|---|
| 364 | license_mit = ["MIT OCW License","MIT OCW License","http://ocw.mit.edu/OcwWeb/Global/terms-of-use.htm","http://ocw.mit.edu/NR/rdonlyres/B2A8B934-D6FD-4481-A702-3C9C6E56A355/0/cc_button.jpg"] |
|---|
| 365 | licprops.manage_addProperty("license_mit", license_mit, 'lines') |
|---|
| 366 | licprops.manage_changeProperties(SupportedLicenses=list(licprops.SupportedLicenses)+["license_mit"]) |
|---|
| 367 | licprops.manage_changeProperties(AvailableLicenses=list(licprops.AvailableLicenses)+["license_mit"]) |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | class eduCommonsCreator: |
|---|
| 372 | """ Set the creator to an eduCommons specific creator function. """ |
|---|
| 373 | |
|---|
| 374 | def __call__(self, event): |
|---|
| 375 | if 'eduCommons' == event.rtype: |
|---|
| 376 | self.createObject(event.object, event.resource, event.data, event.metadata) |
|---|
| 377 | |
|---|
| 378 | def createObject(self, object, filepath, data, metadata): |
|---|
| 379 | """ Create an object with the given parameters. """ |
|---|
| 380 | # Get info |
|---|
| 381 | id = filepath.split('/')[-1] |
|---|
| 382 | |
|---|
| 383 | res_mimetype = mimetypes.guess_type(id)[0] |
|---|
| 384 | |
|---|
| 385 | if not metadata.has_key('Type'): |
|---|
| 386 | if res_mimetype in ['text/html', 'text/htm' 'text/plain' 'text/x-rst', 'text/structured']: |
|---|
| 387 | metadata['Type'] = 'Document' |
|---|
| 388 | elif res_mimetype == None: |
|---|
| 389 | metadata['Type'] = 'File' |
|---|
| 390 | elif re.match('^image', res_mimetype): |
|---|
| 391 | metadata['Type'] = 'Image' |
|---|
| 392 | else: |
|---|
| 393 | metadata['Type'] = 'File' |
|---|
| 394 | |
|---|
| 395 | objtype = metadata['Type'] |
|---|
| 396 | |
|---|
| 397 | # Must check against type for IMS packages that embed the Course Home Page within folder(s) |
|---|
| 398 | # If the check is not in place, eduCommons will try to create a Course object within a Course |
|---|
| 399 | if objtype == 'Course': |
|---|
| 400 | parent = object |
|---|
| 401 | else: |
|---|
| 402 | # Get the ZODB path, create folders if they do not exist |
|---|
| 403 | pathlist = filepath.split('/') |
|---|
| 404 | #pathlist = pathlist[1:] |
|---|
| 405 | parent = object |
|---|
| 406 | for p in pathlist[:-1]: |
|---|
| 407 | newparent = getattr(parent.aq_explicit, p, None) |
|---|
| 408 | if newparent: |
|---|
| 409 | parent = newparent |
|---|
| 410 | else: |
|---|
| 411 | parent = self.createFolder(parent, p) |
|---|
| 412 | parent.title = parent.id |
|---|
| 413 | |
|---|
| 414 | # Get the new object if it exists, otherwise create it |
|---|
| 415 | newobj = None |
|---|
| 416 | if parent == object and object.portal_type == objtype: |
|---|
| 417 | newobj = object |
|---|
| 418 | elif hasattr(parent.aq_explicit, id): |
|---|
| 419 | # The parent node has the object in it |
|---|
| 420 | childobj = getattr(parent, id) |
|---|
| 421 | # Check to see if the child node is the same type |
|---|
| 422 | # as the new object, as we are going to be rewriting |
|---|
| 423 | # all of its values. |
|---|
| 424 | if childobj.portal_type == objtype: |
|---|
| 425 | newobj = childobj |
|---|
| 426 | else: |
|---|
| 427 | # We need to delete this object, as it is not |
|---|
| 428 | # the same type as the one we are trying to |
|---|
| 429 | # upload. |
|---|
| 430 | parent.manage_delObjects([id]) |
|---|
| 431 | |
|---|
| 432 | # If we do not already have an object, create a new oney |
|---|
| 433 | if not newobj: |
|---|
| 434 | parent.invokeFactory(objtype, id) |
|---|
| 435 | newobj = getattr(parent, id) |
|---|
| 436 | newobj = object.portal_factory.doCreate(newobj, id) |
|---|
| 437 | |
|---|
| 438 | # Set the file data on the object |
|---|
| 439 | if objtype in ['Course', 'Document']: |
|---|
| 440 | data = self.stripHeader(data) |
|---|
| 441 | if metadata.has_key('Format'): |
|---|
| 442 | newobj.setText(data, mimetype=metadata['Format'], filename=id) |
|---|
| 443 | else: |
|---|
| 444 | mimetype = mimetypes.guess_type(newobj.absolute_url())[0] |
|---|
| 445 | newobj.setText(data, mimetype=mimetype, filename=id) |
|---|
| 446 | elif objtype in ['File', 'FSSFile']: |
|---|
| 447 | newobj.setFile(data) |
|---|
| 448 | newobj.setFormat(res_mimetype) |
|---|
| 449 | elif 'Image' == objtype: |
|---|
| 450 | newobj.setImage(data) |
|---|
| 451 | newobj.setFormat(res_mimetype) |
|---|
| 452 | elif 'Link' == objtype: |
|---|
| 453 | doc = minidom.parseString(data) |
|---|
| 454 | anchor_nodes = doc.getElementsByTagName('a') |
|---|
| 455 | if anchor_nodes: |
|---|
| 456 | newobj.setRemoteUrl(str(anchor_nodes[0].getAttribute('href'))) |
|---|
| 457 | |
|---|
| 458 | # Set the metadata on the object |
|---|
| 459 | for key in metadata.keys(): |
|---|
| 460 | field = newobj.getField(key) |
|---|
| 461 | if field: |
|---|
| 462 | mutator = field.getMutator(newobj) |
|---|
| 463 | if mutator: |
|---|
| 464 | field = metadata[key] |
|---|
| 465 | mutator(field) |
|---|
| 466 | |
|---|
| 467 | if metadata.has_key('accessible'): |
|---|
| 468 | IAnnotations(newobj)['eduCommons.accessible'] = metadata['accessible'] |
|---|
| 469 | else: |
|---|
| 470 | IAnnotations(newobj)['eduCommons.accessible'] = False |
|---|
| 471 | |
|---|
| 472 | if objtype == 'Course': |
|---|
| 473 | newobj.setExcludeFromNav('False') |
|---|
| 474 | |
|---|
| 475 | if metadata.has_key('rightsHolder'): |
|---|
| 476 | getUtility(IContentLicensingUtility).setRightsHolder(newobj, metadata['rightsHolder']) |
|---|
| 477 | |
|---|
| 478 | if metadata.has_key('license'): |
|---|
| 479 | getUtility(IContentLicensingUtility).setRightsLicense(newobj, metadata['license']) |
|---|
| 480 | |
|---|
| 481 | if metadata.has_key('homePagePath'): |
|---|
| 482 | course = getUtility(IECUtility).FindECParent(newobj) |
|---|
| 483 | if course.portal_type == 'Course': |
|---|
| 484 | if course.hasProperty('homePagePath'): |
|---|
| 485 | course.manage_changeProperties(homePagePath=metadata['homePagePath']) |
|---|
| 486 | else: |
|---|
| 487 | course.manage_addProperty('homePagePath',metadata['homePagePath'],'string') |
|---|
| 488 | |
|---|
| 489 | # Reindex the object so that the new stuff appears |
|---|
| 490 | newobj.reindexObject() |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | def createFolder(self, parent, id): |
|---|
| 494 | """ Create an object """ |
|---|
| 495 | |
|---|
| 496 | #hack to avoid IMS import failures from MIT package |
|---|
| 497 | if 'search' == id: |
|---|
| 498 | id = '%s-1' %id |
|---|
| 499 | |
|---|
| 500 | parent.invokeFactory('Folder',id) |
|---|
| 501 | obj = getattr(parent, id) |
|---|
| 502 | obj.setExcludeFromNav(True) |
|---|
| 503 | obj = obj.portal_factory.doCreate(obj, id) |
|---|
| 504 | obj.reindexObject() |
|---|
| 505 | |
|---|
| 506 | return obj |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | def stripHeader(self, data): |
|---|
| 510 | """ Tidy up any html, if we can. """ |
|---|
| 511 | # get the body text |
|---|
| 512 | result = RE_BODY.search(data) |
|---|
| 513 | if result: |
|---|
| 514 | data = result.group(1) |
|---|
| 515 | return data |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | eduCommonsCreateObject = eduCommonsCreator() |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | def syndicateFolderishObject(object, event): |
|---|
| 522 | """ Enable RSS feed upon FolderishObject creation. """ |
|---|
| 523 | if not hasattr(object.aq_explicit, 'syndication_information'): |
|---|
| 524 | syInfo = SyndicationInformation() |
|---|
| 525 | object._setObject('syndication_information', syInfo) |
|---|
| 526 | portal = object.portal_url.getPortalObject() |
|---|
| 527 | portal_syn = portal.portal_syndication |
|---|
| 528 | syInfo = object._getOb('syndication_information') |
|---|
| 529 | syInfo.syUpdatePeriod = portal_syn.syUpdatePeriod |
|---|
| 530 | syInfo.syUpdateFrequency = portal_syn.syUpdateFrequency |
|---|
| 531 | syInfo.syUpdateBase = portal_syn.syUpdateBase |
|---|
| 532 | syInfo.max_items = portal_syn.max_items |
|---|
| 533 | syInfo.description = "Channel Description" |
|---|
| 534 | |
|---|
| 535 | def addObjPosition(object, event): |
|---|
| 536 | appendObjPosition(object) |
|---|
| 537 | |
|---|
| 538 | def appendObjPosition(object): |
|---|
| 539 | if not object.isTemporary(): |
|---|
| 540 | ecutil = queryUtility(IECUtility) |
|---|
| 541 | if ecutil: |
|---|
| 542 | parent = ecutil.FindECParent(object) |
|---|
| 543 | if parent.Type() == 'Course': |
|---|
| 544 | path = {'path':{'query':'/'.join(parent.getPhysicalPath())+'/'},} |
|---|
| 545 | brains = object.portal_catalog.searchResults(path) |
|---|
| 546 | if brains: |
|---|
| 547 | pos = [0,] |
|---|
| 548 | for brain in brains: |
|---|
| 549 | obj = brain.getObject() |
|---|
| 550 | annotations = IAnnotations(obj) |
|---|
| 551 | if annotations.has_key('eduCommons.objPositionInCourse'): |
|---|
| 552 | pos += [annotations['eduCommons.objPositionInCourse'],] |
|---|
| 553 | maxpos = max(pos) |
|---|
| 554 | if maxpos > 0: |
|---|
| 555 | maxpos += 1 |
|---|
| 556 | else: |
|---|
| 557 | maxpos = 1 |
|---|
| 558 | else: |
|---|
| 559 | maxpos = 1 |
|---|
| 560 | |
|---|
| 561 | annotations = IAnnotations(object) |
|---|
| 562 | annotations['eduCommons.objPositionInCourse'] = maxpos |
|---|
| 563 | |
|---|
| 564 | zipobj = getattr(parent, parent.id + '.zip', None) |
|---|
| 565 | if zipobj: |
|---|
| 566 | IAnnotations(zipobj)['eduCommons.objPositionInCourse'] = maxpos + 1 |
|---|
| 567 | zipobj.reindexObject() |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | class eduCommonsImportForm(ImportForm): |
|---|
| 572 | """ Render the import form """ |
|---|
| 573 | |
|---|
| 574 | @action('Upload') |
|---|
| 575 | def action_import(self, action, data): |
|---|
| 576 | |
|---|
| 577 | filename = self.context.REQUEST['form.filename'] |
|---|
| 578 | packagetype = self.context.REQUEST['form.packagetype'] |
|---|
| 579 | |
|---|
| 580 | imsvocab = getUtility(IVocabularyFactory, name='imsvocab')(self.context) |
|---|
| 581 | package_xform = imsvocab.getTermByToken(packagetype).value |
|---|
| 582 | |
|---|
| 583 | self.ims_util.importZipfile(self.context,filename,package_xform,rtype='eduCommons') |
|---|
| 584 | |
|---|
| 585 | self.request.response.redirect('.') |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | def updateZipDownload(object, event): |
|---|
| 590 | """ Check for factors related to editing and adding objects """ |
|---|
| 591 | pw = event.object.portal_workflow |
|---|
| 592 | |
|---|
| 593 | if pw.getInfoFor(event.object,'review_state') == 'Published': |
|---|
| 594 | validateContext(object, event) |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | def ZipFileMaker(event): |
|---|
| 599 | """ Handler for creating zip download for objects that are moving through workflow changes """ |
|---|
| 600 | |
|---|
| 601 | if event.bulkChange and event.target in ['manager_rework','retract']: |
|---|
| 602 | validateContext(event.object,event) |
|---|
| 603 | elif event.initial_state == 'Published' or event.target == 'publish': |
|---|
| 604 | validateContext(event.object,event) |
|---|
| 605 | else: |
|---|
| 606 | pass |
|---|
| 607 | |
|---|
| 608 | def deleteObjectHandler(event): |
|---|
| 609 | """ Handlet the delete object event """ |
|---|
| 610 | if event.bulkChange == True: |
|---|
| 611 | if event.contains_published: |
|---|
| 612 | validateContext(event.object, event) |
|---|
| 613 | else: |
|---|
| 614 | validateContext(event.object, event) |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | def validateContext(object, event): |
|---|
| 618 | """ create the Zipfile after some final checks """ |
|---|
| 619 | |
|---|
| 620 | parent = getUtility(IECUtility).FindECParent(object) |
|---|
| 621 | file_id = parent.id + '.zip' |
|---|
| 622 | pw = event.object.portal_workflow |
|---|
| 623 | |
|---|
| 624 | if parent.portal_type == 'Course': |
|---|
| 625 | if pw.getInfoFor(parent,'review_state') == 'Published': |
|---|
| 626 | if not event.object.isTemporary(): |
|---|
| 627 | if event.object.id != file_id: |
|---|
| 628 | if not IContainerModifiedEvent.providedBy(event): |
|---|
| 629 | ZipFileCreator(parent,event).createZipFile() |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | |
|---|
| 634 | ## Deprecated for 3.1.0, as auto generated Course packages have been disabled |
|---|
| 635 | ## Replaced by Package Course functionality :: browser/packagecourseview.py |
|---|
| 636 | class ZipFileCreator: |
|---|
| 637 | |
|---|
| 638 | def __init__(self, object, event): |
|---|
| 639 | self.obj = object |
|---|
| 640 | self.event = event |
|---|
| 641 | |
|---|
| 642 | def createZipFile(self): |
|---|
| 643 | """ Create a zip file for when the file is modified. """ |
|---|
| 644 | |
|---|
| 645 | course = self.obj |
|---|
| 646 | file_id = course.id + '.zip' |
|---|
| 647 | |
|---|
| 648 | pm = course.portal_membership |
|---|
| 649 | user_id = pm.getAuthenticatedMember().id |
|---|
| 650 | roles = pm.getAuthenticatedMember().getRoles() |
|---|
| 651 | if 'Publisher' in roles: |
|---|
| 652 | roles += ['Administrator'] |
|---|
| 653 | course.manage_setLocalRoles(user_id, roles) |
|---|
| 654 | course.reindexObjectSecurity() |
|---|
| 655 | |
|---|
| 656 | data = self.getZipFileData(course=course) |
|---|
| 657 | |
|---|
| 658 | if not data: |
|---|
| 659 | return |
|---|
| 660 | |
|---|
| 661 | if not hasattr(course,file_id): |
|---|
| 662 | |
|---|
| 663 | course.invokeFactory("File",file_id) |
|---|
| 664 | fileobj = getattr(course,file_id) |
|---|
| 665 | fileobj.content_status_modify(workflow_action='submit') |
|---|
| 666 | fileobj.content_status_modify(workflow_action='release') |
|---|
| 667 | fileobj.content_status_modify(workflow_action='publish') |
|---|
| 668 | fileobj.setTitle("Download This Course") |
|---|
| 669 | |
|---|
| 670 | else: |
|---|
| 671 | fileobj = getattr(course,file_id) |
|---|
| 672 | fileobj.setTitle("Download This Course") |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | fileobj.setExcludeFromNav(False) |
|---|
| 676 | fileobj.setFile(data) |
|---|
| 677 | appendObjPosition(fileobj) |
|---|
| 678 | |
|---|
| 679 | course.portal_catalog.reindexObject(fileobj) |
|---|
| 680 | user_ids = [] |
|---|
| 681 | user_ids += [user_id] |
|---|
| 682 | course.manage_delLocalRoles(userids=user_ids) |
|---|
| 683 | course.reindexObjectSecurity() |
|---|
| 684 | |
|---|
| 685 | def getZipFileData(self, course, obj_paths=[], filename=None): |
|---|
| 686 | """ |
|---|
| 687 | Return the content for a zip file |
|---|
| 688 | """ |
|---|
| 689 | objects_list = getUtility(IZipFileTransportUtility)._createObjectList(course, obj_paths, state=['Published']) |
|---|
| 690 | objects_list.insert(0,course) |
|---|
| 691 | context_path = str( course.virtual_url_path() ) |
|---|
| 692 | |
|---|
| 693 | # Do not include the zip file for the course |
|---|
| 694 | mod_objects_list = [object for object in objects_list if object.virtual_url_path().replace(course.virtual_url_path(),'') != '/' + course.id + '.zip'] |
|---|
| 695 | |
|---|
| 696 | if mod_objects_list: |
|---|
| 697 | content = getUtility(IZipFileTransportUtility)._getAllObjectsData(mod_objects_list, context_path) |
|---|
| 698 | return content |
|---|
| 699 | else: |
|---|
| 700 | return None |
|---|
| 701 | |
|---|