api.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from django.db.models import F
  2. from django.db.transaction import atomic
  3. from django.utils.html import escape
  4. from misago.notifications.models import Notification
  5. __all__ = ['notify_user']
  6. def notify_user(user, message, url, trigger, formats=None, sender=None):
  7. message_escaped = escape(message)
  8. if formats:
  9. final_formats = {}
  10. for format, replace in formats.items():
  11. final_formats[format] = '<strong>%s</strong>' % escape(replace)
  12. message_escaped = message_escaped % final_formats
  13. new_notification = Notification(user=user,
  14. trigger=trigger,
  15. url=url,
  16. message=message_escaped)
  17. if sender:
  18. new_notification.sender = sender
  19. new_notification.sender_username = sender.username
  20. new_notification.sender_slug = sender.slug
  21. new_notification.save()
  22. user.new_notifications = F('new_notifications') + 1
  23. user.save(update_fields=['new_notifications'])
  24. #def read_user_notifications(user, trigger):
  25. # pass
  26. #def read_all_user_alerts(user):
  27. # pass