Ejemplo n.º 1
0
def export_matches(export_id):
    """Export the top N matches of cross-referencing for the given collection
    to an Excel formatted export."""
    export = Export.by_id(export_id)
    export_dir = ensure_path(mkdtemp(prefix="aleph.export."))
    try:
        role = Role.by_id(export.creator_id)
        authz = Authz.from_role(role)
        collection = Collection.by_id(export.collection_id)
        file_name = "%s - Crossreference.xlsx" % collection.label
        file_path = export_dir.joinpath(file_name)
        excel = ExcelWriter()
        headers = [
            "Score",
            "Entity Name",
            "Entity Date",
            "Entity Countries",
            "Candidate Collection",
            "Candidate Name",
            "Candidate Date",
            "Candidate Countries",
            "Entity Link",
            "Candidate Link",
        ]
        sheet = excel.make_sheet("Cross-reference", headers)
        batch = []

        for match in iter_matches(collection, authz):
            batch.append(match)
            if len(batch) >= BULK_PAGE:
                _iter_match_batch(excel, sheet, batch)
                batch = []
        if len(batch):
            _iter_match_batch(excel, sheet, batch)

        with open(file_path, "wb") as fp:
            buffer = excel.get_bytesio()
            for data in buffer:
                fp.write(data)

        complete_export(export_id, file_path)
    except Exception:
        log.exception("Failed to process export [%s]", export_id)
        export = Export.by_id(export_id)
        export.set_status(status=Status.FAILED)
        db.session.commit()
    finally:
        shutil.rmtree(export_dir)
Ejemplo n.º 2
0
    def setUp(self):
        super(ExportApiTestCase, self).setUp()
        self.load_fixtures()
        self.email = "*****@*****.**"
        self.role_email = self.create_user("with_email", email=self.email)
        _, self.headers = self.login(foreign_id="with_email")

        csv_path = self.get_fixture_path("experts.csv")
        temp_path = self._create_temporary_copy(csv_path, "experts.csv")
        self.export1 = create_export("TEST", self.role_email.id, "test1")
        complete_export(self.export1.id, temp_path, "exports.csv")

        temp_path = self._create_temporary_copy(csv_path, "experts.csv")
        self.export2 = create_export("TEST", self.role_email.id, "test2")
        self.export2.expires_at = datetime.utcnow() + timedelta(days=-1)
        complete_export(self.export2.id, temp_path, "experts.csv")
Ejemplo n.º 3
0
    def setUp(self):
        super(ExportsTestCase, self).setUp()
        self.load_fixtures()
        self.email = "*****@*****.**"
        self.role_email = self.create_user("with_email", email=self.email)

        csv_path = self.get_fixture_path("experts.csv")
        temp_path = self._create_temporary_copy(csv_path, "experts.csv")
        self.export1 = create_export("TEST",
                                     self.role_email.id,
                                     "test1",
                                     expires_after=Export.DEFAULT_EXPIRATION)
        complete_export(self.export1.id, temp_path)

        temp_path = self._create_temporary_copy(csv_path, "experts.csv")
        self.export2 = create_export("TEST",
                                     self.role_email.id,
                                     "test2",
                                     expires_after=timedelta(days=-1))
        complete_export(self.export2.id, temp_path)
Ejemplo n.º 4
0
    def setUp(self):
        super(ExportsTestCase, self).setUp()
        self.load_fixtures()
        self.email = "*****@*****.**"
        self.role_email = self.create_user("with_email", email=self.email)

        csv_path = self.get_fixture_path("experts.csv")
        temp_path = self._create_temporary_copy(csv_path, "experts.csv")
        self.export1 = create_export("TEST", self.role_email.id, "test1")
        complete_export(self.export1.id, temp_path, "experts.csv")

        temp_path = self._create_temporary_copy(csv_path, "experts.csv")
        self.export2 = create_export("TEST", self.role_email.id, "test2")
        self.export2.expires_at = datetime.utcnow() + timedelta(days=-1)
        complete_export(self.export2.id, temp_path, "experts.csv")

        source_path = self.get_fixture_path("../util.py")
        temp_path = self._create_temporary_copy(source_path, "init.py")
        self.export3 = create_export("TEST", self.role_email.id, "test3")
        self.export3.expires_at = datetime.utcnow() + timedelta(days=-1)
        complete_export(self.export3.id, temp_path, "init.py")