浏览代码

Add notifications to site header

master
David Winterbottom 13 年前
父节点
当前提交
793f2df5cf

+ 9
- 2
oscar/apps/customer/notification_views.py 查看文件

@@ -19,28 +19,35 @@ class NotificationListView(generic.ListView):
19 19
     paginate_by = 20
20 20
 
21 21
     def get_context_data(self, **kwargs):
22
-        ctx = super(InboxView, self).get_context_data(**kwargs)
22
+        ctx = super(NotificationListView, self).get_context_data(**kwargs)
23 23
         ctx['title'] = self.title
24
+        ctx['list_type'] = self.list_type
24 25
         return ctx
25 26
 
26 27
 
27 28
 class InboxView(NotificationListView):
28 29
     title = _("Notifications inbox")
30
+    list_type = 'inbox'
29 31
 
30 32
     def get_queryset(self):
31 33
         qs = self.model._default_manager.filter(
32 34
             recipient=self.request.user,
33 35
             location=self.model.INBOX)
36
+        for obj in qs:
37
+            if not obj.is_read:
38
+                setattr(obj, 'is_new', True)
34 39
         self.mark_as_read(qs)
35 40
         return qs
36 41
 
37 42
     def mark_as_read(self, queryset):
38 43
         now = datetime.datetime.now()
39
-        queryset.update(date_read=now)
44
+        unread = queryset.filter(date_read=None)
45
+        unread.update(date_read=now)
40 46
 
41 47
 
42 48
 class ArchiveView(NotificationListView):
43 49
     title = _("Archived notifications")
50
+    list_type = 'archive'
44 51
 
45 52
     def get_queryset(self):
46 53
         return self.model._default_manager.filter(

+ 9
- 0
oscar/apps/notifications/context_processors.py 查看文件

@@ -0,0 +1,9 @@
1
+from django.db.models import get_model
2
+
3
+Notification = get_model('notifications', 'Notification')
4
+
5
+
6
+def notifications(request):
7
+    num_unread = Notification.objects.filter(
8
+        recipient=request.user, date_read=None).count()
9
+    return {'num_unread_notifications': num_unread}

+ 4
- 0
oscar/apps/notifications/models.py 查看文件

@@ -36,3 +36,7 @@ class Notification(models.Model):
36 36
     def archive(self):
37 37
         self.location = self.ARCHIVE
38 38
         self.save()
39
+
40
+    @property
41
+    def is_read(self):
42
+        return self.date_read is not None

+ 6
- 4
oscar/templates/oscar/notifications/list.html 查看文件

@@ -27,10 +27,10 @@
27 27
 
28 28
 {% block content %}
29 29
 
30
-<p>
31
-	<a class="btn" href="{% url customer:notifications-inbox %}">Inbox</a>
32
-	<a class="btn" href="{% url customer:notifications-archive %}">Archive</a>
33
-</p>
30
+<ul class="nav nav-tabs">
31
+	<li class="{% if list_type == 'inbox' %}active{% endif %}"><a href="{% url customer:notifications-inbox %}">Inbox</a></li>
32
+	<li class="{% if list_type == 'archive' %}active{% endif %}"><a href="{% url customer:notifications-archive %}">Archive</a></li>
33
+</ul>
34 34
 
35 35
 {% if notifications %}
36 36
 {% if page_obj %}
@@ -48,6 +48,7 @@
48 48
 				value="{{ notification.id }}"/>
49 49
 			</td>
50 50
 			<td>
51
+				{% if notification.is_new %}*NEW*{% endif %}
51 52
 				{{ notification.subject|safe }} <br/>
52 53
 				Send: {{ notification.date_sent }}<br/>
53 54
 				Read: {{ notification.date_read }}
@@ -56,6 +57,7 @@
56 57
 		{% endfor %}
57 58
 		</tbody>
58 59
 	</table>
60
+	With selected items: 
59 61
 	<button type="submit" class="btn" name="action" value="archive">Archive</button>
60 62
 	<button type="submit" class="btn" name="action" value="delete">Delete</button>
61 63
 </form>

+ 15
- 6
oscar/templates/oscar/partials/nav_accounts.html 查看文件

@@ -5,17 +5,26 @@
5 5
 			<ul class="nav pull-right">
6 6
 				{% if user.is_authenticated %}
7 7
 				    {% if user.get_full_name %}
8
-					<li><div>{% trans "Welcome" %} <em>{{ user.get_full_name }}</em></div></li>
8
+						<li><div>{% trans "Welcome" %} <em>{{ user.get_full_name }}</em></div></li>
9 9
 					{% endif %}
10
-					<li> <a href="{% url customer:summary %}" class="app-ico ico_profile">{% trans "Account" %}</a></li>
11
-					<li><a href="{% url customer:logout %}" class="app-ico ico_logout">{% trans "Logout" %}</a></li>
10
+					<li>
11
+						<a href="{% url customer:summary %}" class="app-ico ico_profile">{% trans "Account" %}</a>
12
+					</li>
13
+					<li>
14
+						<a class="app-ico ico_email" href="{% url customer:notifications-inbox %}">
15
+							{% trans "Notifications" %}
16
+							{% if num_unread_notifications > 0 %}
17
+								<span class="label label-important">{{ num_unread_notifications }}</span>
18
+							{% endif %}</a>
19
+					</li>
12 20
 					{% if user.is_staff %}
13
-					<li><a href="{% url dashboard:index %}" class="app-ico ico_settings">{% trans "Dashboard" %}</a></li>
21
+						<li><a href="{% url dashboard:index %}" class="app-ico ico_settings">{% trans "Dashboard" %}</a></li>
14 22
 					{% endif %}
23
+					<li><a href="{% url customer:logout %}" class="app-ico ico_logout">{% trans "Logout" %}</a></li>
15 24
 				{% else %}
16 25
 					<li><a href="{% url customer:login %}" class="app-ico ico_logout">{% trans "Login or register" %}</a></li>
17 26
 				{% endif %}
18 27
 			</ul>
19 28
 		</div>
20
-    </div><!-- /navbar-inner -->
21
-</div><!-- /navbar -->
29
+    </div>
30
+</div>

+ 1
- 0
sites/sandbox/settings.py 查看文件

@@ -108,6 +108,7 @@ TEMPLATE_CONTEXT_PROCESSORS = (
108 108
     'oscar.apps.promotions.context_processors.promotions',
109 109
     'oscar.apps.checkout.context_processors.checkout',
110 110
     'oscar.core.context_processors.metadata',
111
+    'oscar.apps.notifications.context_processors.notifications',
111 112
 )
112 113
 
113 114
 MIDDLEWARE_CLASSES = (

正在加载...
取消
保存