Exemple #1
0
def export_worklist_task(self, user_id, param_path):
    """
    Task runs the code for creating a worklist export, from form data validated by a view.

    :param user_id: the primary key of the user running the worklist
    :param param_path: the key returned from main.redis.ScratchStorage.save()
        used to access saved worklist parameters
    :returns: the key used to access worklist data from main.redis.ScratchStorage.load()
    :throws RuntimeError: on any errors occuring while running the export
    """
    try:
        # load info needed to build worklist
        User = get_user_model()
        user = User.objects.get(id=user_id)
        notifications = RedisBroker(user)
        broker = ExportBroker(user_id)
        export_id = self.request.id[:8]
        try:
            export_name = execute_export_worklist(broker, user, export_id,
                                                  param_path)
            url = f'{reverse("export:worklist")}?download={export_id}'
            message = _(
                'Your worklist for "{name}" is ready. '
                '<a href="{url}" class="download">Download the file here</a>.'
            ).format(name=export_name, url=url)
            notifications.notify(message,
                                 tags=("download", ),
                                 payload={"url": url})
        except Exception as e:
            logger.exception(f"Failure in export_worklist_task: {e}")
            message = _("Export failed. EDD encountered this problem: {ex}"
                        ).format(ex=e)
            notifications.notify(message)
        notifications.mark_read(self.request.id)
    except Exception as e:
        logger.exception("Failure in export_worklist_task: %s", e)
        raise RuntimeError(
            _("Failed export, EDD encountered this problem: {e}").format(e=e))
Exemple #2
0
def import_table_task(self, study_id, user_id, import_id):
    """
    Task runs the code for importing a table of data.

    :param study_id: the primary key of the target study
    :param user_id: the primary key of the user running the import
    :param import_id: the UUID of this import
    :returns: a message to display via the TaskNotification middleware
    :throws RuntimeError: on any errors occurring while running the import
    """
    start = arrow.utcnow()
    study = None
    user = None
    import_params = None
    try:
        # load all the import data into memory from DB/from cache, leaving it in cache for
        # potential later reuse
        study = models.Study.objects.get(pk=study_id)
        user = User.objects.get(pk=user_id)
        notifications = RedisBroker(user)

        # set a fake request object with update info
        fake_request = HttpRequest()

        try:
            # load global context for the import
            broker = ImportBroker()
            import_params = json.loads(broker.load_context(import_id))
            if "update_id" in import_params:
                update_id = import_params.get("update_id")
                fake_request.update_obj = models.Update.objects.get(pk=update_id)
            else:
                fake_request.update_obj = models.Update.load_update(user=user)
            set_thread_variable("request", fake_request)

            # load paged series data
            pages = broker.load_pages(import_id)

            # do the import
            total_added = 0
            total_updated = 0
            importer = TableImport(study, user)
            importer.parse_context(import_params)

            with transaction.atomic(savepoint=False):
                for page in pages:
                    parsed_page = json.loads(page)
                    added, updated = importer.import_series_data(parsed_page)
                    total_added += added
                    total_updated += updated
                importer.finish_import()

            # if requested, notify user of completion (e.g. for a large import)
            send_import_completion_email(
                study, user, import_params, start, total_added, total_updated
            )
            message = _(
                "Finished import to {study}: {total_added} added and {total_updated} "
                "updated measurements.".format(
                    study=study.name,
                    total_added=total_added,
                    total_updated=total_updated,
                )
            )
            notifications.notify(message, tags=("legacy-import-message",))
            notifications.mark_read(self.request.id)

        except Exception as e:
            logger.exception("Failure in import_table_task", e)

            # send configured error notifications
            send_import_failure_email(study, user, import_id, import_params)
            message = _(
                "Failed import to {study}, EDD encountered this problem: {e}"
            ).format(study=study.name, e=e)
            notifications.notify(message, tags=("legacy-import-message",))
            notifications.mark_read(self.request.id)
            raise RuntimeError(
                _(
                    f"Failed import to {study.name}, EDD encountered this problem: "
                    f"{e}"
                )
            )
        finally:
            set_thread_variable("request", None)
    except Exception as e:
        logger.exception(f"Failure in import_table_task: {e}")
        raise RuntimeError(
            _(f"Failed import to study {study_id}, EDD encountered this problem: {e}")
        )