def migrate(context, annotations, oldnotes): # construct a set of old notes to verify whether or not to use # the CellML profile. oldnote_set = set(oldnotes) session_file = None if 'opencellsession' in oldnote_set: # It can die here because something wrong with getting the # vocabulary from the manifest. session_file = annotations['opencellsession'].filename # discard this. oldnote_set.remove('opencellsession') groups = {} groups['opencellsession'] = [('filename', session_file),] if cellml_force or (cellml_type_notes and oldnote_set == cellml_notes): context.file_type = cellml_type # update views context.views = cellml_type_notes context.setSubject(cellml_type_tags) groups['license_citation'] = [('format', u'cellml_rdf_metadata')] groups['source_text'] = [('langtype', u'xml')] else: # reuse the existing views, and unknown profile. pass # annotate using the form. form = ExposureFileTypeAnnotatorForm(context, None) # It can die here too because of various reasons (workspace # missing, malformed input data, incompatibilities of existing # data with new formatting scheme, stray electrons, etc.). form._annotate(groups)
def filetype_bulk_update(context): import traceback import transaction from pmr2.app.annotation.interfaces import IExposureFileNote from pmr2.app.exposure.browser.util import viewinfo try: from pmr2.app.exposure.browser.browser import \ ExposureFileTypeAnnotatorForm except ImportError: from pmr2.app.browser.exposure import ExposureFileTypeAnnotatorForm logger = getLogger('pmr2.app') catalog = getToolByName(context, 'portal_catalog') brains = catalog(portal_type='ExposureFileType') filetypes = dict([(b.getPath(), b.pmr2_eftype_views) for b in brains]) if not filetypes: logger.error('Please reindex or create some Exposure File Types ' 'before running this.') return errors = [] commit_interval = 100 files = catalog(portal_type='ExposureFile') t = len(files) for c, b in enumerate(files, 1): file_sp = transaction.savepoint() file = b.getObject() logger.info('Rebuilding notes for `%s` (%d/%d).', file.absolute_url_path(), c, t) ftpath = file.file_type if not ftpath: continue if not ftpath in filetypes: # XXX only log this information for now, we may need to give # adminstrators some way to automatically migrate to a new # existing type since the old one was removed. logger.warning('`%s` has `%s` as its filetype but it no longer ' 'exists ' % (file.absolute_url_path(), ftpath)) continue cftviews = filetypes[ftpath] # file type views for current file try: # assign the new set of views # annotate using the form. file.views = cftviews # XXX does not set the new subjects, i.e. not applying tags # via setSubject method of this file object groups = {} for k, v in viewinfo(file): groups[k] = v and v.items() or None form = ExposureFileTypeAnnotatorForm(file, None) form._annotate(groups) file.reindexObject() except: file_sp.rollback() errors.append(file.absolute_url_path()) logger.error('Failed to rebuild `%s`' % file.absolute_url_path()) logger.warning(traceback.format_exc()) continue if c % commit_interval == 0: transaction.get().commit() logger.info('Committed transaction, interval %d reached' % commit_interval) if errors: logger.error('The following file(s) have failed to rebuild:\n%s' % '\n'.join(errors))