|
@@ -1,14 +1,21 @@
|
|
from django.db.models import F
|
|
from django.db.models import F
|
|
-from django.db.transaction import atomic
|
|
|
|
|
|
+from django.db import transaction
|
|
from django.utils.html import escape
|
|
from django.utils.html import escape
|
|
|
|
|
|
from misago.notifications.models import Notification
|
|
from misago.notifications.models import Notification
|
|
|
|
+from misago.notifications.utils import hash_trigger
|
|
|
|
|
|
|
|
|
|
-__all__ = ['notify_user']
|
|
|
|
|
|
+__all__ = [
|
|
|
|
+ 'notify_user',
|
|
|
|
+ 'read_user_notification',
|
|
|
|
+ 'read_all_user_alerts',
|
|
|
|
+ 'assert_real_new_notifications_count',
|
|
|
|
+]
|
|
|
|
|
|
|
|
|
|
-def notify_user(user, message, url, trigger, formats=None, sender=None):
|
|
|
|
|
|
+def notify_user(user, message, url, trigger, formats=None, sender=None,
|
|
|
|
+ update_user=True):
|
|
message_escaped = escape(message)
|
|
message_escaped = escape(message)
|
|
if formats:
|
|
if formats:
|
|
final_formats = {}
|
|
final_formats = {}
|
|
@@ -17,7 +24,7 @@ def notify_user(user, message, url, trigger, formats=None, sender=None):
|
|
message_escaped = message_escaped % final_formats
|
|
message_escaped = message_escaped % final_formats
|
|
|
|
|
|
new_notification = Notification(user=user,
|
|
new_notification = Notification(user=user,
|
|
- trigger=trigger,
|
|
|
|
|
|
+ trigger=hash_trigger(trigger),
|
|
url=url,
|
|
url=url,
|
|
message=message_escaped)
|
|
message=message_escaped)
|
|
|
|
|
|
@@ -28,12 +35,45 @@ def notify_user(user, message, url, trigger, formats=None, sender=None):
|
|
|
|
|
|
new_notification.save()
|
|
new_notification.save()
|
|
user.new_notifications = F('new_notifications') + 1
|
|
user.new_notifications = F('new_notifications') + 1
|
|
|
|
+ if update_user:
|
|
|
|
+ user.save(update_fields=['new_notifications'])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def read_user_notification(user, trigger, atomic=True):
|
|
|
|
+ if user.new_notifications:
|
|
|
|
+ if atomic:
|
|
|
|
+ with transaction.atomic():
|
|
|
|
+ _real_read_user_notification(user, trigger)
|
|
|
|
+ else:
|
|
|
|
+ _real_read_user_notification(user, trigger)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def _real_read_user_notification(user, trigger):
|
|
|
|
+ trigger_hash = hash_trigger(trigger)
|
|
|
|
+ update_qs = user.notifications.filter(is_new=True)
|
|
|
|
+ update_qs = update_qs.filter(trigger=trigger_hash)
|
|
|
|
+ updated = update_qs.update(is_new=False)
|
|
|
|
+
|
|
|
|
+ if updated:
|
|
|
|
+ user.new_notifications -= updated
|
|
|
|
+ if user.new_notifications < 0:
|
|
|
|
+ # Cos no. of changed rows returned via update()
|
|
|
|
+ # isn't always accurate
|
|
|
|
+ user.new_notifications = 0
|
|
user.save(update_fields=['new_notifications'])
|
|
user.save(update_fields=['new_notifications'])
|
|
|
|
|
|
|
|
|
|
-#def read_user_notifications(user, trigger):
|
|
|
|
-# pass
|
|
|
|
|
|
+def read_all_user_alerts(user):
|
|
|
|
+ locked_user = user.lock()
|
|
|
|
+ user.new_notifications = 0
|
|
|
|
+ locked_user.new_notifications = 0
|
|
|
|
+ locked_user.save(update_fields=['new_notifications'])
|
|
|
|
+ locked_user.notifications.update(is_new=False)
|
|
|
|
|
|
|
|
|
|
-#def read_all_user_alerts(user):
|
|
|
|
-# pass
|
|
|
|
|
|
+def assert_real_new_notifications_count(user):
|
|
|
|
+ if user.new_notifications:
|
|
|
|
+ real_new_count = user.notifications.filter(is_new=True).count()
|
|
|
|
+ if real_new_count != user.new_notifications:
|
|
|
|
+ user.new_notifications = real_new_count
|
|
|
|
+ user.save(update_fields=['new_notifications'])
|