Skip to content

Commit

Permalink
filter alerts on mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Dec 10, 2024
1 parent 55750a5 commit 81cbb89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
14 changes: 1 addition & 13 deletions osidb/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down
11 changes: 9 additions & 2 deletions osidb/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 81cbb89

Please sign in to comment.