How to use the sortinghat.core.log.TransactionsLog.open 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_db.py View on Github external
def setUp(self):
        """Load initial dataset"""

        self.user = get_user_model().objects.create(username='test')
        self.ctx = SortingHatContext(self.user)

        self.trxl = TransactionsLog.open('delete_identity', self.ctx)
github chaoss / grimoirelab-sortinghat / tests / test_log.py View on Github external
def test_log_operation(self):
        """Check if a new operation is logged"""

        trxl = TransactionsLog.open('test', self.ctx)
        input_args = {'mk': '12345abcd'}

        operation = trxl.log_operation(op_type=Operation.OpType.ADD, timestamp=datetime_utcnow(),
                                       entity_type='test_entity', target='test', args=input_args)
        self.assertIsInstance(operation, Operation)

        operation_db = Operation.objects.get(ouid=operation.ouid)
        self.assertIsInstance(operation_db, Operation)
        self.assertEqual(operation_db.op_type, Operation.OpType.ADD.value)
        self.assertEqual(operation_db.entity_type, 'test_entity')
        self.assertEqual(operation_db.timestamp, operation.timestamp)
        self.assertEqual(operation_db.trx, trxl.trx)
        self.assertEqual(operation_db.target, 'test')
        self.assertEqual(operation_db.args, json.dumps(input_args))
        self.assertEqual(input_args, json.loads(operation_db.args))
github chaoss / grimoirelab-sortinghat / tests / test_log.py View on Github external
def test_operation_type_empty(self):
        """Check if it fails when type field is an empty string"""

        trxl = TransactionsLog.open('test', self.ctx)
        input_args = json.dumps({'mk': '12345abcd'})

        with self.assertRaisesRegex(TypeError, OPERATION_TYPE_EMPTY_ERROR):
            trxl.log_operation(op_type='', entity_type='test_entity',
                               timestamp=datetime_utcnow(), target='test', args=input_args)
github chaoss / grimoirelab-sortinghat / tests / test_log.py View on Github external
def test_context_anonymous_user(self):
        """Check if a new transaction is added when the user is anonymous"""

        anon_user = AnonymousUser()
        ctx = SortingHatContext(anon_user)

        timestamp = datetime_utcnow()

        trxl = TransactionsLog.open('test', ctx)
        self.assertIsInstance(trxl, TransactionsLog)

        trx_db = Transaction.objects.get(tuid=trxl.trx.tuid)
        self.assertIsInstance(trx_db, Transaction)
        self.assertEqual(trx_db.name, 'test')
        self.assertGreater(trx_db.created_at, timestamp)
        self.assertIsNone(trx_db.closed_at)
        self.assertIsNone(trx_db.authored_by)
github chaoss / grimoirelab-sortinghat / tests / test_db.py View on Github external
def setUp(self):
        """Load initial dataset"""

        self.user = get_user_model().objects.create(username='test')
        self.ctx = SortingHatContext(self.user)

        self.trxl = TransactionsLog.open('add_organization', self.ctx)
github chaoss / grimoirelab-sortinghat / tests / test_log.py View on Github external
def test_context_user_empty(self):
        """Check if it fails when `user` field is an empty string"""

        ctx = SortingHatContext('')
        with self.assertRaisesRegex(TypeError, TRANSACTION_CTX_USER_EMPTY_ERROR):
            TransactionsLog.open('test', ctx)
github chaoss / grimoirelab-sortinghat / sortinghat / core / api.py View on Github external
try:
            indv = move_identity_db(trxl, identity, individual)
        except ValueError:
            # Case when the identity is already assigned to the individual
            indv = individual

        return indv

    # Check input value
    if uuids is None:
        raise InvalidValueError(msg="'uuids' cannot be None")
    if uuids == []:
        raise InvalidValueError(msg="'uuids' cannot be an empty list")

    trxl = TransactionsLog.open('unmerge_identities', ctx)

    identities = _find_identities(uuids)

    new_individuals = []
    for identity in identities:
        indv = _set_destination_for_identity(trxl, identity)
        individual = _move_to_destination(trxl, identity, indv)
        new_individuals.append(individual)

    trxl.close()

    return new_individuals
github chaoss / grimoirelab-sortinghat / sortinghat / core / api.py View on Github external
:param uuid: identifier assigned to the identity or individual
        that will be removed

    :returns: an individual with the identity removed; `None` when
        the individual was also removed.

    :raises InvalidValueError: when `uuid` is `None` or empty.
    :raises NotFoundError: when the identity does not exist in the
        registry.
    """
    if uuid is None:
        raise InvalidValueError(msg="'uuid' cannot be None")
    if uuid == '':
        raise InvalidValueError(msg="'uuid' cannot be an empty string")

    trxl = TransactionsLog.open('delete_identity', ctx)

    identity = find_identity(uuid)
    individual = identity.individual

    if individual.mk == uuid:
        delete_individual_db(trxl, identity.individual)
        individual = None
    else:
        delete_identity_db(trxl, identity)
        individual.refresh_from_db()

    trxl.close()

    return individual
github chaoss / grimoirelab-sortinghat / sortinghat / core / api.py View on Github external
The result of calling this function will be the `Individual`
    object with an updated profile.

    :param ctx: context from where this method is called
    :param uuid: identifier of the individual which its project
        will be updated
    :param kwargs: keyword arguments with data to update the profile

    :returns: an individual with its profile updated

    :raises NotFoundError: raised when either the individual
        or the country code do not exist in the registry.
    :raises InvalidValueError: raised when any of the keyword arguments
        has an invalid value.
    """
    trxl = TransactionsLog.open('update_profile', ctx)

    individual = find_individual_by_uuid(uuid)

    try:
        individual = update_profile_db(trxl, individual, **kwargs)
    except ValueError as e:
        raise InvalidValueError(msg=str(e))

    trxl.close()

    return individual
github chaoss / grimoirelab-sortinghat / sortinghat / core / api.py View on Github external
a `NotFoundError` exception.

    :param ctx: context from where this method is called
    :param domain_name: name of the domain to remove

    :returns the removed domain object

    :raises NotFoundError: raised when the domain
        does not exist in the registry.
    """
    if domain_name is None:
        raise InvalidValueError(msg="'domain_name' cannot be None")
    if domain_name == '':
        raise InvalidValueError(msg="'domain_name' cannot be an empty string")

    trxl = TransactionsLog.open('delete_domain', ctx)

    try:
        domain = find_domain(domain_name)
    except ValueError as e:
        raise InvalidValueError(msg=str(e))
    except NotFoundError as exc:
        raise exc

    delete_domain_db(trxl, domain)

    trxl.close()

    return domain