diff --git a/forum/static/js/forum.js b/forum/static/js/forum.js index 5fa5148..a95b380 100644 --- a/forum/static/js/forum.js +++ b/forum/static/js/forum.js @@ -1,3 +1,20 @@ +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; +} + + /* * * Function to load forum to modal @@ -33,6 +50,21 @@ function showForum(url, forum_id) { $('#forumModal').modal(); } +function delete_post(url, post) { + var csrftoken = getCookie('csrftoken'); + + $.ajax({ + method: 'post', + beforeSend: function (request) { + request.setRequestHeader('X-CSRFToken', csrftoken); + }, + url: url, + success: function(data) { + $("#post_"+post).remove(); + } + }); +} + function answer(id, url) { $.ajax({ url: url, diff --git a/forum/templates/post/post_list.html b/forum/templates/post/post_list.html index d6af5e7..ee4066e 100644 --- a/forum/templates/post/post_list.html +++ b/forum/templates/post/post_list.html @@ -11,13 +11,14 @@ reply {% if request.user|has_role:'system_admin' or request.user|has_role:'professor' and request.user == post.user %} + {% csrf_token %}
{% endif %} diff --git a/forum/templates/post/post_render.html b/forum/templates/post/post_render.html index 3163aac..04c80be 100644 --- a/forum/templates/post/post_render.html +++ b/forum/templates/post/post_render.html @@ -9,13 +9,14 @@ reply {% if request.user|has_role:'system_admin' or request.user|has_role:'professor' and request.user == post.user %} + {% csrf_token %} {% endif %} diff --git a/forum/urls.py b/forum/urls.py index 3d68afa..9163164 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -8,7 +8,9 @@ urlpatterns = [ url(r'^create$', views.CreateForumView.as_view(), name='create'), url(r'^posts$', views.PostIndex.as_view(), name='posts'), url(r'^create_post$', views.CreatePostView.as_view(), name='create_post'), - url(r'^render+post/([\w_-]+)/$', views.render_post, name='render_post'), + url(r'^render_post/([\w_-]+)/$', views.render_post, name='render_post'), + url(r'^delete_post/(?P