Commit df375701804ecfdc3e61091321ed34bce2986c0b
1 parent
c62b0571
Exists in
master
and in
5 other branches
ajax call is working and notifications html are coming, I've separated notificat…
…ions into a template: notifications.html #158 #80
Showing
7 changed files
with
61 additions
and
39 deletions
Show diff stats
core/context_processors.py
... | ... | @@ -5,6 +5,6 @@ def notifications(request): |
5 | 5 | context['notifications'] = None |
6 | 6 | if request.user.is_authenticated: |
7 | 7 | return { |
8 | - 'notifications': Notification.objects.filter(user= request.user, read=False).order_by('-datetime') | |
8 | + 'notifications': Notification.objects.filter(user= request.user, read=False).order_by('-datetime')[0:5] | |
9 | 9 | } |
10 | 10 | return context | ... | ... |
core/static/js/base/amadeus.js
... | ... | @@ -126,4 +126,14 @@ function validarCpfSemAlert(campo,nome,idElementoMensagemErro){ |
126 | 126 | return true; |
127 | 127 | } |
128 | 128 | return retorno; |
129 | -} | |
130 | 129 | \ No newline at end of file |
130 | +} | |
131 | + | |
132 | +/* | |
133 | +This functions get the next 5 notifications from the user given a "step"(an amount) of previous notifications | |
134 | +*/ | |
135 | +function getNotifications(step){ | |
136 | + $.get('/getNotifications', | |
137 | + {'steps':step, 'amount': 5}, function(data){ | |
138 | + console.log(data); | |
139 | + }); | |
140 | +} | ... | ... |
core/static/js/base/header.js
core/templates/base.html
core/templates/notifications.html
1 | 1 | {% for notification in notifications %} |
2 | - {% if notification.actor %} <!-- if the notification has a user--> | |
3 | - <li> | |
4 | - <a href="{% url 'core:notification_read' notification.id %}"><div class="list-group-item"> | |
5 | - <div class="row-picture"> | |
6 | - <img class="circle" src="http://lorempixel.com/56/56/people/1" alt="icon"> | |
7 | - <div class="least-content pull-right">{{ notification.datetime }}</div> | |
8 | - </div> | |
9 | - <div class="row-content"> | |
10 | - <p class="list-group-item-text">{{ notification.message }}</p> | |
11 | - </div> | |
12 | - </div> | |
13 | - </a> | |
14 | - </li> | |
15 | - {% else %} | |
16 | - <li> | |
17 | - <a href="{% url 'core:notification_read' notification.id %}"> | |
18 | - <div class="list-group-item"> | |
19 | - <div class="row-action-primary"> | |
20 | - <i class="material-icons">folder</i> | |
21 | - </div> | |
22 | - <div class="row-content"> | |
2 | + {% if notification.actor %} <!-- if the notification has a user--> | |
3 | + <li> | |
4 | + <a href="{% url 'core:notification_read' notification.id %}"><div class="list-group-item"> | |
5 | + <div class="row-picture"> | |
6 | + <img class="circle" src="http://lorempixel.com/56/56/people/1" alt="icon"> | |
7 | + <div class="least-content pull-right">{{ notification.datetime }}</div> | |
8 | + </div> | |
9 | + <div class="row-content"> | |
10 | + <p class="list-group-item-text">{{ notification.message }}</p> | |
11 | + </div> | |
12 | + </div> | |
13 | + </a> | |
14 | + </li> | |
15 | + {% else %} | |
16 | + <li> | |
17 | + <a href="{% url 'core:notification_read' notification.id %}"> | |
18 | + <div class="list-group-item"> | |
19 | + <div class="row-action-primary"> | |
20 | + <i class="material-icons">folder</i> | |
21 | + </div> | |
22 | + <div class="row-content"> | |
23 | 23 | |
24 | - <div class="least-content pull-right">{{ notification.datetime }}</div> | |
24 | + <div class="least-content pull-right">{{ notification.datetime }}</div> | |
25 | 25 | |
26 | - <p class="list-group-item-text">{{ notification.message }}</p> | |
27 | - </div> | |
28 | - </a> | |
29 | - </li> | |
30 | - {% endif %} | |
31 | - {% endfor %} | |
32 | 26 | \ No newline at end of file |
27 | + <p class="list-group-item-text">{{ notification.message }}</p> | |
28 | + </div> | |
29 | + </a> | |
30 | + </li> | |
31 | + {% endif %} | |
32 | +{% endfor %} | |
33 | 33 | \ No newline at end of file | ... | ... |
core/urls.py
... | ... | @@ -9,5 +9,6 @@ urlpatterns = [ |
9 | 9 | url(r'^register/$', views.RegisterUser.as_view(), name='register'), |
10 | 10 | url(r'^remember_password/$', views.remember_password, name='remember_password'), |
11 | 11 | url(r'^logout/$', auth_views.logout, {'next_page': 'core:home'}, name='logout'), |
12 | - url(r'^notification/([0-9]+)/$', views.processNotification, name='notification_read') | |
12 | + url(r'^notification/([0-9]+)/$', views.processNotification, name='notification_read'), | |
13 | + url(r'^getNotifications/$', views.getNotifications, name='getNotifications'), | |
13 | 14 | ] | ... | ... |
core/views.py
... | ... | @@ -5,6 +5,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin |
5 | 5 | from .decorators import log_decorator |
6 | 6 | from django.contrib import messages |
7 | 7 | from django.shortcuts import render, redirect |
8 | +from django.template.loader import render_to_string | |
8 | 9 | from django.views.generic import CreateView, UpdateView |
9 | 10 | from django.http import HttpResponse, JsonResponse |
10 | 11 | from django.core.mail import send_mail,BadHeaderError |
... | ... | @@ -97,13 +98,23 @@ def processNotification(self, notificationId): |
97 | 98 | |
98 | 99 | |
99 | 100 | |
100 | -def getNotifications(self, amount, step=0): | |
101 | - if self.request.is_ajax and self.request.user.is_authenticated: | |
102 | - notifications = Notification.objects.filter(user= self.request.user, read=False).orderby('-datetime')[step:amount] | |
101 | + | |
102 | +def getNotifications(request): | |
103 | + context = {} | |
104 | + if request.user.is_authenticated: | |
105 | + | |
106 | + steps = int(request.GET['steps']) | |
107 | + amount = int(request.GET['amount']) | |
108 | + notifications = Notification.objects.filter(user= request.user, read=False).order_by('-datetime')[steps:steps+amount] | |
109 | + context['notifications'] = notifications | |
103 | 110 | else: #go to login page |
104 | - return response('teste') | |
111 | + return HttpResponse('teste') | |
105 | 112 | |
106 | - return JsonResponse(notifications) | |
113 | + | |
114 | + html = render_to_string("notifications.html", context) | |
115 | + print(html) | |
116 | + return HttpResponse(html) | |
117 | + | |
107 | 118 | |
108 | 119 | |
109 | 120 | ... | ... |