Return notifications based on user permissions. Filter out expired and dismissed notifications for regular users. Evaluate conditions for developer notifications.
(self)
| 587 | permission_classes = [IsAuthenticated] |
| 588 | |
| 589 | def get_queryset(self): |
| 590 | """ |
| 591 | Return notifications based on user permissions. |
| 592 | Filter out expired and dismissed notifications for regular users. |
| 593 | Evaluate conditions for developer notifications. |
| 594 | """ |
| 595 | from core.developer_notifications import evaluate_conditions |
| 596 | from django.core.cache import cache |
| 597 | |
| 598 | user = self.request.user |
| 599 | now = dj_timezone.now() |
| 600 | |
| 601 | queryset = SystemNotification.objects.filter(is_active=True) |
| 602 | |
| 603 | # Filter out expired notifications |
| 604 | queryset = queryset.filter( |
| 605 | models.Q(expires_at__isnull=True) | models.Q(expires_at__gt=now) |
| 606 | ) |
| 607 | |
| 608 | # Filter admin-only notifications for non-admins |
| 609 | if getattr(user, 'user_level', 0) < 10: |
| 610 | queryset = queryset.filter(admin_only=False) |
| 611 | |
| 612 | # For developer notifications, evaluate conditions |
| 613 | # Cache the evaluation per notification to avoid repeated condition checks |
| 614 | notifications_to_exclude = [] |
| 615 | developer_notifications = queryset.filter(source=SystemNotification.Source.DEVELOPER) |
| 616 | |
| 617 | for notification in developer_notifications: |
| 618 | action_data = notification.action_data or {} |
| 619 | conditions = action_data.get('condition', []) |
| 620 | |
| 621 | if not conditions: |
| 622 | continue |
| 623 | |
| 624 | # Cache key based on notification ID and current settings |
| 625 | # Cache for 5 minutes to balance freshness with performance |
| 626 | cache_key = f'dev_notif_condition_{notification.id}_{user.id}' |
| 627 | should_show = cache.get(cache_key) |
| 628 | |
| 629 | if should_show is None: |
| 630 | should_show = evaluate_conditions(conditions, user) |
| 631 | cache.set(cache_key, should_show, timeout=300) # 5 minutes |
| 632 | |
| 633 | if not should_show: |
| 634 | notifications_to_exclude.append(notification.id) |
| 635 | |
| 636 | if notifications_to_exclude: |
| 637 | queryset = queryset.exclude(id__in=notifications_to_exclude) |
| 638 | |
| 639 | return queryset |
| 640 | |
| 641 | def get_serializer_context(self): |
| 642 | context = super().get_serializer_context() |
no test coverage detected