Commit 27792fefc38edc74e8268a9f61231db19c877d9a
1 parent
2666c250
Exists in
master
and in
5 other branches
Adding post and answer post forms (Post functionality too ) [Issue: #86]
Showing
4 changed files
with
74 additions
and
33 deletions
Show diff stats
forum/static/js/forum.js
| 1 | +/* | ||
| 2 | +* | ||
| 3 | +* Function to get a cookie stored on browser | ||
| 4 | +* | ||
| 5 | +*/ | ||
| 1 | function getCookie(name) { | 6 | function getCookie(name) { |
| 2 | var cookieValue = null; | 7 | var cookieValue = null; |
| 3 | if (document.cookie && document.cookie !== '') { | 8 | if (document.cookie && document.cookie !== '') { |
| @@ -16,6 +21,31 @@ function getCookie(name) { | @@ -16,6 +21,31 @@ function getCookie(name) { | ||
| 16 | 21 | ||
| 17 | /* | 22 | /* |
| 18 | * | 23 | * |
| 24 | +* Defining action of the form to make a post in forum | ||
| 25 | +* | ||
| 26 | +*/ | ||
| 27 | +$(document).ready(function (){ | ||
| 28 | + var frm = $('#form_post'); | ||
| 29 | + frm.submit(function () { | ||
| 30 | + $.ajax({ | ||
| 31 | + type: frm.attr('method'), | ||
| 32 | + url: frm.attr('action'), | ||
| 33 | + data: frm.serialize(), | ||
| 34 | + success: function (data) { | ||
| 35 | + $("#posts_list").append(data); | ||
| 36 | + frm[0].reset(); | ||
| 37 | + }, | ||
| 38 | + error: function(data) { | ||
| 39 | + console.log(frm.serialize()); | ||
| 40 | + console.log('Error'); | ||
| 41 | + } | ||
| 42 | + }); | ||
| 43 | + return false; | ||
| 44 | + }); | ||
| 45 | +}); | ||
| 46 | + | ||
| 47 | +/* | ||
| 48 | +* | ||
| 19 | * Function to load create forum's form and set the submit function | 49 | * Function to load create forum's form and set the submit function |
| 20 | * | 50 | * |
| 21 | */ | 51 | */ |
| @@ -59,39 +89,9 @@ function createForum(url, topic) { | @@ -59,39 +89,9 @@ function createForum(url, topic) { | ||
| 59 | 89 | ||
| 60 | /* | 90 | /* |
| 61 | * | 91 | * |
| 62 | -* Function to load forum to modal | 92 | +* Function to delete a forum |
| 63 | * | 93 | * |
| 64 | */ | 94 | */ |
| 65 | -function showForum(url, forum_id) { | ||
| 66 | - $.ajax({ | ||
| 67 | - url: url, | ||
| 68 | - data: {'forum_id': forum_id}, | ||
| 69 | - success: function(data) { | ||
| 70 | - $(".forum_topics").html(data); | ||
| 71 | - | ||
| 72 | - var frm = $('#form_post'); | ||
| 73 | - frm.submit(function () { | ||
| 74 | - $.ajax({ | ||
| 75 | - type: frm.attr('method'), | ||
| 76 | - url: frm.attr('action'), | ||
| 77 | - data: frm.serialize(), | ||
| 78 | - success: function (data) { | ||
| 79 | - $("#posts_list").append(data); | ||
| 80 | - frm[0].reset(); | ||
| 81 | - }, | ||
| 82 | - error: function(data) { | ||
| 83 | - console.log(frm.serialize()); | ||
| 84 | - console.log('Error'); | ||
| 85 | - } | ||
| 86 | - }); | ||
| 87 | - return false; | ||
| 88 | - }); | ||
| 89 | - } | ||
| 90 | - }); | ||
| 91 | - | ||
| 92 | - $('#forumModal').modal(); | ||
| 93 | -} | ||
| 94 | - | ||
| 95 | function delete_forum(url, forum, message) { | 95 | function delete_forum(url, forum, message) { |
| 96 | alertify.confirm(message, function(){ | 96 | alertify.confirm(message, function(){ |
| 97 | var csrftoken = getCookie('csrftoken'); | 97 | var csrftoken = getCookie('csrftoken'); |
forum/templates/forum/forum_view.html
| @@ -59,7 +59,45 @@ | @@ -59,7 +59,45 @@ | ||
| 59 | <h4><b>{% trans 'Opened in' %}:</b> {{ forum.create_date }}</h4> | 59 | <h4><b>{% trans 'Opened in' %}:</b> {{ forum.create_date }}</h4> |
| 60 | </div> | 60 | </div> |
| 61 | </div> | 61 | </div> |
| 62 | - | 62 | + <div class="form_post_block"> |
| 63 | + <hr /> | ||
| 64 | + <form id="form_post" method="post" action="{% url 'course:forum:create_post' %}" enctype="multipart/form-data"> | ||
| 65 | + {% csrf_token %} | ||
| 66 | + {% for field in form %} | ||
| 67 | + {% if field.field.widget.input_type == 'hidden' %} | ||
| 68 | + {% render_field field class='form-control' value=forum.id %} | ||
| 69 | + {% else %} | ||
| 70 | + <div class="form-group {% if form.has_error %} has-error {% endif %} is-fileinput"> | ||
| 71 | + <div class="input-group"> | ||
| 72 | + {% render_field field class='form-control' placeholder="Post a message" %} | ||
| 73 | + <span class="help-block">{{ field.help_text }}</span> | ||
| 74 | + {% if field.errors %} | ||
| 75 | + <div class="row"> | ||
| 76 | + <br /> | ||
| 77 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 78 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 79 | + <span aria-hidden="true">×</span> | ||
| 80 | + </button> | ||
| 81 | + <ul> | ||
| 82 | + {% for error in field.errors %} | ||
| 83 | + <li>{{ error }}</li> | ||
| 84 | + {% endfor %} | ||
| 85 | + </ul> | ||
| 86 | + </div> | ||
| 87 | + </div> | ||
| 88 | + {% endif %} | ||
| 89 | + <span class="input-group-btn"> | ||
| 90 | + <button type="submit" class="btn btn-fab btn-fab-mini"> | ||
| 91 | + <i class="material-icons">send</i> | ||
| 92 | + </button> | ||
| 93 | + </span> | ||
| 94 | + </div> | ||
| 95 | + </div> | ||
| 96 | + {% endif %} | ||
| 97 | + {% endfor %} | ||
| 98 | + </form> | ||
| 99 | + <hr /> | ||
| 100 | + </div> | ||
| 63 | <div id="posts_list"> | 101 | <div id="posts_list"> |
| 64 | {% list_posts request forum %} | 102 | {% list_posts request forum %} |
| 65 | </div> | 103 | </div> |
forum/urls.py
| @@ -5,7 +5,6 @@ from . import views | @@ -5,7 +5,6 @@ from . import views | ||
| 5 | 5 | ||
| 6 | urlpatterns = [ | 6 | urlpatterns = [ |
| 7 | url(r'^$', views.ForumIndex.as_view(), name='index'), | 7 | url(r'^$', views.ForumIndex.as_view(), name='index'), |
| 8 | - url(r'^(?P<slug>[\w_-]+)/$', views.ForumDetailView.as_view(), name='view'), | ||
| 9 | url(r'^create/$', views.CreateForumView.as_view(), name='create'), | 8 | url(r'^create/$', views.CreateForumView.as_view(), name='create'), |
| 10 | url(r'^delete/(?P<pk>[\w_-]+)/$', views.ForumDeleteView.as_view(), name='delete'), | 9 | url(r'^delete/(?P<pk>[\w_-]+)/$', views.ForumDeleteView.as_view(), name='delete'), |
| 11 | url(r'^render_forum/([\w_-]+)/$', views.render_forum, name='render_forum'), | 10 | url(r'^render_forum/([\w_-]+)/$', views.render_forum, name='render_forum'), |
| @@ -17,4 +16,5 @@ urlpatterns = [ | @@ -17,4 +16,5 @@ urlpatterns = [ | ||
| 17 | url(r'^post_deleted/$', views.post_deleted, name='deleted_post'), | 16 | url(r'^post_deleted/$', views.post_deleted, name='deleted_post'), |
| 18 | url(r'^post_answers/$', views.PostAnswerIndex.as_view(), name='post_answers'), | 17 | url(r'^post_answers/$', views.PostAnswerIndex.as_view(), name='post_answers'), |
| 19 | url(r'^reply_post/$', views.CreatePostAnswerView.as_view(), name='reply_post'), | 18 | url(r'^reply_post/$', views.CreatePostAnswerView.as_view(), name='reply_post'), |
| 19 | + url(r'^(?P<slug>[\w_-]+)/$', views.ForumDetailView.as_view(), name='view'), | ||
| 20 | ] | 20 | ] |
forum/views.py
| @@ -74,6 +74,7 @@ class ForumDetailView(LoginRequiredMixin, generic.DetailView): | @@ -74,6 +74,7 @@ class ForumDetailView(LoginRequiredMixin, generic.DetailView): | ||
| 74 | context = super(ForumDetailView, self).get_context_data(**kwargs) | 74 | context = super(ForumDetailView, self).get_context_data(**kwargs) |
| 75 | forum = get_object_or_404(Forum, slug = self.kwargs.get('slug')) | 75 | forum = get_object_or_404(Forum, slug = self.kwargs.get('slug')) |
| 76 | 76 | ||
| 77 | + context['form'] = PostForm() | ||
| 77 | context['forum'] = forum | 78 | context['forum'] = forum |
| 78 | context['title'] = forum.name | 79 | context['title'] = forum.name |
| 79 | 80 | ||
| @@ -145,6 +146,8 @@ class PostAnswerIndex(LoginRequiredMixin, generic.ListView): | @@ -145,6 +146,8 @@ class PostAnswerIndex(LoginRequiredMixin, generic.ListView): | ||
| 145 | return context | 146 | return context |
| 146 | 147 | ||
| 147 | class CreatePostAnswerView(LoginRequiredMixin, generic.edit.CreateView): | 148 | class CreatePostAnswerView(LoginRequiredMixin, generic.edit.CreateView): |
| 149 | + login_url = reverse_lazy("core:home") | ||
| 150 | + redirect_field_name = 'next' | ||
| 148 | 151 | ||
| 149 | template_name = 'post_answers/post_answer_form.html' | 152 | template_name = 'post_answers/post_answer_form.html' |
| 150 | form_class = PostAnswerForm | 153 | form_class = PostAnswerForm |