Пример #1
0
    def imp(self, **params):
        stash = {}

        # Get any existing Importer
        imp = None
        imp_id = getintparam("id", None, stash, params)
        if imp_id is not None:
            imp = self.imports.get(imp_id, None)
            if imp is None:
                context = {}
                context['error'] = 'Unknown import ID'
                return render("import/choose-file.html", context)

        if imp is None:
            # No import in progress
            if cherrypy.request.method == "POST":
                return self.handle_import_start(params)
            elif cherrypy.request.method == "GET":
                return render("import/choose-file.html", {})
        else:
            # Import in progress; let it handle the request.
            if cherrypy.request.method == "POST":
                if getparam("import_cancel", None):
                    imp.cancel()
                    redirect(url("home"))
                return imp.handle_post(params)
            elif cherrypy.request.method == "GET":
                return imp.handle_get(params)

        raise cherrypy.NotFound
Пример #2
0
    def build_form_context(self, params):
        context = {}
        class Info(object):
            def __init__(self, **kwargs):
                for k, v in kwargs.items():
                    setattr(self, k, v)

        # Determine how conflicts should be resolved
        self.handler.record_conflict_resolution = self.get_conflict_option('record_conflict', params)
        context['record_conflict'] = self.handler.record_conflict_resolution

        self.handler.coll_conflict_resolution = self.get_conflict_option('coll_conflict', params)
        context['coll_conflict'] = self.handler.coll_conflict_resolution

        # Get the media root mappings supplied by the client.
        media_root_mappings = {}
        for num, (root, rootinfo) in enumerate(sorted(self.handler.media_roots.items())):
            newroot = getparam('r%d' % num, root, None, params)
            rootinfo['newroot'] = newroot
            if newroot != root:
                media_root_mappings[root] = newroot
        self.handler.update_media_root_mappings(media_root_mappings)

        # Calculate the info, after the mappings have been applied.
        info = Info(filename = self.importer.filename,
                    records_count = len(self.handler.records),
                    record_identical = self.handler.record_identical,
                    record_conflicts = self.handler.record_conflicts,
                    record_new = self.handler.record_new,
                    collections = self.handler.collections,
                    coll_identical = self.handler.coll_identical,
                    coll_conflicts = self.handler.coll_conflicts,
                    coll_new = self.handler.coll_new,
                    media_files_count = len(self.handler.media_paths),
                    media_roots = enumerate(sorted(self.handler.media_roots.items())),
                   )
        context['info'] = info
        context['import_id'] = getintparam("id", None, None, params)
        return context
Пример #3
0
    def export_records(self, export_id=None, fmt='rtf', download=False,
                       **params):
        """Export a set of records.

        """
        # Read parameters first, to ensure they all get stashed.
        stash = {}
        record_id = getparam('record_id', None, stash, params)
        issearch = getparam('issearch', None, stash, params)
        coll = getparam('coll', None, stash, params)
        incsubs = getintparam('incsubs', 1, stash, params)

        # Make a new exporter, and get any parameters it needs into the stash.
        try:
            exp = Exporter.get(fmt)()
        except KeyError:
            raise cherrypy.NotFound
        exp.read_params(stash, params)

        # Handle exports which are in progress, or have finished
        if export_id is not None:
            try:
                export_id = int(export_id)
            except ValueError:
                export_id = None
        if export_id is not None:
            try:
                running_exp = exports[export_id]
            except KeyError:
                # Invalid export id (most probably due to a server restart)
                running_exp = None
            if running_exp is not None:
                # Export in progress
                progress = pool.get_progress(export_id)
                context = dict(export_id=export_id, fmt=fmt, progress=progress,
                               stash=stash)
                if not progress.complete:
                    return render('export/in_progress.html', context)
                if progress.failed:
                    return render('export/failed.html', context)
                # Export complete
                if not download:
                    # Display a "Export prepared"
                    return render('export/complete.html', context)
                return running_exp.respond()

        # Read the set of things to export.
        search, desc, topcoll = self._get_export_records(stash, record_id, issearch,
                                                coll, incsubs)

        if not getparam('export', False, stash, params):
            # Not ready to start the export yet.
            context = dict(
                           fmts = Exporter.fmts(params),
                           fmt = fmt,
                           export_desc = desc,
                           stash = stash,
                          )

            exp.add_to_context(context, search, stash, params)
            return render('export/pickfmt.html', context)

        # Start the exporter
        task = ExportRecords(exp, search)
        export_id = pool.add_task(task)
        exports[export_id] = task

        redirect(url("records-export-inprogress", export_id=export_id, fmt=fmt, **stash))