views.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. def event_sender(request, resolver_match):
  26. if request.user.new_notifications:
  27. message = ungettext("You have %(notifications)s new notification",
  28. "You have %(notifications)s new notifications",
  29. request.user.new_notifications)
  30. message = message % {'notifications': request.user.new_notifications}
  31. else:
  32. message = _("Your notifications")
  33. return {
  34. 'count': request.user.new_notifications,
  35. 'message': message,
  36. }
  37. @deny_guests
  38. def new_notification(request):
  39. from django.contrib.auth import get_user_model
  40. from faker import Factory
  41. faker = Factory.create()
  42. sender = get_user_model().objects.order_by('?')[:1][0]
  43. from misago.notifications import notify_user
  44. notify_user(
  45. request.user,
  46. _("Replied to %(thread)s"),
  47. '/',
  48. 'test',
  49. formats={'thread': 'LoremIpsum'},
  50. sender=sender,)
  51. from django.http import HttpResponse
  52. return HttpResponse('Notification set.')