Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from django.core.urlresolvers import reverse
  2. from django.utils.translation import ugettext_lazy as _, ungettext
  3. from django.utils.timezone import now
  4. from django.contrib import messages
  5. from django import http
  6. from django.views import generic
  7. from django.db.models import get_model
  8. from oscar.views.generic import BulkEditMixin
  9. Notification = get_model('customer', 'Notification')
  10. class NotificationListView(generic.ListView):
  11. model = Notification
  12. template_name = 'customer/notifications/list.html'
  13. context_object_name = 'notifications'
  14. paginate_by = 20
  15. def get_context_data(self, **kwargs):
  16. ctx = super(NotificationListView, self).get_context_data(**kwargs)
  17. ctx['title'] = self.title
  18. ctx['list_type'] = self.list_type
  19. return ctx
  20. class InboxView(NotificationListView):
  21. title = _("Notifications inbox")
  22. list_type = 'inbox'
  23. def get_queryset(self):
  24. qs = self.model._default_manager.filter(
  25. recipient=self.request.user,
  26. location=self.model.INBOX)
  27. for obj in qs:
  28. if not obj.is_read:
  29. setattr(obj, 'is_new', True)
  30. self.mark_as_read(qs)
  31. return qs
  32. def mark_as_read(self, queryset):
  33. unread = queryset.filter(date_read=None)
  34. unread.update(date_read=now())
  35. class ArchiveView(NotificationListView):
  36. title = _("Archived notifications")
  37. list_type = 'archive'
  38. def get_queryset(self):
  39. return self.model._default_manager.filter(
  40. recipient=self.request.user,
  41. location=self.model.ARCHIVE)
  42. class DetailView(generic.DetailView):
  43. model = Notification
  44. template_name = 'customer/notifications/detail.html'
  45. context_object_name = 'notification'
  46. def get_queryset(self):
  47. return self.model._default_manager.filter(
  48. recipient=self.request.user)
  49. class UpdateView(BulkEditMixin, generic.RedirectView):
  50. model = Notification
  51. actions = ('archive', 'delete')
  52. checkbox_object_name = 'notification'
  53. def get_object_dict(self, ids):
  54. return self.model.objects.filter(
  55. recipient=self.request.user).in_bulk(ids)
  56. def get_success_response(self):
  57. default = reverse('customer:notifications-inbox')
  58. return http.HttpResponseRedirect(
  59. self.request.META.get('HTTP_REFERER', default))
  60. def archive(self, request, notifications):
  61. for notification in notifications:
  62. notification.archive()
  63. msg = ungettext(
  64. '%(count)d notification archived',
  65. '%(count)d notifications archived', len(notifications)) % {
  66. 'count': len(notifications)}
  67. messages.success(request, msg)
  68. return self.get_success_response()
  69. def delete(self, request, notifications):
  70. for notification in notifications:
  71. notification.delete()
  72. msg = ungettext(
  73. '%(count)d notification deleted',
  74. '%(count)d notifications deleted', len(notifications)) % {
  75. 'count': len(notifications)}
  76. messages.success(request, msg)
  77. return self.get_success_response()