From e56e313b8f7f259f182295f37a0de5755b0ddc6c Mon Sep 17 00:00:00 2001 From: Zambom Date: Wed, 22 Mar 2017 10:16:32 -0300 Subject: [PATCH] Adding favorite, filter and load old messages functions and adjusts --- amadeus/static/css/base/amadeus.css | 4 ++-- amadeus/static/js/chat.js | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- chat/templates/chat/_message.html | 4 +++- chat/templates/chat/talk.html | 13 +++++++++++-- chat/templatetags/chat_tags.py | 25 +++++++++++++++++++++++-- chat/urls.py | 2 ++ chat/views.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 7 files changed, 270 insertions(+), 19 deletions(-) diff --git a/amadeus/static/css/base/amadeus.css b/amadeus/static/css/base/amadeus.css index f9c1bc8..cf1f608 100755 --- a/amadeus/static/css/base/amadeus.css +++ b/amadeus/static/css/base/amadeus.css @@ -1349,9 +1349,9 @@ div.dataTables_wrapper div.dataTables_paginate { border-width: 1px; border-style: solid; border-radius: 10px; - margin-right: 10px; + margin-right: 5px; position: relative; - top: 3px; + top: -1px; display: inline-block; border-spacing: 0; border-collapse: collapse; diff --git a/amadeus/static/js/chat.js b/amadeus/static/js/chat.js index 23183e8..0ed85aa 100644 --- a/amadeus/static/js/chat.js +++ b/amadeus/static/js/chat.js @@ -1,3 +1,5 @@ +var new_msgs = {}; + function getModalInfo(btn, space, space_type) { var url = btn.data('url'); @@ -6,13 +8,15 @@ function getModalInfo(btn, space, space_type) { url: url, data: {'space': space, 'space_type': space_type}, success: function (response) { + var modal_shown = $("#chat-modal-info").is(":visible"); + $("#chat-modal-info").html(response); $("#chat-modal-info").modal('show'); $.material.init(); - $('#chat-modal-info').on('shown.bs.modal', function () { + if (modal_shown) { $(".messages-container").each(function () { var height = $(this)[0].scrollHeight; @@ -20,7 +24,19 @@ function getModalInfo(btn, space, space_type) { }); setShortChatFormSubmit(); - }); + setFiltersSubmitAndPagination(); + } else { + $('#chat-modal-info').on('shown.bs.modal', function () { + $(".messages-container").each(function () { + var height = $(this)[0].scrollHeight; + + $(this).animate({scrollTop: height}, 0); + }); + + setShortChatFormSubmit(); + setFiltersSubmitAndPagination(); + }); + } $("#msg_editable").on('click', function () { $(this).trigger('focusin'); @@ -93,9 +109,13 @@ function setChatFormSubmit() { $(this).animate({scrollTop: height}, 0); }); - $('#chat-modal-form').modal('hide'); + if (typeof(new_msgs[data.talk_id]) == 'undefined') { + new_msgs[data.talk_id] = []; + } + + new_msgs[data.talk_id].push(data.new_id); - alertify.success(data.message); + $('#chat-modal-form').modal('hide'); }, error: function(data) { $("#chat-modal-form").html(data.responseText); @@ -137,13 +157,16 @@ function setShortChatFormSubmit() { $(this).animate({scrollTop: height}, 0); }); + if (typeof(new_msgs[data.talk_id]) == 'undefined') { + new_msgs[data.talk_id] = []; + } + + new_msgs[data.talk_id].push(data.new_id); + editable.html(""); editable.trigger("focusout"); - - alertify.success(data.message); }, error: function(data) { - alertify.error(data.responseText); setShortChatFormSubmit(); }, cache: false, @@ -154,4 +177,133 @@ function setShortChatFormSubmit() { 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 setFiltersSubmitAndPagination() { + var filters = $('#chat-filters'), + clear_filters = $('#clear_filter'), + messages = $('.messages-container'), + loading = messages.find('.loading-msgs'), + more = messages.find('.more-msgs'), + msg_section = $('.messages-list'); + + filters.submit(function () { + var favorite = $(this).find("input[name='favorite']").is(':checked') ? "True" : "", + mine = $(this).find("input[name='mine']").is(':checked') ? "True" : "", + url = messages.data('url'); + + msg_section.html(''); + + more.hide(); + loading.show(); + + $.ajax({ + url: url, + data: {'favorite': favorite, 'mine': mine}, + dataType: 'json', + success: function (data) { + loading.hide(); + + if (data.count > 0) { + msg_section.append(data.messages); + + messages.data('pages', data.num_pages); + messages.data('page', data.num_page); + + if (data.num_page < data.num_pages) { + more.show(); + } else { + more.hide(); + } + } + + messages.data('fav', favorite); + messages.data('mine', mine); + + messages.each(function () { + var height = $(this)[0].scrollHeight; + + $(this).animate({scrollTop: height}, 0); + }); + } + }); + + return false; + }); + + clear_filters.click(function () { + var frm = $(this).parent(); + + $("input[type='checkbox']").prop('checked', false); + + frm.submit(); + }); + + more.click(function () { + var url = messages.data('url'), + pageNum = messages.data('page'), + numberPages = messages.data('pages'), + favorites = messages.data('fav'), + mine = messages.data('mine'), + talk = messages.data('talk'); + + var showing; + + if (typeof(new_msgs[talk]) == 'undefined') { + showing = ""; + } else { + showing = new_msgs[talk].join(','); + } + + if (pageNum == numberPages) { + return false + } + + pageNum = pageNum + 1; + + more.hide(); + loading.show(); + + $.ajax({ + url: url, + data: {'page': pageNum, 'favorite': favorites, 'mine': mine, 'showing': showing}, + dataType: 'json', + success: function (data) { + loading.hide(); + + msg_section.prepend(data.messages); + + messages.data('pages', data.num_pages); + messages.data('page', data.num_page); + + if (data.num_page < data.num_pages) { + more.show(); + } else { + more.hide(); + } + } + }); + }); } \ No newline at end of file diff --git a/chat/templates/chat/_message.html b/chat/templates/chat/_message.html index faa5658..e7a333f 100644 --- a/chat/templates/chat/_message.html +++ b/chat/templates/chat/_message.html @@ -1,3 +1,5 @@ +{% load chat_tags %} +

{{ talk_msg.user }} @@ -5,7 +7,7 @@ ({{ talk_msg.create_date }}) - diff --git a/chat/templates/chat/talk.html b/chat/templates/chat/talk.html index 8c6b997..63b59ec 100644 --- a/chat/templates/chat/talk.html +++ b/chat/templates/chat/talk.html @@ -22,7 +22,7 @@

{% trans 'Filter' %}

-
+
-
+
+ + +
{% trans 'You have new messages...' %}
diff --git a/chat/templatetags/chat_tags.py b/chat/templatetags/chat_tags.py index 7f5e0d8..f801b8b 100644 --- a/chat/templatetags/chat_tags.py +++ b/chat/templatetags/chat_tags.py @@ -7,7 +7,7 @@ from django.contrib.sessions.models import Session from log.models import Log -from chat.models import TalkMessages, ChatVisualizations +from chat.models import TalkMessages, ChatVisualizations, ChatFavorites register = template.Library() @@ -55,4 +55,25 @@ def last_message(chat): def notifies(chat, user): total = ChatVisualizations.objects.filter(message__talk = chat, user = user).count() - return total \ No newline at end of file + return total + +@register.filter(name = 'fav_label') +def fav_label(message, user): + if ChatFavorites.objects.filter(message = message, user = user).exists(): + return _('Unfavorite') + + return _('Favorite') + +@register.filter(name = 'fav_action') +def fav_action(message, user): + if ChatFavorites.objects.filter(message = message, user = user).exists(): + return "unfavorite" + + return "favorite" + +@register.filter(name = 'fav_class') +def fav_class(message, user): + if ChatFavorites.objects.filter(message = message, user = user).exists(): + return "btn_unfav" + + return "btn_fav" \ No newline at end of file diff --git a/chat/urls.py b/chat/urls.py index be3a30b..3384939 100644 --- a/chat/urls.py +++ b/chat/urls.py @@ -5,6 +5,8 @@ urlpatterns = [ url(r'^$', views.GeneralIndex.as_view(), name='manage_general'), url(r'^participants/$', views.GeneralParticipants.as_view(), name='participants_general'), url(r'^render_message/([\w_-]+)/$', views.render_message, name='render_message'), + url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), + url(r'^load_messages/([\w_-]+)/$', views.load_messages, name='load_messages'), url(r'^talk/(?P[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.GetTalk.as_view(), name = 'talk'), url(r'^send_message/(?P[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/(?P[\w_-]+)/(?P[\w_-]+)/(?P[\w_-]+)/$', views.SendMessage.as_view(), name = 'create'), url(r'^participant/profile/(?P[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.ParticipantProfile.as_view(), name = 'profile'), diff --git a/chat/views.py b/chat/views.py index 606cb93..15aa092 100644 --- a/chat/views.py +++ b/chat/views.py @@ -7,10 +7,12 @@ from django.http import JsonResponse from django.template.loader import render_to_string from django.core.urlresolvers import reverse, reverse_lazy import textwrap +from datetime import datetime from django.utils import formats from django.utils.html import strip_tags from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib.auth.decorators import login_required from django.db.models import Q from channels import Group @@ -20,7 +22,7 @@ from categories.models import Category from subjects.models import Subject from users.models import User -from .models import Conversation, GeneralTalk, CategoryTalk, SubjectTalk, TalkMessages, ChatVisualizations +from .models import Conversation, GeneralTalk, CategoryTalk, SubjectTalk, TalkMessages, ChatVisualizations, ChatFavorites from .forms import ChatMessageForm class GeneralIndex(LoginRequiredMixin, generic.ListView): @@ -81,7 +83,7 @@ class GeneralParticipants(LoginRequiredMixin, generic.ListView): context['title'] = _('Messages - Participants') context['totals'] = self.totals - context['search'] = self.request.GET.get('search') + context['search'] = self.request.GET.get('search', '') context['chat_menu_active'] = 'subjects_menu_active' return context @@ -222,8 +224,71 @@ def render_message(request, talk_msg): context = {} context['talk_msg'] = msg - message = _('Message sent successfully!') - html = render_to_string("chat/_message.html", context, request) - return JsonResponse({'message': message, 'view': html, 'new_id': msg.id}) \ No newline at end of file + return JsonResponse({'view': html, 'new_id': msg.id, 'talk_id': msg.talk.id}) + +@login_required +def favorite(request, message): + action = request.GET.get('action', '') + message = get_object_or_404(TalkMessages, id = message) + + if action == 'favorite': + ChatFavorites.objects.create(message = message, user = request.user) + + return JsonResponse({'label': _('Unfavorite')}) + else: + ChatFavorites.objects.filter(message = message, user = request.user).delete() + + return JsonResponse({'label': _('Favorite')}) + +def load_messages(request, talk): + context = { + 'request': request, + } + + user = request.user + favorites = request.GET.get('favorite', False) + mines = request.GET.get('mine', False) + showing = request.GET.get('showing', '') + n_views = 0 + + if not favorites: + if mines: + messages = TalkMessages.objects.filter(talk__id = talk, user = user) + else: + messages = TalkMessages.objects.filter(talk__id = talk) + else: + if mines: + messages = TalkMessages.objects.filter(talk__id = talk, chat_favorites_message__isnull = False, chat_favorites_message__user = user, user = user) + else: + messages = TalkMessages.objects.filter(talk__id = talk, chat_favorites_message__isnull = False, chat_favorites_message__user = user) + + if showing: #Exclude ajax creation messages results + showing = showing.split(',') + messages = messages.exclude(id__in = showing) + + has_page = request.GET.get('page', None) + + if has_page is None: + views = ChatVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(message__talk__id = talk))) + n_views = views.count() + views.update(viewed = True, date_viewed = datetime.now()) + + paginator = Paginator(messages.order_by("-create_date"), 20) + + try: + page_number = int(request.GET.get('page', 1)) + except ValueError: + raise Http404 + + try: + page_obj = paginator.page(page_number) + except EmptyPage: + raise Http404 + + context['messages'] = page_obj.object_list + + response = render_to_string("chat/_list_messages.html", context, request) + + return JsonResponse({"messages": response, "unviewed": n_views, "count": messages.count(), "num_pages": paginator.num_pages, "num_page": page_obj.number}) \ No newline at end of file -- libgit2 0.21.2