Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

views.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import datetime
  2. from django.core.urlresolvers import reverse
  3. from django.utils.translation import ugettext_lazy as _
  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 = '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. now = datetime.datetime.now()
  34. unread = queryset.filter(date_read=None)
  35. unread.update(date_read=now)
  36. class ArchiveView(NotificationListView):
  37. title = _("Archived notifications")
  38. list_type = 'archive'
  39. def get_queryset(self):
  40. return self.model._default_manager.filter(
  41. recipient=self.request.user,
  42. location=self.model.ARCHIVE)
  43. class DetailView(generic.DetailView):
  44. model = Notification
  45. template_name = 'notifications/detail.html'
  46. context_object_name = 'notification'
  47. def get_queryset(self):
  48. return self.model._default_manager.filter(
  49. recipient=self.request.user)
  50. class UpdateView(BulkEditMixin, generic.RedirectView):
  51. model = Notification
  52. actions = ('archive', 'delete')
  53. checkbox_object_name = 'notification'
  54. def get_object_dict(self, ids):
  55. return self.model.objects.filter(
  56. recipient=self.request.user).in_bulk(ids)
  57. def get_success_response(self):
  58. default = reverse('customer:notifications-inbox')
  59. return http.HttpResponseRedirect(
  60. self.request.META.get('HTTP_REFERER', default))
  61. def archive(self, request, notifications):
  62. for notification in notifications:
  63. notification.archive()
  64. messages.success(request, "%d messages archived" % len(notifications))
  65. return self.get_success_response()
  66. def delete(self, request, notifications):
  67. for notification in notifications:
  68. notification.delete()
  69. messages.success(request, "%d messages deleted" % len(notifications))
  70. return self.get_success_response()