瀏覽代碼

notifs: système fonctionnel

omassot 7 年之前
父節點
當前提交
a8e04c479d
共有 4 個文件被更改,包括 96 次插入6 次删除
  1. 75 0
      main/static/js/custom.js
  2. 9 1
      main/templates/_layout.html
  3. 3 0
      main/urls.py
  4. 9 5
      main/views.py

+ 75 - 0
main/static/js/custom.js

@@ -95,5 +95,80 @@ $(document).ready( function () {
 		}
 	});
 	
+	function csrfSafeMethod(method) {
+	    // these HTTP methods do not require CSRF protection
+	    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
+	}
+	function getCookie(name) {
+	    var cookieValue = null;
+	    if (document.cookie && document.cookie !== '') {
+	        var cookies = document.cookie.split(';');
+	        for (var i = 0; i < cookies.length; i++) {
+	            var cookie = jQuery.trim(cookies[i]);
+	            // Does this cookie string begin with the name we want?
+	            if (cookie.substring(0, name.length + 1) === (name + '=')) {
+	                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+	                break;
+	            }
+	        }
+	    }
+	    return cookieValue;
+	}
+	var csrftoken = getCookie('csrftoken');
+	
+	$(".notif-seen").click(function(event) {
+		event.preventDefault();
+		var notif = $(this).closest('.notif');
+		var notif_id = notif.data('id');
+		
+        $.ajax({
+            type: "POST",
+            url: "/notif/seen/" + notif_id + "/",
+            data: '{notif_id:' + notif_id + '}',
+            contentType: "application/json; charset=utf-8",
+            dataType: "json",
+            beforeSend: function(xhr, settings) {
+                if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
+                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
+                }
+            },
+            success: function (response) {
+            	notif.remove();
+            },
+            failure: function (response) {
+                alert(response.responseText);
+            },
+            error: function (response) {
+                alert(response.responseText);
+            }
+        });
+	});
+	
+	$(".notif-all-seen").click(function(event) {
+		event.preventDefault();
+		var notif_list = $('#notif-dropdown').find('.notif-list');
+		if (confirm('Êtes vous sûr de vouloir faire disparaitre toutes les notifications ?')) {
+			$.ajax({
+				type: "POST",
+				url: "/notif/allseen/",
+				contentType: "application/json; charset=utf-8",
+				dataType: "json",
+				beforeSend: function(xhr, settings) {
+					if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
+						xhr.setRequestHeader("X-CSRFToken", csrftoken);
+					}
+				},
+				success: function (response) {
+					notif_list.html('');
+				},
+				failure: function (response) {
+					alert(response.responseText);
+				},
+				error: function (response) {
+					alert(response.responseText);
+				}
+			});
+		}
+	});
 	
 });

+ 9 - 1
main/templates/_layout.html

@@ -63,10 +63,13 @@
 	        				<i class="fa fa-bell"></i>{{ notif_unread_count }} <i class="fa fa-caret-down" style="margin-left: 0.5em;"></i>
 	        			</a>
 	        		
+	        			
 						<div id="notif-dropdown" class="dropdown-content">
+						
+						{% if notif_unread_count > 0 %}
 							<ul class="alt notif-list" style="">
 							{% for notification in request.user.notifications.unread %}
-								<li class="notif">
+								<li class="notif" data-id='{{ notification.id }}'>
 									<span class="notif-head">
 										<i class="flex-extend">Il y a {{ notification.timesince }}</i>
 										<a href="" class="notif-seen"><i class="fa fa-times"></i></a>
@@ -80,6 +83,11 @@
 							<div class="notif-footer">
 								<a href="" class="notif-all-seen">Marquer tout comme vu</a>
 							</div>
+						{% else %}
+							<div class="notif-footer">
+								<i>Aucune notification à afficher</i>
+							</div>
+						{% endif %}
 						</div>
 					</div>
 	        		

+ 3 - 0
main/urls.py

@@ -40,4 +40,7 @@ urlpatterns = [
     path('comment-edit/<int:comment_id>/', views.comment_edit, name='comment_edit'),
     path('comment-del/<int:comment_id>/', views.comment_del, name='comment_del'),
     path('search/', views.search, name='search'),
+    path('notif/seen/<int:notif_id>/', views.notif_seen, name='search'),
+    path('notif/allseen/', views.notif_all_seen, name='search'),
+    
 ]

+ 9 - 5
main/views.py

@@ -13,6 +13,7 @@ from notifications.signals import notify
 from main.forms import StoryForm, EpicForm, RegisterForm, ProfileForm, \
     CommentForm, SprintForm
 from main.models import Story, Epic, Sprint, Comment, Project
+from notifications.models import Notification
 
 
 def register(request):
@@ -364,12 +365,15 @@ def search(request):
         return render(request, 'search_results.html', {'results': results, 'pages': range(1, paginator.num_pages + 1)})
 
 @login_required
-def notif_seen(request):
-    pass
+def notif_seen(request, notif_id):
+    notif = get_object_or_404(Notification, id=notif_id)
+    notif.mark_as_read()
+    return HttpResponse('{}')
 
 @login_required
-def all_notif_seen(request):
-    pass
-
+def notif_all_seen(request):
+    for notif in request.user.notifications.all():
+        notif.mark_as_read()
+    return HttpResponse('{}')