Fix chat box and add Delete feature

This commit is contained in:
thanhluong 2020-05-05 18:17:42 +00:00
parent 005ac64be7
commit 5b1de72270
5 changed files with 51 additions and 5 deletions

View file

@ -15,6 +15,7 @@ class Message(models.Model):
author = models.ForeignKey(Profile, verbose_name=_('user'), on_delete=CASCADE)
time = models.DateTimeField(verbose_name=_('posted time'), auto_now_add=True)
body = models.TextField(verbose_name=_('body of comment'), max_length=8192)
hidden = models.BooleanField(verbose_name='is hidden', default=False)
def save(self, *args, **kwargs):
new_message = self.id

View file

@ -2,6 +2,7 @@ from django.urls import re_path
from . import consumers
ASGI_APPLICATION = "chat_box.routing.application"
websocket_urlpatterns = [
re_path(r'ws/chat/', consumers.ChatConsumer),
]
]

View file

@ -1,6 +1,6 @@
from django.utils.translation import gettext as _
from django.views.generic import ListView
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from django.core.paginator import Paginator
from judge.jinja2.gravatar import gravatar
@ -28,7 +28,7 @@ class ChatView(ListView):
template_name = 'chat/chat.html'
title = _('Chat Box')
paginate_by = 50
paginator = Paginator(Message.objects.all(), paginate_by)
paginator = Paginator(Message.objects.filter(hidden=False), paginate_by)
def get(self, request, *args, **kwargs):
page = request.GET.get('page')
@ -46,3 +46,25 @@ class ChatView(ListView):
msg.time = format_time(msg.time)
return context
def delete_message(request):
ret = {'delete': 'done'}
if request.method == 'GET':
return JsonResponse(ret)
if request.user.is_staff:
author = request.POST.get('author')
time = request.POST.get('messtime')
all_mess = Message.objects.all()
for mess in all_mess:
if mess.author.__str__() == author and format_time(mess.time) == time:
mess.hidden = True
mess.save()
new_elt = {'time': format_time(mess.time), 'content': mess.body}
ret = new_elt
return JsonResponse(ret)
return JsonResponse(ret)

View file

@ -1,4 +1,4 @@
from chat_box.views import ChatView
from chat_box.views import ChatView, delete_message
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
@ -372,6 +372,7 @@ urlpatterns = [
url(r'^$',
login_required(ChatView.as_view()),
name='chat'),
url(r'^delete/$', delete_message, name='delete_message')
])),
]

View file

@ -38,6 +38,9 @@
${user}
</a>
<span class="time">${time}</span>
{% if request.user.is_staff %}
<a id="remove_btn_${user}_" class="chatbtn_remove_mess" data-messtime="${time}" data-author="${user}">Delete</a>
{% endif %}
</div>
<span class="content-message">${content} </span>
</div>
@ -55,7 +58,9 @@
(function init_chatlog() {
ul = $('#chat-log')
{% for msg in message %}
loadMessage(`{{msg.body}}`, `{{msg.author}}`, `{{msg.time}}`, `{{gravatar(msg.author, 32)}}`)
{% if not msg.hidden %}
loadMessage(`{{msg.body}}`, `{{msg.author}}`, `{{msg.time}}`, `{{gravatar(msg.author, 32)}}`)
{% endif %}
{% endfor %}
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
})()
@ -97,6 +102,21 @@
scrollContainer($('#chat-box'), $('#loader'))
$(document).on("click", ".chatbtn_remove_mess", function() {
var elt = $(this);
console.log(elt.data());
$.ajax({
url: 'delete/',
type: 'post',
data: elt.data(),
dataType: 'json',
success: function(data){
console.log(data);
console.log('delete ajax call success!');
location.reload();
}
});
});
$("#chat-submit").click(function() {
if ($("#chat-input").val().trim()) {
@ -158,6 +178,7 @@
{% endblock js_media %}
{% block body %}
{% csrf_token %}
<div id="chat-area">
<div id="chat-box">
<img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif" id="loader">