您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

views.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.utils.html import strip_tags
  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.views import generic
  6. from oscar.core.loading import get_model
  7. from oscar.core.utils import redirect_to_referrer
  8. from oscar.apps.customer.mixins import PageTitleMixin
  9. from oscar.views.generic import BulkEditMixin
  10. Notification = get_model('customer', 'Notification')
  11. class NotificationListView(PageTitleMixin, generic.ListView):
  12. model = Notification
  13. template_name = 'customer/notifications/list.html'
  14. context_object_name = 'notifications'
  15. paginate_by = 20
  16. page_title = _("Notifications")
  17. active_tab = 'notifications'
  18. def get_context_data(self, **kwargs):
  19. ctx = super(NotificationListView, self).get_context_data(**kwargs)
  20. ctx['list_type'] = self.list_type
  21. return ctx
  22. class InboxView(NotificationListView):
  23. list_type = 'inbox'
  24. def get_queryset(self):
  25. qs = self.model._default_manager.filter(
  26. recipient=self.request.user,
  27. location=self.model.INBOX)
  28. # Mark unread notifications so they can be rendered differently...
  29. for obj in qs:
  30. if not obj.is_read:
  31. setattr(obj, 'is_new', True)
  32. # ...but then mark everything as read.
  33. self.mark_as_read(qs)
  34. return qs
  35. def mark_as_read(self, queryset):
  36. unread = queryset.filter(date_read=None)
  37. unread.update(date_read=now())
  38. class ArchiveView(NotificationListView):
  39. list_type = 'archive'
  40. def get_queryset(self):
  41. return self.model._default_manager.filter(
  42. recipient=self.request.user,
  43. location=self.model.ARCHIVE)
  44. class DetailView(PageTitleMixin, generic.DetailView):
  45. model = Notification
  46. template_name = 'customer/notifications/detail.html'
  47. context_object_name = 'notification'
  48. active_tab = 'notifications'
  49. def get_page_title(self):
  50. """Append subject to page title"""
  51. title = strip_tags(self.object.subject)
  52. return u'%s: %s' % (_('Notification'), title)
  53. def get_queryset(self):
  54. return self.model._default_manager.filter(
  55. recipient=self.request.user)
  56. class UpdateView(BulkEditMixin, generic.RedirectView):
  57. model = Notification
  58. actions = ('archive', 'delete')
  59. checkbox_object_name = 'notification'
  60. def get_object_dict(self, ids):
  61. return self.model.objects.filter(
  62. recipient=self.request.user).in_bulk(ids)
  63. def get_success_response(self):
  64. return redirect_to_referrer(
  65. self.request.META, 'customer:notifications-inbox')
  66. def archive(self, request, notifications):
  67. for notification in notifications:
  68. notification.archive()
  69. msg = ungettext(
  70. '%(count)d notification archived',
  71. '%(count)d notifications archived', len(notifications)) \
  72. % {'count': len(notifications)}
  73. messages.success(request, msg)
  74. return self.get_success_response()
  75. def delete(self, request, notifications):
  76. for notification in notifications:
  77. notification.delete()
  78. msg = ungettext(
  79. '%(count)d notification deleted',
  80. '%(count)d notifications deleted', len(notifications)) \
  81. % {'count': len(notifications)}
  82. messages.success(request, msg)
  83. return self.get_success_response()