diff --git a/osidb/api_views.py b/osidb/api_views.py index 2f24a85d4..645de1501 100644 --- a/osidb/api_views.py +++ b/osidb/api_views.py @@ -527,24 +527,15 @@ def get(self, request): class FlawView(RudimentaryUserPathLoggingMixin, ModelViewSet): queryset = Flaw.objects.prefetch_related( "acknowledgments", - "acknowledgments__alerts", "affects", - "affects__alerts", "affects__cvss_scores", - "affects__cvss_scores__alerts", "affects__trackers", "affects__trackers__errata", "affects__trackers__affects", - "affects__trackers__alerts", - "alerts", "comments", - "comments__alerts", "cvss_scores", - "cvss_scores__alerts", "package_versions", - "package_versions__alerts", "references", - "references__alerts", ).all() serializer_class = FlawSerializer filter_backends = (DjangoFilterBackend,) @@ -827,13 +818,10 @@ class AffectView( RudimentaryUserPathLoggingMixin, SubFlawViewDestroyMixin, ModelViewSet ): queryset = Affect.objects.prefetch_related( - "alerts", "cvss_scores", - "cvss_scores__alerts", "trackers", "trackers__errata", "trackers__affects", - "trackers__alerts", ).all() serializer_class = AffectSerializer filterset_class = AffectFilter @@ -1095,7 +1083,7 @@ def destroy(self, request, *args, **kwargs): ), ) class TrackerView(RudimentaryUserPathLoggingMixin, ModelViewSet): - queryset = Tracker.objects.prefetch_related("alerts", "errata", "affects").all() + queryset = Tracker.objects.prefetch_related("errata", "affects").all() serializer_class = TrackerSerializer filterset_class = TrackerFilter http_method_names = get_valid_http_methods(ModelViewSet, excluded=["delete"]) diff --git a/osidb/mixins.py b/osidb/mixins.py index 71c86c5e9..8baf76e13 100644 --- a/osidb/mixins.py +++ b/osidb/mixins.py @@ -687,10 +687,17 @@ class AlertMixin(ValidateMixin): It also provides the automatic validation mechanism on every save. """ - alerts = GenericRelation(Alert) + _alerts = GenericRelation(Alert) last_validated_dt = models.DateTimeField(blank=True, default=timezone.now) + @property + def alerts(self): + """ + Get all alerts related to this object. + """ + return self._alerts.filter(created_dt__gte=self.last_validated_dt) + def alert( self, name, @@ -817,7 +824,7 @@ def save(self, *args, **kwargs): dry_run = kwargs.pop("no_alerts", False) if not dry_run: - self.last_validated_dt = timezone.now().replace(microsecond=0) + self.last_validated_dt = timezone.now() self.validate( raise_validation_error=kwargs.pop("raise_validation_error", True),