From c426f2edf5440a3c7bc746bfc702d3c8451632df Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 9 Feb 2017 00:42:36 -0200 Subject: [PATCH] Adding mural post comment creation (Adjusts to be made) --- amadeus/static/css/base/amadeus.css | 27 ++++++++++++++++++++------- amadeus/static/css/themes/green.css | 18 +++++++++++++++--- mural/forms.py | 34 ++++++++++++++++++++++++++++++++-- mural/models.py | 2 +- mural/templates/mural/_form_comment.html | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mural/templates/mural/_view.html | 9 +++++++-- mural/templates/mural/_view_comment.html | 42 ++++++++++++++++++++++++++++++++++++++++++ mural/templates/mural/list.html | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mural/urls.py | 4 +++- mural/views.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 10 files changed, 372 insertions(+), 19 deletions(-) create mode 100644 mural/templates/mural/_form_comment.html create mode 100644 mural/templates/mural/_view_comment.html diff --git a/amadeus/static/css/base/amadeus.css b/amadeus/static/css/base/amadeus.css index 7e05949..56f4ea4 100755 --- a/amadeus/static/css/base/amadeus.css +++ b/amadeus/static/css/base/amadeus.css @@ -1009,16 +1009,17 @@ li.item .notify_badge { .post .post-body { padding-right: 0; + margin-bottom: 10px; } -.post .post-user { +.post .post-user, .comment .comment-user { margin-top: 0; margin-bottom: 5px; font-size: 18px; font-weight: 700; } -.post .post-user .btn-group { +.post .post-user .btn-group, .comment .comment-user .btn-group { margin: 0; } @@ -1030,28 +1031,27 @@ li.item .notify_badge { font-size: 18px; } -.post .post-user .user-action { +.post .post-user .user-action, .comment .comment-user .user-action { padding-left: 5px; font-size: 16px; font-weight: normal; } -.post .post-user .user-action i { +.post .post-user .user-action i, .comment .comment-user .user-action i { font-size: 22px; } -.post .time { +.post .time, .comment-time { font-size: 14px; } .post .post-comment { - margin-top: 10px; border-top-width: 1px; border-top-style: solid; padding: 10px 0px 8px 0px; } -.post .post-comment .user-img div { +.post .post-comment .user-img div, .comment .comment-img div { display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */ display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */ display: -ms-flexbox; /* TWEENER - IE 10 */ @@ -1084,6 +1084,19 @@ li.item .notify_badge { margin-bottom: 15px; } +.comment-section { + max-height: 700px; + overflow-y: scroll; +} + +.comment { + padding: 10px 0px 8px 0px; +} + +.comment .comment-user .btn-group .btn_menu i { + font-size: 15px; +} + .btn:focus, .btn:active:focus, .btn.active:focus, .btn.focus, .btn:active.focus, .btn.active.focus { outline: none; } \ No newline at end of file diff --git a/amadeus/static/css/themes/green.css b/amadeus/static/css/themes/green.css index 27728ed..24a2aef 100644 --- a/amadeus/static/css/themes/green.css +++ b/amadeus/static/css/themes/green.css @@ -496,7 +496,7 @@ a.add-row { background: #FFFFFF; } -.mural .post_make .user-img, .post .post-img, .post .post-comment .user-img div { +.mural .post_make .user-img, .post .post-img, .post .post-comment .user-img div, .comment .comment-img div { background: #CCCCCC; } @@ -508,11 +508,11 @@ a.add-row { color: #CCCCCC; } -.post_action i, .post .post-user .user-action i { +.post_action i, .post .post-user .user-action i, .comment .comment-user .user-action i { color: #1d8fe0; } -.post .post-user { +.post .post-user, .comment .comment-user { color: #4caf50; } @@ -534,6 +534,18 @@ a.add-row { background: #FFFFFF; } +.comment-section { + background: #D5D5D5; +} + +.comment .comment-user .user-action { + color: #999999; +} + +.comment-time { + color: #9A9A9A; +} + .btn_unfav { color: #FF0000 !important; } diff --git a/mural/forms.py b/mural/forms.py index 7bfedde..eebc153 100644 --- a/mural/forms.py +++ b/mural/forms.py @@ -3,7 +3,7 @@ from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.html import strip_tags -from .models import GeneralPost +from .models import GeneralPost, Comment class Validation(forms.ModelForm): MAX_UPLOAD_SIZE = 5*1024*1024 @@ -38,4 +38,34 @@ class GeneralPostForm(Validation): widgets = { 'action': forms.RadioSelect, 'post': forms.Textarea - } \ No newline at end of file + } + +class CommentForm(forms.ModelForm): + MAX_UPLOAD_SIZE = 5*1024*1024 + + def clean_comment(self): + comment = self.cleaned_data.get('comment', '') + cleaned_comment = strip_tags(comment) + + if cleaned_comment == '': + self._errors['comment'] = [_('This field is required.')] + + return ValueError + + return comment + + def clean_image(self): + image = self.cleaned_data.get('image', False) + + if image: + if hasattr(image, '_size'): + if image._size > self.MAX_UPLOAD_SIZE: + self._errors['image'] = [_("The image is too large. It should have less than 5MB.")] + + return ValueError + + return image + + class Meta: + model = Comment + fields = ['comment', 'image'] \ No newline at end of file diff --git a/mural/models.py b/mural/models.py index 3754a7e..40bbdc4 100644 --- a/mural/models.py +++ b/mural/models.py @@ -65,7 +65,7 @@ class SubjectPost(Mural): class Comment(models.Model): comment = models.TextField(_('Comment'), blank = True) image = models.ImageField(verbose_name = _('Image'), null=True, blank = True, upload_to = 'posts/comments/', validators = [validate_img_extension]) - post = models.ForeignKey(Mural, verbose_name = _('Post'), related_name = 'comment_post') + post = models.ForeignKey(Mural, verbose_name = _('Post'), related_name = 'comment_post', null = True) 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) diff --git a/mural/templates/mural/_form_comment.html b/mural/templates/mural/_form_comment.html new file mode 100644 index 0000000..9c37af6 --- /dev/null +++ b/mural/templates/mural/_form_comment.html @@ -0,0 +1,121 @@ +{% load static i18n %} +{% load widget_tweaks %} + + + + diff --git a/mural/templates/mural/_view.html b/mural/templates/mural/_view.html index 56dbde0..a55129b 100644 --- a/mural/templates/mural/_view.html +++ b/mural/templates/mural/_view.html @@ -43,15 +43,20 @@ {% endif %} +
+ {% for comment in post.comment_post.all %} + {% include 'mural/_view_comment.html' %} + {% endfor %} +
-
+
-

{% trans 'Make a comment...' %}

+

{% trans 'Make a comment...' %}

diff --git a/mural/templates/mural/_view_comment.html b/mural/templates/mural/_view_comment.html new file mode 100644 index 0000000..2217ef7 --- /dev/null +++ b/mural/templates/mural/_view_comment.html @@ -0,0 +1,42 @@ +{% load i18n %} + +
+
+
+ +
+
+
+

+ {{ comment.user }} + + + {% trans 'Comment' %} + + {% if request.user == comment.user or request.user.is_staff %} + + + + + {% endif %} +

+

+ + {% trans 'In' %} {{ comment.last_update }} +

+ + {% autoescape off %} + {{ comment.comment }} + {% endautoescape %} + + {% if comment.image %} + + {% endif %} +
+
\ No newline at end of file diff --git a/mural/templates/mural/list.html b/mural/templates/mural/list.html index 69697b4..b74fd90 100644 --- a/mural/templates/mural/list.html +++ b/mural/templates/mural/list.html @@ -268,5 +268,63 @@ 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 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 (post != "") { + var old = $("#post-" + post); + + 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; + }); + } {% endblock %} \ No newline at end of file diff --git a/mural/urls.py b/mural/urls.py index 6366492..a1a9bb2 100644 --- a/mural/urls.py +++ b/mural/urls.py @@ -6,7 +6,9 @@ urlpatterns = [ url(r'^create_gen/$', views.GeneralCreate.as_view(), name='create_general'), url(r'^update_gen/(?P[\w_-]+)/$', views.GeneralUpdate.as_view(), name='update_general'), url(r'^delete_gen/(?P[\w_-]+)/$', views.GeneralDelete.as_view(), name='delete_general'), - url(r'^render_post/([\w_-]+)/([\w_-]+)/$', views.render_gen_post, name='render_post_general'), url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), url(r'^deleted/$', views.deleted_post, name='deleted_post'), + url(r'^comment/(?P[\w_-]+)/$', views.CommentCreate.as_view(), name='create_comment'), + url(r'^render_comment/([\w_-]+)/([\w_-]+)/$', views.render_comment, name='render_comment'), + url(r'^render_post/([\w_-]+)/([\w_-]+)/$', views.render_gen_post, name='render_post_general'), ] \ No newline at end of file diff --git a/mural/views.py b/mural/views.py index 08b2985..3ed7053 100644 --- a/mural/views.py +++ b/mural/views.py @@ -14,8 +14,8 @@ import json from users.models import User -from .models import Mural, GeneralPost, CategoryPost, SubjectPost, MuralVisualizations, MuralFavorites -from .forms import GeneralPostForm +from .models import Mural, GeneralPost, CategoryPost, SubjectPost, MuralVisualizations, MuralFavorites, Comment +from .forms import GeneralPostForm, CommentForm class GeneralIndex(LoginRequiredMixin, generic.ListView): login_url = reverse_lazy("users:login") @@ -232,4 +232,74 @@ def favorite(request, post): else: MuralFavorites.objects.filter(post = post, user = request.user).delete() - return JsonResponse({'label': _('Favorite')}) \ No newline at end of file + return JsonResponse({'label': _('Favorite')}) + +class CommentCreate(LoginRequiredMixin, generic.edit.CreateView): + login_url = reverse_lazy("users:login") + redirect_field_name = 'next' + + template_name = 'mural/_form_comment.html' + form_class = CommentForm + + def form_invalid(self, form): + context = super(CommentCreate, self).form_invalid(form) + context.status_code = 400 + + return context + + def form_valid(self, form): + self.object = form.save(commit = False) + + post_id = self.kwargs.get('post', '') + post = get_object_or_404(Mural, id = post_id) + + self.object.user = self.request.user + self.object.post = post + + self.object.save() + + users = User.objects.all().exclude(id = self.request.user.id) + entries = [] + + notify_type = "mural" + user_icon = self.object.user.image_url + #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) + simple_notify = _("%s has made a post in General")%(str(self.object.user)) + pathname = reverse("mural:manage_general") + + #for user in users: + # entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) + # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})}) + + #MuralVisualizations.objects.bulk_create(entries) + + return super(CommentCreate, self).form_valid(form) + + def get_context_data(self, *args, **kwargs): + context = super(CommentCreate, self).get_context_data(*args, **kwargs) + + post_id = self.kwargs.get('post', '') + + context['form_url'] = reverse_lazy("mural:create_comment", kwargs = {"post": post_id}) + + return context + + def get_success_url(self): + return reverse_lazy('mural:render_comment', args = (self.object.id, 'create', )) + +def render_comment(request, comment, msg): + comment = get_object_or_404(Comment, id = comment) + + context = {} + context['comment'] = comment + + msg = "" + + if msg == 'create': + msg = _('Your comment was published successfully!') + else: + msg = _('Your comment was edited successfully!') + + html = render_to_string("mural/_view_comment.html", context, request) + + return JsonResponse({'message': msg, 'view': html, 'new_id': comment.id}) \ No newline at end of file -- libgit2 0.21.2