32 lines
976 B
Python
32 lines
976 B
Python
|
|
"""
|
||
|
|
Notification helpers for preference checks.
|
||
|
|
"""
|
||
|
|
from typing import Optional
|
||
|
|
|
||
|
|
from .models import Notification, NotificationPreference
|
||
|
|
|
||
|
|
|
||
|
|
def get_notification_preference(user) -> Optional[NotificationPreference]:
|
||
|
|
if not user:
|
||
|
|
return None
|
||
|
|
preference, _ = NotificationPreference.objects.get_or_create(user=user)
|
||
|
|
return preference
|
||
|
|
|
||
|
|
|
||
|
|
def should_notify(user, notification_type: str) -> bool:
|
||
|
|
"""Check if user has enabled notification type."""
|
||
|
|
preference = get_notification_preference(user)
|
||
|
|
if not preference:
|
||
|
|
return False
|
||
|
|
if notification_type == Notification.Type.PRICE_ALERT:
|
||
|
|
return preference.enable_price_alert
|
||
|
|
if notification_type in (
|
||
|
|
Notification.Type.BOUNTY_ACCEPTED,
|
||
|
|
Notification.Type.BOUNTY_COMPLETED,
|
||
|
|
Notification.Type.NEW_COMMENT,
|
||
|
|
):
|
||
|
|
return preference.enable_bounty
|
||
|
|
if notification_type == Notification.Type.SYSTEM:
|
||
|
|
return preference.enable_system
|
||
|
|
return True
|