Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

views.py 3.5KB

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