Ejemplo n.º 1
0
    def _export(self, cur, prefix=''):

        # we don't have or need leading / to denote root.
        if not prefix:
            objpath = lambda x: '%s' % x
        else:
            objpath = lambda x: '%s/%s' % (prefix, x)

        for obj_id, obj in cur.items():
            p = objpath(obj_id)
            # do stuff depend on type

            if IExposureFile.providedBy(obj):
                d = {}
                for n in IExposureFile.names():
                    d[n] = getattr(obj, n)
                # query each views manually.
                d['views'] = viewinfo(obj)
                d['hidden_views'] = obj.hidden_views
                # retain the subject.
                d['Subject'] = obj.Subject()
                # deprecate the docgen fields off the object
                d['docview_generator'] = None
                d['docview_gensource'] = None
                yield (p, d,)
            elif IExposureFolder.providedBy(obj):
                # we are ignoring other folder types, let an adapter
                # handle it below.
                for i in self._export(obj, p):
                    yield i
            else:
                # we don't know, so we ask an adapter to provide the
                # structure required, and trust they won't provide
                # something they don't own.
                a = zope.component.queryAdapter(obj, IExposurePortDataProvider)
                if a is not None:
                    yield a()

        # folder gets appended last for lazy reason - we assume all
        # paths will be created as files, meaning folders are created 
        # automatically, rather than creating that as file.
        # Then annotations can be assigned to them later, use viewinfo
        # to grab the relevant fields.
        fv = fieldvalues(cur)
        # need the subject of the current folder.
        fv['Subject'] = cur.Subject()
        yield (prefix, fv,)
Ejemplo n.º 2
0
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))