Commit 80ac5e86ad9672725466dcb3339a9b43995eb4c6
1 parent
82e90bd1
Exists in
master
and in
5 other branches
Adding post answer deletion [Issue: #165]
Showing
5 changed files
with
47 additions
and
5 deletions
Show diff stats
forum/static/js/forum.js
| @@ -212,6 +212,11 @@ function delete_post(url, post) { | @@ -212,6 +212,11 @@ function delete_post(url, post) { | ||
| 212 | }); | 212 | }); |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | +/* | ||
| 216 | +* | ||
| 217 | +* Function to load answer post form and set the submit function | ||
| 218 | +* | ||
| 219 | +*/ | ||
| 215 | function answer(id, url) { | 220 | function answer(id, url) { |
| 216 | $.ajax({ | 221 | $.ajax({ |
| 217 | url: url, | 222 | url: url, |
| @@ -241,4 +246,28 @@ function answer(id, url) { | @@ -241,4 +246,28 @@ function answer(id, url) { | ||
| 241 | }); | 246 | }); |
| 242 | 247 | ||
| 243 | $("#post_"+id).find(".answer_post").show(); | 248 | $("#post_"+id).find(".answer_post").show(); |
| 249 | +} | ||
| 250 | + | ||
| 251 | +/* | ||
| 252 | +* | ||
| 253 | +* Function to delete an answer | ||
| 254 | +* | ||
| 255 | +*/ | ||
| 256 | +function delete_answer(url, answer, message) { | ||
| 257 | + alertify.confirm(message, function(){ | ||
| 258 | + var csrftoken = getCookie('csrftoken'); | ||
| 259 | + | ||
| 260 | + $.ajax({ | ||
| 261 | + method: 'post', | ||
| 262 | + beforeSend: function (request) { | ||
| 263 | + request.setRequestHeader('X-CSRFToken', csrftoken); | ||
| 264 | + }, | ||
| 265 | + url: url, | ||
| 266 | + success: function(data) { | ||
| 267 | + alertify.alert('Amadeus', data, function(){ | ||
| 268 | + $("#answer_"+answer).remove(); | ||
| 269 | + }); | ||
| 270 | + } | ||
| 271 | + }); | ||
| 272 | + }); | ||
| 244 | } | 273 | } |
| 245 | \ No newline at end of file | 274 | \ No newline at end of file |
forum/templates/post_answers/post_answer_list.html
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | {% if answers|length > 0 %} | 3 | {% if answers|length > 0 %} |
| 4 | {% for answer in answers %} | 4 | {% for answer in answers %} |
| 5 | - <div id="answer_{{ answer_id }}" class="row" style="background-color: #e0e0e0"> | 5 | + <div id="answer_{{ answer.id }}" class="row" style="background-color: #e0e0e0"> |
| 6 | <div class="col-sm-12 col-xs-12"> | 6 | <div class="col-sm-12 col-xs-12"> |
| 7 | <h3 class="user-name"> | 7 | <h3 class="user-name"> |
| 8 | {{ answer.user }} | 8 | {{ answer.user }} |
| @@ -14,7 +14,7 @@ | @@ -14,7 +14,7 @@ | ||
| 14 | </a> | 14 | </a> |
| 15 | <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> | 15 | <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> |
| 16 | <li><a href="javascript:void(0)"><i class="material-icons">create</i> {% trans 'Edit' %}</a></li> | 16 | <li><a href="javascript:void(0)"><i class="material-icons">create</i> {% trans 'Edit' %}</a></li> |
| 17 | - <li><a href="javascript:void(0)"><i class="material-icons">delete_sweep</i> {% trans 'Remove' %}</a></li> | 17 | + <li><a href="javascript:delete_answer('{% url 'course:forum:delete_answer' answer.id %}', '{{ answer.id }}', '{% trans "Are you sure you want to delete this answer?" %}')"><i class="material-icons">delete_sweep</i> {% trans 'Remove' %}</a></li> |
| 18 | </ul> | 18 | </ul> |
| 19 | </div> | 19 | </div> |
| 20 | </div> | 20 | </div> |
forum/templates/post_answers/post_answer_render.html
| 1 | {% load i18n permission_tags %} | 1 | {% load i18n permission_tags %} |
| 2 | 2 | ||
| 3 | -<div id="answer_{{ answer_id }}" class="row" style="background-color: #e0e0e0"> | 3 | +<div id="answer_{{ answer.id }}" class="row" style="background-color: #e0e0e0"> |
| 4 | <div class="col-sm-12 col-xs-12"> | 4 | <div class="col-sm-12 col-xs-12"> |
| 5 | <h3 class="user-name"> | 5 | <h3 class="user-name"> |
| 6 | {{ answer.user }} | 6 | {{ answer.user }} |
| @@ -12,7 +12,7 @@ | @@ -12,7 +12,7 @@ | ||
| 12 | </a> | 12 | </a> |
| 13 | <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> | 13 | <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> |
| 14 | <li><a href="javascript:void(0)"><i class="material-icons">create</i> {% trans 'Edit' %}</a></li> | 14 | <li><a href="javascript:void(0)"><i class="material-icons">create</i> {% trans 'Edit' %}</a></li> |
| 15 | - <li><a href="javascript:void(0)"><i class="material-icons">delete_sweep</i> {% trans 'Remove' %}</a></li> | 15 | + <li><a href="javascript:delete_answer('{% url 'course:forum:delete_answer' answer.id %}', '{{ answer.id }}', '{% trans "Are you sure you want to delete this answer?" %}')"><i class="material-icons">delete_sweep</i> {% trans 'Remove' %}</a></li> |
| 16 | </ul> | 16 | </ul> |
| 17 | </div> | 17 | </div> |
| 18 | </div> | 18 | </div> |
forum/urls.py
| @@ -19,5 +19,7 @@ urlpatterns = [ | @@ -19,5 +19,7 @@ urlpatterns = [ | ||
| 19 | url(r'^post_answers/$', views.PostAnswerIndex.as_view(), name='post_answers'), | 19 | url(r'^post_answers/$', views.PostAnswerIndex.as_view(), name='post_answers'), |
| 20 | url(r'^reply_post/$', views.CreatePostAnswerView.as_view(), name='reply_post'), | 20 | url(r'^reply_post/$', views.CreatePostAnswerView.as_view(), name='reply_post'), |
| 21 | url(r'^render_post_answer/([\w_-]+)/$', views.render_post_answer, name='render_post_answer'), | 21 | url(r'^render_post_answer/([\w_-]+)/$', views.render_post_answer, name='render_post_answer'), |
| 22 | + url(r'^delete_post_answer/(?P<pk>[\w_-]+)/$', views.PostAnswerDeleteView.as_view(), name='delete_answer'), | ||
| 23 | + url(r'^post_answer_deleted/$', views.answer_deleted, name='deleted_answer'), | ||
| 22 | url(r'^(?P<slug>[\w_-]+)/$', views.ForumDetailView.as_view(), name='view'), | 24 | url(r'^(?P<slug>[\w_-]+)/$', views.ForumDetailView.as_view(), name='view'), |
| 23 | ] | 25 | ] |
forum/views.py
| @@ -208,4 +208,15 @@ def render_post_answer(request, answer): | @@ -208,4 +208,15 @@ def render_post_answer(request, answer): | ||
| 208 | context = {} | 208 | context = {} |
| 209 | context['answer'] = last_answer | 209 | context['answer'] = last_answer |
| 210 | 210 | ||
| 211 | - return render(request, "post_answers/post_answer_render.html", context) | ||
| 212 | \ No newline at end of file | 211 | \ No newline at end of file |
| 212 | + return render(request, "post_answers/post_answer_render.html", context) | ||
| 213 | + | ||
| 214 | +class PostAnswerDeleteView(LoginRequiredMixin, generic.DeleteView): | ||
| 215 | + login_url = reverse_lazy("core:home") | ||
| 216 | + redirect_field_name = 'next' | ||
| 217 | + | ||
| 218 | + model = PostAnswer | ||
| 219 | + pk_url_kwarg = 'pk' | ||
| 220 | + success_url = reverse_lazy('course:forum:deleted_answer') | ||
| 221 | + | ||
| 222 | +def answer_deleted(request): | ||
| 223 | + return HttpResponse(_("Post answer deleted successfully.")) | ||
| 213 | \ No newline at end of file | 224 | \ No newline at end of file |