Пример #1
0
def test_set_current_transaction():
    tx = factories.TransactionFactory.create()
    assert tx is not None
    assert get_current_transaction() is None

    set_current_transaction(tx)
    assert get_current_transaction() is tx
Пример #2
0
def test_override_current_transaction():
    tx = factories.TransactionFactory.create()
    tx2 = factories.TransactionFactory.create()

    set_current_transaction(tx)

    with override_current_transaction(tx2):
        assert get_current_transaction() is tx2

    assert get_current_transaction() is tx
Пример #3
0
 def checkers_for(cls: Type[Self], model: TrackedModel) -> Collection[Self]:
     """Return a set of IndirectBusinessRuleCheckers for every model found on
     rule.get_linked_models."""
     rules = set()
     transaction = get_current_transaction()
     if cls.rule in model.indirect_business_rules:
         for linked_model in cls.rule.get_linked_models(model, transaction):
             rules.add(cls(linked_model))
     return rules
Пример #4
0
 def run(self, model: TrackedModel) -> CheckResult:
     """
     :return CheckResult, a Tuple(rule_passed: str, violation_reason: Optional[str]).
     """
     transaction = get_current_transaction()
     try:
         self.rule(transaction).validate(model)
         return True, None
     except BusinessRuleViolation as violation:
         return False, violation.args[0]
Пример #5
0
    def get_descriptions(self,
                         transaction=None,
                         request=None) -> TrackedModelQuerySet:
        """
        Get the latest descriptions related to this instance of the Tracked
        Model.

        If there is no Description relation existing a `NoDescriptionError` is raised.

        If a transaction is provided then all latest descriptions that are either approved
        or in the workbasket of the transaction up to the transaction will be provided.
        """
        try:
            descriptions_model = self.descriptions.model
        except AttributeError as e:
            raise NoDescriptionError(
                f"Model {self.__class__.__name__} has no descriptions relation.",
            ) from e

        for field, model in get_relations(descriptions_model).items():
            if isinstance(self, model):
                field_name = field.name
                break
        else:
            raise NoDescriptionError(
                f"No foreign key back to model {self.__class__.__name__} "
                f"found on description model {descriptions_model.__name__}.", )

        filter_kwargs = {
            f"{field_name}__{key}": value
            for key, value in self.get_identifying_fields().items()
        }

        query = descriptions_model.objects.filter(**filter_kwargs).order_by(
            *descriptions_model._meta.ordering)

        if transaction:
            return query.approved_up_to_transaction(transaction)

        # if a global transaction variable is available, filter objects approved up to this
        if get_current_transaction():
            return query.current()

        return query.latest_approved()
Пример #6
0
def test_get_current_transaction_from_thread_locals():
    with mock.patch("common.models.utils._thread_locals") as thread_locals:
        assert get_current_transaction() is thread_locals.transaction