From a93eb05cc6c50c97f5e4ffea7ce558dd91ecb8de Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Fri, 18 Oct 2024 13:37:31 -0400 Subject: [PATCH] fix: duplicate reports when run for past years --- osf/metrics/reports.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/osf/metrics/reports.py b/osf/metrics/reports.py index 08d14867ae0..5375b539daa 100644 --- a/osf/metrics/reports.py +++ b/osf/metrics/reports.py @@ -29,6 +29,11 @@ def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) assert 'report_date' in cls.UNIQUE_TOGETHER_FIELDS, f'DailyReport subclasses must have "report_date" in UNIQUE_TOGETHER_FIELDS (on {cls.__qualname__}, got {cls.UNIQUE_TOGETHER_FIELDS})' + def save(self, *args, **kwargs): + if self.timestamp is None: + self.timestamp = self.report_date + super().save(*args, **kwargs) + class Meta: abstract = True dynamic = metrics.MetaField('strict') @@ -101,6 +106,12 @@ def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) assert 'report_yearmonth' in cls.UNIQUE_TOGETHER_FIELDS, f'MonthlyReport subclasses must have "report_yearmonth" in UNIQUE_TOGETHER_FIELDS (on {cls.__qualname__}, got {cls.UNIQUE_TOGETHER_FIELDS})' + def save(self, *args, **kwargs): + if self.timestamp is None: + self.timestamp = self.report_yearmonth.target_month() + super().save(*args, **kwargs) + + @receiver(metrics_pre_save) def set_report_id(sender, instance, **kwargs):