From bfc4ec8f3bc0902dc1aa1e2fc99543621afab99f Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 9 Feb 2017 19:57:46 -0200 Subject: [PATCH] Adding comment edit --- amadeus/static/js/mural.js | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mural/migrations/0007_comment_edited.py | 20 ++++++++++++++++++++ mural/models.py | 1 + mural/templates/mural/_form_comment.html | 10 ++-------- mural/templates/mural/_user_suggestions_list.html | 2 +- mural/templates/mural/_view_comment.html | 6 +++--- mural/templates/mural/list.html | 256 +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- mural/urls.py | 1 + mural/views.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 9 files changed, 348 insertions(+), 270 deletions(-) create mode 100644 amadeus/static/js/mural.js create mode 100644 mural/migrations/0007_comment_edited.py diff --git a/amadeus/static/js/mural.js b/amadeus/static/js/mural.js new file mode 100644 index 0000000..2869388 --- /dev/null +++ b/amadeus/static/js/mural.js @@ -0,0 +1,271 @@ +var new_posts = []; +// loadOnScroll handler +var loadOnScroll = function() { + // If the current scroll position is past out cutoff point... + if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) { + // temporarily unhook the scroll event watcher so we don't call a bunch of times in a row + $(window).unbind(); + // execute the load function below that will visit the view and return the content + loadPosts(); + } +}; + +var loadPosts = function() { + var loadUrl = $('.mural').data('url'), + pageNum = $('.mural').data('page'), + numberPages = $('.mural').data('pages'), + favorites = $('.mural').data('fav'), + mine = $('.mural').data('mine'), + showing = new_posts.join(','); + // Check if page is equal to the number of pages + if (pageNum == numberPages) { + return false + } + // Update the page number + pageNum = pageNum + 1; + + $("#loading_posts").show(); + // Configure the url we're about to hit + setTimeout(function (){ + $.ajax({ + url: loadUrl, + data: {'page': pageNum, "favorite": favorites, "mine": mine, "showing": showing}, + success: function(data) { + $("#loading_posts").hide(); + + $(".posts").append(data); + + $('.mural').data('page', pageNum); + }, + complete: function(data, textStatus){ + // Turn the scroll monitor back on + $(window).bind('scroll', loadOnScroll); + } + }); + }, 1000) +}; + +$(function () { + $(window).bind('scroll', loadOnScroll); + + $(".post-field").click(function () { + var url = $(this).find('h4').data('url'); + + $.ajax({ + url: url, + success: function (data) { + $('#post-modal-form').html(data); + + setPostFormSubmit(); + + $('#post-modal-form').modal('show'); + } + }); + }); + + $("#clear_filter").click(function () { + var frm = $(this).parent(); + + frm.find("input[type='checkbox']").prop('checked', false); + + frm.submit(); + }); +}); + +function setPostFormSubmit(post = "") { + var frm = $('#post-form'); + + frm.submit(function () { + var formData = new FormData($(this)[0]); + + $.ajax({ + type: frm.attr('method'), + url: frm.attr('action'), + data: formData, + dataType: "json", + async: false, + success: function (data) { + if (post != "") { + var old = $("#post-" + post); + + old.before(data.view); + + old.remove(); + } else { + $('.posts').prepend(data.view); + + new_posts.push(data.new_id); + + $('.no-subjects').attr('style', 'display:none'); + } + + $('#post-modal-form').modal('hide'); + + alertify.success(data.message); + }, + error: function(data) { + $("#post-modal-form").html(data.responseText); + setPostFormSubmit(post); + }, + cache: false, + contentType: false, + processData: false + }); + + return false; + }); +} + +function favorite(btn) { + var action = btn.data('action'), + url = btn.data('url'); + + $.ajax({ + url: url, + data: {'action': action}, + dataType: 'json', + success: function (response) { + if (action == 'favorite') { + btn.switchClass("btn_fav", "btn_unfav", 250, "easeInOutQuad"); + btn.data('action', 'unfavorite'); + } else { + btn.switchClass("btn_unfav", "btn_fav", 250, "easeInOutQuad"); + btn.data('action', 'favorite'); + } + + btn.attr('data-original-title', response.label); + } + }); +} + +function editPost(btn) { + var url = btn.data('url'); + var post = btn.data('post'); + + $.ajax({ + url: url, + success: function (data) { + $('#post-modal-form').html(data); + + setPostFormSubmit(post); + + $('#post-modal-form').modal('show'); + } + }); +} + +function deletePost(btn) { + var url = btn.data('url'); + var post = btn.data('post'); + + $.ajax({ + url: url, + success: function (data) { + $('#post-modal-form').html(data); + + setPostDeleteSubmit(post); + + $('#post-modal-form').modal('show'); + } + }); +} + +function setPostDeleteSubmit (post) { + var frm = $("#delete_form"); + + frm.submit(function () { + $.ajax({ + type: frm.attr('method'), + url: frm.attr('action'), + data: frm.serialize(), + success: function (response) { + $("#post-" + post).remove(); + + $('#post-modal-form').modal('hide'); + + alertify.success(response.msg); + }, + error: function (data) { + console.log(data); + } + }); + + return false; + }); +} + +function comment(field) { + var url = field.find('h4').data('url'), + post = field.parent().parent(); + + $.ajax({ + url: url, + success: function (data) { + $('#post-modal-form').html(data); + + setCommentFormSubmit(post); + + $('#post-modal-form').modal('show'); + } + }); +} + +function editComment(btn) { + var url = btn.data('url'), + post_id = btn.data('post'), + post = $("#post-" + post_id), + comment = btn.data('id'); + + $.ajax({ + url: url, + success: function (data) { + $('#post-modal-form').html(data); + + setCommentFormSubmit(post, comment); + + $('#post-modal-form').modal('show'); + } + }); +} + +function setCommentFormSubmit(post, comment = "") { + var frm = $('#comment-form'); + + frm.submit(function () { + var formData = new FormData($(this)[0]); + + $.ajax({ + type: frm.attr('method'), + url: frm.attr('action'), + data: formData, + dataType: "json", + async: false, + success: function (data) { + if (comment != "") { + var old = $("#comment-" + comment); + + old.before(data.view); + + old.remove(); + } else { + $(post).find(".comment-section").append(data.view); + + //new_posts.push(data.new_id); + } + + $('#post-modal-form').modal('hide'); + + alertify.success(data.message); + }, + error: function(data) { + $("#post-modal-form").html(data.responseText); + setPostFormSubmit(post, comment); + }, + cache: false, + contentType: false, + processData: false + }); + + return false; + }); +} \ No newline at end of file diff --git a/mural/migrations/0007_comment_edited.py b/mural/migrations/0007_comment_edited.py new file mode 100644 index 0000000..b037ff4 --- /dev/null +++ b/mural/migrations/0007_comment_edited.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2017-02-09 21:47 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mural', '0006_auto_20170209_1434'), + ] + + operations = [ + migrations.AddField( + model_name='comment', + name='edited', + field=models.BooleanField(default=False, verbose_name='Edited'), + ), + ] diff --git a/mural/models.py b/mural/models.py index 40bbdc4..ac09167 100644 --- a/mural/models.py +++ b/mural/models.py @@ -69,6 +69,7 @@ class Comment(models.Model): user = models.ForeignKey(User, verbose_name = _('User'), related_name = "comment_user", null = True) create_date = models.DateTimeField(_('Create Date'), auto_now_add = True) last_update = models.DateTimeField(_('Last Update'), auto_now = True) + edited = models.BooleanField(_('Edited'), default = False) """ Model to handle posts visualizations diff --git a/mural/templates/mural/_form_comment.html b/mural/templates/mural/_form_comment.html index 8c51f91..7442c42 100644 --- a/mural/templates/mural/_form_comment.html +++ b/mural/templates/mural/_form_comment.html @@ -73,16 +73,10 @@