| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 | import datetime
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from django import http
from django.views import generic
from django.db.models import get_model
from oscar.views.generic import BulkEditMixin
Notification = get_model('customer', 'Notification')
class NotificationListView(generic.ListView):
    model = Notification
    template_name = 'notifications/list.html'
    context_object_name = 'notifications'
    paginate_by = 20
    def get_context_data(self, **kwargs):
        ctx = super(NotificationListView, self).get_context_data(**kwargs)
        ctx['title'] = self.title
        ctx['list_type'] = self.list_type
        return ctx
class InboxView(NotificationListView):
    title = _("Notifications inbox")
    list_type = 'inbox'
    def get_queryset(self):
        qs = self.model._default_manager.filter(
            recipient=self.request.user,
            location=self.model.INBOX)
        for obj in qs:
            if not obj.is_read:
                setattr(obj, 'is_new', True)
        self.mark_as_read(qs)
        return qs
    def mark_as_read(self, queryset):
        now = datetime.datetime.now()
        unread = queryset.filter(date_read=None)
        unread.update(date_read=now)
class ArchiveView(NotificationListView):
    title = _("Archived notifications")
    list_type = 'archive'
    def get_queryset(self):
        return self.model._default_manager.filter(
            recipient=self.request.user,
            location=self.model.ARCHIVE)
class DetailView(generic.DetailView):
    model = Notification
    template_name = 'notifications/detail.html'
    context_object_name = 'notification'
    def get_queryset(self):
        return self.model._default_manager.filter(
            recipient=self.request.user)
class UpdateView(BulkEditMixin, generic.RedirectView):
    model = Notification
    actions = ('archive', 'delete')
    checkbox_object_name = 'notification'
    def get_object_dict(self, ids):
        return self.model.objects.filter(
            recipient=self.request.user).in_bulk(ids)
    def get_success_response(self):
        default = reverse('customer:notifications-inbox')
        return http.HttpResponseRedirect(
            self.request.META.get('HTTP_REFERER', default))
    def archive(self, request, notifications):
        for notification in notifications:
            notification.archive()
        messages.success(request, "%d messages archived" % len(notifications))
        return self.get_success_response()
    def delete(self, request, notifications):
        for notification in notifications:
            notification.delete()
        messages.success(request, "%d messages deleted" % len(notifications))
        return self.get_success_response()
 |