diff --git a/commodities/models/dc.py b/commodities/models/dc.py index 7bbce5cfd..38ba18ce2 100644 --- a/commodities/models/dc.py +++ b/commodities/models/dc.py @@ -502,7 +502,7 @@ def get_dependent_measures( measure_qs = Measure.objects.filter(goods_sid_query) if self.moment.clock_type.is_transaction_clock: - measure_qs = measure_qs.current() + measure_qs = measure_qs.approved_up_to_transaction(self.moment.transaction) else: measure_qs = measure_qs.latest_approved() @@ -823,7 +823,7 @@ def get_snapshot( date=snapshot_date, ) - commodities = self._get_snapshot_commodities(snapshot_date) + commodities = self._get_snapshot_commodities(transaction, snapshot_date) return CommodityTreeSnapshot( moment=moment, @@ -832,6 +832,7 @@ def get_snapshot( def _get_snapshot_commodities( self, + transaction: Transaction, snapshot_date: date, ) -> List[Commodity]: """ @@ -852,10 +853,12 @@ def _get_snapshot_commodities( that match the latest_version goods. """ item_ids = {c.item_id for c in self.commodities if c.obj} - goods = GoodsNomenclature.objects.filter( + goods = GoodsNomenclature.objects.approved_up_to_transaction( + transaction, + ).filter( item_id__in=item_ids, valid_between__contains=snapshot_date, - ).current() + ) latest_versions = get_latest_versions(goods) pks = {good.pk for good in latest_versions} diff --git a/common/business_rules.py b/common/business_rules.py index 97c7414a1..79b7a6283 100644 --- a/common/business_rules.py +++ b/common/business_rules.py @@ -144,7 +144,7 @@ def get_linked_models( related_instances = [getattr(model, field.name)] for instance in related_instances: try: - yield instance.version_at() + yield instance.version_at(transaction) except TrackedModel.DoesNotExist: # `related_instances` will contain all instances, even # deleted ones, and `version_at` will return @@ -278,8 +278,8 @@ def validate(self, model): if ( type(model) - .objects.current() - .filter(**query) + .objects.filter(**query) + .approved_up_to_transaction(self.transaction) .exclude(version_group=model.version_group) .exists() ): @@ -305,8 +305,8 @@ def validate(self, model): query["valid_between__overlap"] = model.valid_between if ( - model.__class__.objects.current() - .filter(**query) + model.__class__.objects.filter(**query) + .approved_up_to_transaction(self.transaction) .exclude(version_group=model.version_group) .exists() ): @@ -573,7 +573,7 @@ def validate(self, exclusion): Membership = geo_group._meta.get_field("members").related_model if ( - not Membership.objects.current() + not Membership.objects.approved_up_to_transaction(self.transaction) .filter( geo_group__sid=geo_group.sid, member__sid=exclusion.excluded_geographical_area.sid, diff --git a/common/models/trackedmodel.py b/common/models/trackedmodel.py index f3cddd514..d2cc3947c 100644 --- a/common/models/trackedmodel.py +++ b/common/models/trackedmodel.py @@ -342,7 +342,7 @@ def current_version(self: Cls) -> Cls: raise self.__class__.DoesNotExist("Object has no current version") return current_version - def version_at(self: Cls) -> Cls: + def version_at(self: Cls, transaction) -> Cls: """ The latest version of this model that was approved as of the given transaction. @@ -350,7 +350,7 @@ def version_at(self: Cls) -> Cls: :param transaction Transaction: Limit versions to this transaction :rtype TrackedModel: """ - return self.get_versions().current().get() + return self.get_versions().approved_up_to_transaction(transaction).get() @classproperty def copyable_fields(cls): @@ -504,12 +504,12 @@ def copy( kwargs.update(nested_fields) if not ignore: - for model in queryset.current(): + for model in queryset.approved_up_to_transaction(transaction): model.copy(transaction, **kwargs) return new_object - def in_use_by(self, via_relation: str) -> QuerySet[TrackedModel]: + def in_use_by(self, via_relation: str, transaction=None) -> QuerySet[TrackedModel]: """ Returns all of the models that are referencing this one via the specified relation and exist as of the passed transaction. @@ -526,7 +526,7 @@ def in_use_by(self, via_relation: str) -> QuerySet[TrackedModel]: return remote_model.objects.filter( **{f"{remote_field_name}__version_group": self.version_group} - ).current() + ).approved_up_to_transaction(transaction) def in_use(self, transaction=None, *relations: str) -> bool: """ @@ -572,7 +572,7 @@ def in_use(self, transaction=None, *relations: str) -> bool: # If we find any objects for any relation, then the model is in use. for relation_name in using_models: - relation_queryset = self.in_use_by(relation_name) + relation_queryset = self.in_use_by(relation_name, transaction) if relation_queryset.exists(): return True diff --git a/measures/querysets.py b/measures/querysets.py index 552c4327d..62c23fc34 100644 --- a/measures/querysets.py +++ b/measures/querysets.py @@ -95,7 +95,7 @@ def duty_sentence( # Components with the greatest transaction_id that is less than # or equal to component_parent's transaction_id, are considered 'current'. component_qs = component_parent.components.approved_up_to_transaction( - transaction=component_parent.transaction + component_parent.transaction, ) if not component_qs: return "" diff --git a/measures/tests/factories.py b/measures/tests/factories.py index 061fdd49c..3f8a00539 100644 --- a/measures/tests/factories.py +++ b/measures/tests/factories.py @@ -40,7 +40,7 @@ class Meta: excluded_origin_descriptions = factory.LazyAttribute( lambda m: random.choice(MeasureSheetRow.separators).join( e.excluded_geographical_area.descriptions.approved_up_to_transaction( - transaction=e.excluded_geographical_area + transaction=e.excluded_geographical_area.transaction, ) .last() .description diff --git a/workbaskets/views/mixins.py b/workbaskets/views/mixins.py index 671392774..83a626352 100644 --- a/workbaskets/views/mixins.py +++ b/workbaskets/views/mixins.py @@ -10,5 +10,9 @@ def workbasket(self) -> WorkBasket: def get_queryset(self): qs = super().get_queryset() + transaction = None + current = self.workbasket + if current: + transaction = current.transactions.last() - return qs.current() + return qs.approved_up_to_transaction(transaction)