Commit df375701804ecfdc3e61091321ed34bce2986c0b

Authored by fbormann
1 parent c62b0571

ajax call is working and notifications html are coming, I've separated notificat…

…ions into a template: notifications.html #158 #80
core/context_processors.py
@@ -5,6 +5,6 @@ def notifications(request): @@ -5,6 +5,6 @@ def notifications(request):
5 context['notifications'] = None 5 context['notifications'] = None
6 if request.user.is_authenticated: 6 if request.user.is_authenticated:
7 return { 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 return context 10 return context
core/static/js/base/amadeus.js
@@ -126,4 +126,14 @@ function validarCpfSemAlert(campo,nome,idElementoMensagemErro){ @@ -126,4 +126,14 @@ function validarCpfSemAlert(campo,nome,idElementoMensagemErro){
126 return true; 126 return true;
127 } 127 }
128 return retorno; 128 return retorno;
129 -}  
130 \ No newline at end of file 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
@@ -6,8 +6,8 @@ $(document).ready(function(){ @@ -6,8 +6,8 @@ $(document).ready(function(){
6 /* 6 /*
7 7
8 */ 8 */
9 -function loadNotifications(step){  
10 - $.ajax('/loadNotifications',{ 9 +function getNotifications(step){
  10 + $.ajax('/getNotifications',{
11 steps: step, 11 steps: step,
12 amount: 5, 12 amount: 5,
13 sucess: function(response){ 13 sucess: function(response){
core/templates/base.html
@@ -64,7 +64,7 @@ @@ -64,7 +64,7 @@
64 {% include "notifications.html" %} 64 {% include "notifications.html" %}
65 65
66 <li> 66 <li>
67 - <a> 67 + <a onclick="getNotifications(5)">
68 <div class="list-group-item"> 68 <div class="list-group-item">
69 <div class="row-content"> 69 <div class="row-content">
70 <p class="list-group-item-text">See More</p> 70 <p class="list-group-item-text">See More</p>
core/templates/notifications.html
1 {% for notification in notifications %} 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 \ No newline at end of file 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 \ No newline at end of file 33 \ No newline at end of file
@@ -9,5 +9,6 @@ urlpatterns = [ @@ -9,5 +9,6 @@ urlpatterns = [
9 url(r'^register/$', views.RegisterUser.as_view(), name='register'), 9 url(r'^register/$', views.RegisterUser.as_view(), name='register'),
10 url(r'^remember_password/$', views.remember_password, name='remember_password'), 10 url(r'^remember_password/$', views.remember_password, name='remember_password'),
11 url(r'^logout/$', auth_views.logout, {'next_page': 'core:home'}, name='logout'), 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 ]
@@ -5,6 +5,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin @@ -5,6 +5,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin
5 from .decorators import log_decorator 5 from .decorators import log_decorator
6 from django.contrib import messages 6 from django.contrib import messages
7 from django.shortcuts import render, redirect 7 from django.shortcuts import render, redirect
  8 +from django.template.loader import render_to_string
8 from django.views.generic import CreateView, UpdateView 9 from django.views.generic import CreateView, UpdateView
9 from django.http import HttpResponse, JsonResponse 10 from django.http import HttpResponse, JsonResponse
10 from django.core.mail import send_mail,BadHeaderError 11 from django.core.mail import send_mail,BadHeaderError
@@ -97,13 +98,23 @@ def processNotification(self, notificationId): @@ -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 else: #go to login page 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