views.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from django.http import JsonResponse
  2. from django.shortcuts import render
  3. from django.utils.translation import ugettext as _, ungettext
  4. from misago.core.decorators import ajax_only
  5. from misago.users.decorators import deny_guests
  6. @deny_guests
  7. def notifications(request):
  8. if request.is_ajax():
  9. return dropdown(request)
  10. else:
  11. return full_page(request)
  12. def dropdown(request):
  13. template = render(request, 'misago/notifications/dropdown.html', {
  14. 'items': request.user.notifications.order_by('-id').iterator(),
  15. 'notifications_count': request.user.notifications.count(),
  16. })
  17. return JsonResponse({
  18. 'is_error': False,
  19. 'count': request.user.new_notifications,
  20. 'html': template.content,
  21. })
  22. def full_page(request):
  23. return render(request, 'misago/notifications/full.html')
  24. @ajax_only
  25. @deny_guests
  26. def event_sender(request):
  27. if request.user.new_notifications:
  28. message = ungettext("You have %(notifications)s new notification",
  29. "You have %(notifications)s new notifications",
  30. request.user.new_notifications)
  31. message = message % {'notifications': request.user.new_notifications}
  32. else:
  33. message = _("Your notifications")
  34. return JsonResponse({
  35. 'is_error': False,
  36. 'count': request.user.new_notifications,
  37. 'message': message,
  38. })
  39. @deny_guests
  40. def new_notification(request):
  41. from django.contrib.auth import get_user_model
  42. from faker import Factory
  43. faker = Factory.create()
  44. sender = get_user_model().objects.order_by('?')[:1][0]
  45. from misago.notifications import notify_user
  46. notify_user(
  47. request.user,
  48. _("Replied to %(thread)s"),
  49. '/',
  50. 'test',
  51. formats={'thread': 'LoremIpsum'},
  52. sender=sender,)
  53. from django.http import HttpResponse
  54. return HttpResponse('Notification set.')