views.py 1.8 KB

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