How to use the sortinghat.db.model.Enrollment function in sortinghat

To help you get started, we’ve selected a few sortinghat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chaoss / grimoirelab-sortinghat / tests / test_api.py View on Github external
self.assertEqual(rol.organization.name, 'LibreSoft')

        rol = enrollments[4]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.uidentity.uuid, 'John Smith')
        self.assertEqual(rol.organization.name, 'Bitergia')

        rol = enrollments[5]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.uidentity.uuid, 'John Smith')
        self.assertEqual(rol.organization.name, 'Bitergia')
        self.assertEqual(rol.start, datetime.datetime(1999, 1, 1))
        self.assertEqual(rol.end, datetime.datetime(2000, 1, 1))

        rol = enrollments[6]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.uidentity.uuid, 'John Smith')
        self.assertEqual(rol.organization.name, 'Example')

        # Test dates
        enrollments = api.enrollments(self.db,
                                      from_date=datetime.datetime(1998, 5, 1),
                                      to_date=datetime.datetime(2006, 1, 1))
        self.assertEqual(len(enrollments), 1)

        rol = enrollments[0]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.uidentity.uuid, 'John Smith')
        self.assertEqual(rol.organization.name, 'Bitergia')
        self.assertEqual(rol.start, datetime.datetime(1999, 1, 1))
        self.assertEqual(rol.end, datetime.datetime(2000, 1, 1))
github chaoss / grimoirelab-sortinghat / tests / test_parser_grimoirelab.py View on Github external
def test_enrollments_parser(self):
        """Check whether enrollments are correctly parsed"""

        stream_ids = self.read_file(datadir('grimoirelab_valid.yml'))

        parser = GrimoireLabParser(stream_ids)

        # Parsed unique identities
        uids = parser.identities

        # J. Manrique Lopez de la Fuente
        uid = uids[0]
        rol = uid.enrollments[0]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.organization.name, 'Bitergia')
        self.assertEqual(rol.start, datetime.datetime(2013, 1, 1, 0, 0))
        self.assertEqual(rol.end, datetime.datetime(2100, 1, 1, 0, 0))

        # Luis Cañas-Díaz
        uid = uids[1]

        # Unknown organization is ignored during the parsing process
        self.assertEqual(len(uid.enrollments), 2)

        rol = uid.enrollments[0]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.organization.name, 'GSyC/LibreSoft')
        self.assertEqual(rol.start, datetime.datetime(2003, 1, 1, 0, 0))
        self.assertEqual(rol.end, datetime.datetime(2011, 12, 31, 0, 0))
github chaoss / grimoirelab-sortinghat / tests / test_parser_grimoirelab.py View on Github external
self.assertEqual(rol.end, datetime.datetime(2100, 1, 1, 0, 0))

        # Luis Cañas-Díaz
        uid = uids[1]

        # Unknown organization is ignored during the parsing process
        self.assertEqual(len(uid.enrollments), 2)

        rol = uid.enrollments[0]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.organization.name, 'GSyC/LibreSoft')
        self.assertEqual(rol.start, datetime.datetime(2003, 1, 1, 0, 0))
        self.assertEqual(rol.end, datetime.datetime(2011, 12, 31, 0, 0))

        rol = uid.enrollments[1]
        self.assertIsInstance(rol, Enrollment)
        self.assertEqual(rol.organization.name, 'Bitergia')
        self.assertEqual(rol.start, datetime.datetime(2012, 1, 1, 0, 0))
        self.assertEqual(rol.end, datetime.datetime(2100, 1, 1, 0, 0))

        # Owl Bot
        uid = uids[2]

        # Unknown organization is ignored during the parsing process
        self.assertEqual(len(uid.enrollments), 0)
github chaoss / grimoirelab-sortinghat / tests / test_db_api.py View on Github external
with self.db.connect() as session:
            example_org = Organization(name='Example')
            bitergia_org = Organization(name='Bitergia')
            session.add(example_org)
            session.add(bitergia_org)

            jsmith = UniqueIdentity(uuid='AAAA')
            jsmith.profile = Profile(name='John Smith', email='jsmith@example.net')

            jdoe = UniqueIdentity(uuid='BBBB')
            jdoe.profile = Profile(name='John Doe', email='jdoe@bitergia.com')

            enrol = Enrollment(uidentity=jsmith, organization=example_org)
            session.add(enrol)
            enrol = Enrollment(uidentity=jdoe, organization=example_org)
            session.add(enrol)
            enrol = Enrollment(uidentity=jdoe, organization=bitergia_org)
            session.add(enrol)

        before_dt = datetime.datetime.utcnow()

        with self.db.connect() as session:
            organization = api.find_organization(session, 'Example')
            api.delete_organization(session, organization)

        after_dt = datetime.datetime.utcnow()

        with self.db.connect() as session:
            jsmith = api.find_unique_identity(session, 'AAAA')
            self.assertIsInstance(jsmith.last_modified, datetime.datetime)
            self.assertLessEqual(before_dt, jsmith.last_modified)
github chaoss / grimoirelab-sortinghat / tests / test_db_api.py View on Github external
def test_move_enrollment(self):
        """Test if an enrollment is moved to a unique identity"""

        with self.db.connect() as session:
            uidentity = api.add_unique_identity(session, 'AAAA')
            api.add_unique_identity(session, 'BBBB')

            org = api.add_organization(session, 'Example')
            api.enroll(session, uidentity, org)

        with self.db.connect() as session:
            uidentity = api.find_unique_identity(session, 'BBBB')

            enrollment = session.query(Enrollment).\
                filter(Enrollment.uuid == 'AAAA').first()

            result = api.move_enrollment(session, enrollment, uidentity)
            self.assertEqual(result, True)

        with self.db.connect() as session:
            enrollments = session.query(Enrollment).\
                join(UniqueIdentity).\
                filter(UniqueIdentity.uuid == 'AAAA').all()
            self.assertEqual(len(enrollments), 0)

            enrollments = session.query(Enrollment). \
                join(UniqueIdentity). \
                filter(UniqueIdentity.uuid == 'BBBB').all()
            self.assertEqual(len(enrollments), 1)
github chaoss / grimoirelab-sortinghat / sortinghat / api.py View on Github external
profile_data['gender_acc'] = fuid.profile.gender_acc
            if fuid.profile.is_bot:
                profile_data['is_bot'] = True

            edit_profile_db(session, tuid, **profile_data)

        # Update identities
        for identity in fuid.identities:
            move_identity_db(session, identity, tuid)

        # Move those enrollments that to_uid does not have.
        # It is needed to copy the list in-place to avoid
        # sync problems when enrollments are moved.
        for rol in fuid.enrollments[:]:
            enrollment = session.query(Enrollment).\
                filter(Enrollment.uidentity == tuid,
                       Enrollment.organization == rol.organization,
                       Enrollment.start == rol.start,
                       Enrollment.end == rol.end).first()

            if not enrollment:
                move_enrollment_db(session, rol, tuid)

        # For some reason, uuid are not updated until changes are
        # committed (flush does nothing). Force to commit changes
        # to avoid deletion of identities when removing 'fuid'
        session.commit()

        delete_unique_identity_db(session, fuid)

        # Retrieve of organizations to merge the enrollments,
        # before closing the session
github chaoss / grimoirelab-sortinghat / sortinghat / api.py View on Github external
:raises InvalidValueError: when any date is out of bounds
    """
    # Merge enrollments
    with db.connect() as session:
        uidentity = find_unique_identity(session, uuid)

        if not uidentity:
            raise NotFoundError(entity=uuid)

        org = find_organization(session, organization)

        if not org:
            raise NotFoundError(entity=organization)

        disjoint = session.query(Enrollment).\
            filter(Enrollment.uidentity == uidentity,
                   Enrollment.organization == org).all()

        if not disjoint:
            entity = '-'.join((uuid, organization))
            raise NotFoundError(entity=entity)

        dates = [(enr.start, enr.end) for enr in disjoint]

        for st, en in utils.merge_date_ranges(dates):
            # We prefer this method to find duplicates
            # to avoid integrity exceptions when creating
            # enrollments that are already in the database
            is_dup = lambda x, st, en: x.start == st and x.end == en

            filtered = [x for x in disjoint if not is_dup(x, st, en)]
github chaoss / grimoirelab-sortinghat / sortinghat / parsing / grimoirelab.py View on Github external
org = Organization(name=name)

            if org is None:
                continue

            if 'start' in aff:
                start_date = self.__force_datetime(aff['start'])
            else:
                start_date = MIN_PERIOD_DATE

            if 'end' in aff:
                end_date = self.__force_datetime(aff['end'])
            else:
                end_date = MAX_PERIOD_DATE

            enrollment = Enrollment(start=start_date, end=end_date,
                                    organization=org)
            enrollments.append(enrollment)

        if self.enrollment_periods_validation:
            self.__validate_enrollment_periods(enrollments)

        return enrollments
github chaoss / grimoirelab-sortinghat / sortinghat / db / api.py View on Github external
or `from_date > to_date`.
    """
    if not from_date:
        raise ValueError("'from_date' cannot be None")
    if not to_date:
        raise ValueError("'to_date' cannot be None")

    if from_date < MIN_PERIOD_DATE or from_date > MAX_PERIOD_DATE:
        raise ValueError("'from_date' %s is out of bounds" % str(from_date))
    if to_date < MIN_PERIOD_DATE or to_date > MAX_PERIOD_DATE:
        raise ValueError("'to_date' %s is out of bounds" % str(to_date))
    if from_date > to_date:
        raise ValueError("'from_date' %s cannot be greater than %s"
                         % (from_date, to_date))

    enrollment = Enrollment(uidentity=uidentity,
                            organization=organization,
                            start=from_date, end=to_date)
    uidentity.last_modified = datetime.datetime.utcnow()

    session.add(enrollment)

    return enrollment
github chaoss / grimoirelab-sortinghat / sortinghat / parsing / mailmap.py View on Github external
uid = UniqueIdentity(uuid=uuid)
                identity.uuid = uuid
                uid.identities.append(identity)
                self._identities[uuid] = uid

            # Parse organization
            mailmap_id = aliases[0]
            name = self.__encode(mailmap_id[0])

            if name in MAILMAP_NO_ORGS:
                continue

            org = Organization(name=name)
            self._organizations[name] = org

            enrollment = Enrollment(start=MIN_PERIOD_DATE, end=MAX_PERIOD_DATE,
                                    organization=org)
            uid.enrollments.append(enrollment)