views.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.uiviews import uiview
  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. @uiview('misago_notifications')
  25. @deny_guests
  26. def event_sender(request, resolver_match):
  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 {
  35. 'count': request.user.new_notifications,
  36. 'message': message,
  37. }
  38. @deny_guests
  39. def new_notification(request):
  40. from django.contrib.auth import get_user_model
  41. from faker import Factory
  42. faker = Factory.create()
  43. sender = get_user_model().objects.order_by('?')[:1][0]
  44. from misago.notifications import notify_user
  45. notify_user(
  46. request.user,
  47. _("Replied to %(thread)s"),
  48. '/',
  49. 'test',
  50. formats={'thread': 'LoremIpsum'},
  51. sender=sender,)
  52. from django.http import HttpResponse
  53. return HttpResponse('Notification set.')