views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from django.contrib import messages
  2. from django.db.transaction import atomic
  3. from django.http import JsonResponse
  4. from django.shortcuts import get_object_or_404, 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. return read_all(request)
  16. if request.POST.get('notification'):
  17. return read_notification(request)
  18. else:
  19. assert_real_new_notifications_count(request.user)
  20. if request.is_ajax():
  21. return dropdown(request)
  22. else:
  23. return full_page(request)
  24. def dropdown(request):
  25. template = render(request, 'misago/notifications/dropdown.html', {
  26. 'notifications_count': request.user.misago_notifications.count(),
  27. 'items': request.user.misago_notifications.order_by('-id')[:15],
  28. })
  29. return JsonResponse({
  30. 'is_error': False,
  31. 'count': request.user.new_notifications,
  32. 'html': template.content,
  33. })
  34. def full_page(request):
  35. return render(request, 'misago/notifications/full.html', {
  36. 'notifications_count': request.user.misago_notifications.count(),
  37. 'items': request.user.misago_notifications.order_by('-id'),
  38. })
  39. @atomic
  40. def go_to_notification(request, notification_id, hash):
  41. queryset = request.user.misago_notifications.select_for_update()
  42. notification = get_object_or_404(
  43. queryset, pk=notification_id, hash=hash)
  44. if notification.is_new:
  45. update_qs = request.user.misago_notifications.filter(hash=hash)
  46. update_qs.update(is_new=False)
  47. assert_real_new_notifications_count(request.user)
  48. return redirect(notification.url)
  49. @atomic
  50. def read_all(request):
  51. messages.success(request, _("All notifications were set as read."))
  52. read_all_user_alerts(request.user)
  53. @atomic
  54. def read_notification(request):
  55. try:
  56. queryset = request.user.misago_notifications
  57. notification = queryset.get(id=request.POST['notification'])
  58. if notification.is_new:
  59. is_changed = True
  60. with atomic():
  61. notification.is_new = False
  62. notification.save(update_fields=['is_new'])
  63. assert_real_new_notifications_count(request.user)
  64. else:
  65. is_changed = False
  66. if request.is_ajax():
  67. return JsonResponse({
  68. 'is_error': False,
  69. 'is_changed': is_changed
  70. })
  71. else:
  72. messages.success(request, _("Notification was marked as read."))
  73. return redirect('misago:notifications')
  74. except Notification.DoesNotExist:
  75. message = _("Specified notification could not be found.")
  76. if request.is_ajax():
  77. return JsonResponse({
  78. 'is_error': True,
  79. 'message': message,
  80. })
  81. else:
  82. messages.error(request, message)
  83. return redirect('misago:notifications')
  84. @atomic
  85. def read_all(request):
  86. if not request.is_ajax():
  87. messages.success(request, _("All notifications were set as read."))
  88. read_all_user_alerts(request.user)
  89. return redirect('misago:notifications')
  90. @uiview('notifications')
  91. @deny_guests
  92. def event_sender(request, resolver_match):
  93. if request.user.new_notifications:
  94. message = ungettext("%(notifications)s new notification",
  95. "%(notifications)s new notifications",
  96. request.user.new_notifications)
  97. message = message % {'notifications': request.user.new_notifications}
  98. else:
  99. message = _("Your notifications")
  100. return {
  101. 'count': request.user.new_notifications,
  102. 'message': message,
  103. }