From 5a7d79342b098e356e5b6cda9d86764cea3bd8ec Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 29 Sep 2016 21:05:36 -0300 Subject: [PATCH] Adding post edition [Issue: #74] --- forum/models.py | 9 +++++++++ forum/static/js/forum.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- forum/templates/post/post_list.html | 15 +++++++++++---- forum/templates/post/post_render.html | 15 +++++++++++---- forum/templates/post/post_update_form.html | 37 +++++++++++++++++++++++++++++++++++++ forum/templatetags/list_post.py | 2 +- forum/urls.py | 4 ++-- forum/views.py | 27 +++++++++++++-------------- 8 files changed, 132 insertions(+), 57 deletions(-) create mode 100644 forum/templates/post/post_update_form.html diff --git a/forum/models.py b/forum/models.py index fa9f4d0..834972f 100644 --- a/forum/models.py +++ b/forum/models.py @@ -41,6 +41,15 @@ class Post(models.Model): def __str__(self): return ''.join([self.user.name, " / ", str(self.post_date)]) + def is_modified(self): + create = self.post_date.strftime("%Y-%m-%d %H:%M:%S") + edit = self.modification_date.strftime("%Y-%m-%d %H:%M:%S") + + if create != edit: + return True + + return False + """ It represents an answer to a forum's post """ diff --git a/forum/static/js/forum.js b/forum/static/js/forum.js index a95b380..5a54c88 100644 --- a/forum/static/js/forum.js +++ b/forum/static/js/forum.js @@ -50,6 +50,54 @@ function showForum(url, forum_id) { $('#forumModal').modal(); } +/* +* +* Function to load form to edit post +* +*/ +function edit_post(url, post_id) { + $.ajax({ + url: url, + success: function(data) { + $("#post_"+post_id).find(".post_content").hide(); + $("#post_"+post_id).find(".post_content").after(data); + + var frm = $("#post_"+post_id).find(".edit_post_form"); + frm.submit(function () { + $.ajax({ + type: frm.attr('method'), + url: frm.attr('action'), + data: frm.serialize(), + success: function (data) { + $("#post_"+post_id).parent().after(data); + frm.parent().parent().remove(); + }, + error: function(data) { + console.log(frm.serialize()); + console.log('Error'); + } + }); + return false; + }); + } + }); +} + +/* +* +* Function to cancel post edition +* +*/ +function cancelEditPost(post_id) { + $("#post_"+post_id).find(".post_content").show(); + $("#post_"+post_id).find(".edit_post_form").remove(); +} + +/* +* +* Function to delete a post +* +*/ function delete_post(url, post) { var csrftoken = getCookie('csrftoken'); @@ -74,36 +122,4 @@ function answer(id, url) { }); $("#post_"+id).find(".answer_post").show(); -} - -function showPosts(url, forum) { - if ($("#collapse" + forum).hasClass('in')) { - $("#collapse" + forum).collapse('hide'); - } else { - $.ajax({ - url: url, - data: {'forum': forum}, - success: function(data) { - $("#collapse" + forum).find(".well").html(data); - } - }); - - $("#collapse" + forum).collapse('show'); - } -} - -function showPostsAnswers(url, post) { - if ($("#collapse" + post).hasClass('in')) { - $("#collapse" + post).collapse('hide'); - } else { - $.ajax({ - url: url, - data: {'post': post}, - success: function(data) { - $("#collapse" + post).find(".well").html(data); - } - }); - - $("#collapse" + post).collapse('show'); - } } \ No newline at end of file diff --git a/forum/templates/post/post_list.html b/forum/templates/post/post_list.html index ee4066e..97bc858 100644 --- a/forum/templates/post/post_list.html +++ b/forum/templates/post/post_list.html @@ -17,17 +17,24 @@ more_horiz {% endif %} -
-

{{ post.post_date }}

+
+
+

+ {{ post.post_date }} + {% if post.is_modified %} + - {% trans 'Edited' %} + {% endif %} +

+
+

{{ post.message|linebreaks }}

-

{{ post.message|linebreaks }}

diff --git a/forum/templates/post/post_render.html b/forum/templates/post/post_render.html index 04c80be..6ef6146 100644 --- a/forum/templates/post/post_render.html +++ b/forum/templates/post/post_render.html @@ -15,17 +15,24 @@ more_horiz {% endif %} -
-

{{ post.post_date }}

+
+
+

+ {{ post.post_date }} + {% if post.post_date != post.modifiction_date %} + - {% trans 'Edited' %} + {% endif %} +

+
+

{{ post.message|linebreaks }}

-

{{ post.message|linebreaks }}

\ No newline at end of file diff --git a/forum/templates/post/post_update_form.html b/forum/templates/post/post_update_form.html new file mode 100644 index 0000000..f468d0d --- /dev/null +++ b/forum/templates/post/post_update_form.html @@ -0,0 +1,37 @@ +{% load i18n permission_tags list_post %} +{% load widget_tweaks %} + +
+ {% csrf_token %} + {% for field in form %} + {% if field.field.widget.input_type == 'hidden' %} + {% render_field field class='form-control' %} + {% else %} +
+
+ {% render_field field class='form-control' placeholder="Post a message" %} + {{ field.help_text }} + {% if field.errors %} +
+
+ +
+ {% endif %} +
+
+
+ + +
+ {% endif %} + {% endfor %} +
\ No newline at end of file diff --git a/forum/templatetags/list_post.py b/forum/templatetags/list_post.py index b35bed9..819767c 100644 --- a/forum/templatetags/list_post.py +++ b/forum/templatetags/list_post.py @@ -14,6 +14,6 @@ def list_posts(request, forum): 'request': request, } - context['posts'] = Post.objects.filter(forum = forum) + context['posts'] = Post.objects.filter(forum = forum).order_by('post_date') return context \ No newline at end of file diff --git a/forum/urls.py b/forum/urls.py index 9163164..1416a48 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -6,10 +6,10 @@ from . import views urlpatterns = [ url(r'^$', views.ForumIndex.as_view(), name='index'), 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'^update_post/(?P[\w_-]+)/$', views.PostUpdateView.as_view(), name='update_post'), url(r'^delete_post/(?P[\w_-]+)/$', views.PostDeleteView.as_view(), name='delete_post'), + url(r'^render_post/([\w_-]+)/$', views.render_post, name='render_post'), url(r'^post_deleted/$', views.post_deleted, name='deleted_post'), url(r'^post_answers$', views.PostAnswerIndex.as_view(), name='post_answers'), url(r'^reply_post$', views.CreatePostAnswerView.as_view(), name='reply_post'), diff --git a/forum/views.py b/forum/views.py index bf8279f..d9d0664 100644 --- a/forum/views.py +++ b/forum/views.py @@ -36,20 +36,6 @@ class CreateForumView(LoginRequiredMixin, generic.edit.CreateView): form_class = ForumForm success_url = reverse_lazy('forum:index') -class PostIndex(LoginRequiredMixin, generic.ListView): - login_url = reverse_lazy("core:home") - redirect_field_name = 'next' - - template_name = "post/post_list.html" - context_object_name = 'posts' - - def get_queryset(self): - forum = get_object_or_404(Forum, slug = self.request.GET.get('forum', '')) - - context = Post.objects.filter(forum = forum) - - return context - class CreatePostView(LoginRequiredMixin, generic.edit.CreateView): login_url = reverse_lazy("core:home") redirect_field_name = 'next' @@ -77,6 +63,19 @@ def render_post(request, post): return render(request, "post/post_render.html", context) +class PostUpdateView(LoginRequiredMixin, generic.UpdateView): + login_url = reverse_lazy("core:home") + redirect_field_name = 'next' + + form_class = PostForm + model = Post + template_name = "post/post_update_form.html" + + def get_success_url(self): + self.success_url = reverse('forum:render_post', args = (self.object.id, )) + + return self.success_url + class PostDeleteView(LoginRequiredMixin, generic.DeleteView): login_url = reverse_lazy("core:home") redirect_field_name = 'next' -- libgit2 0.21.2