views.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from django.contrib import messages
  2. from django.db.transaction import atomic
  3. from django.http import JsonResponse
  4. from django.shortcuts import redirect, render
  5. from django.utils.translation import ugettext as _, ungettext
  6. from misago.core.uiviews import uiview
  7. from misago.users.decorators import deny_guests
  8. from misago.notifications import (read_all_user_alerts,
  9. assert_real_new_notifications_count)
  10. from misago.notifications.models import Notification
  11. @deny_guests
  12. def notifications(request):
  13. if request.method == 'POST':
  14. if 'read-all' in request.POST:
  15. read_all(request)
  16. if not request.is_ajax():
  17. return redirect('misago:notifications')
  18. if request.POST.get('notification'):
  19. return read_notification(request)
  20. else:
  21. assert_real_new_notifications_count(request.user)
  22. if request.is_ajax():
  23. return dropdown(request)
  24. else:
  25. return full_page(request)
  26. def dropdown(request):
  27. template = render(request, 'misago/notifications/dropdown.html', {
  28. 'notifications_count': request.user.misago_notifications.count(),
  29. 'items': request.user.misago_notifications.order_by('-id')[:15],
  30. })
  31. return JsonResponse({
  32. 'is_error': False,
  33. 'count': request.user.new_notifications,
  34. 'html': template.content,
  35. })
  36. def full_page(request):
  37. return render(request, 'misago/notifications/full.html', {
  38. 'notifications_count': request.user.misago_notifications.count(),
  39. 'items': request.user.misago_notifications.order_by('-id'),
  40. })
  41. @atomic
  42. def read_all(request):
  43. if not request.is_ajax():
  44. messages.success(request, _("All notifications were set as read."))
  45. read_all_user_alerts(request.user)
  46. def read_notification(request):
  47. try:
  48. queryset = request.user.misago_notifications
  49. notification = queryset.get(id=request.POST['notification'])
  50. if notification.is_new:
  51. is_changed = True
  52. with atomic():
  53. notification.is_new = False
  54. notification.save(update_fields=['is_new'])
  55. assert_real_new_notifications_count(request.user)
  56. else:
  57. is_changed = False
  58. if request.is_ajax():
  59. return JsonResponse({
  60. 'is_error': False,
  61. 'is_changed': is_changed
  62. })
  63. else:
  64. messages.success(request, _("Notification was marked as read."))
  65. return redirect('misago:notifications')
  66. except Notification.DoesNotExist:
  67. message = _("Specified notification could not be found.")
  68. if request.is_ajax():
  69. return JsonResponse({
  70. 'is_error': True,
  71. 'message': message,
  72. })
  73. else:
  74. messages.error(request, message)
  75. return redirect('misago:notifications')
  76. @uiview('misago_notifications')
  77. @deny_guests
  78. def event_sender(request, resolver_match):
  79. if request.user.new_notifications:
  80. message = ungettext("You have %(notifications)s new notification",
  81. "You have %(notifications)s new notifications",
  82. request.user.new_notifications)
  83. message = message % {'notifications': request.user.new_notifications}
  84. else:
  85. message = _("Your notifications")
  86. return {
  87. 'count': request.user.new_notifications,
  88. 'message': message,
  89. }