Fix chat box and add Delete feature
This commit is contained in:
parent
005ac64be7
commit
5b1de72270
5 changed files with 51 additions and 5 deletions
|
@ -15,6 +15,7 @@ class Message(models.Model):
|
||||||
author = models.ForeignKey(Profile, verbose_name=_('user'), on_delete=CASCADE)
|
author = models.ForeignKey(Profile, verbose_name=_('user'), on_delete=CASCADE)
|
||||||
time = models.DateTimeField(verbose_name=_('posted time'), auto_now_add=True)
|
time = models.DateTimeField(verbose_name=_('posted time'), auto_now_add=True)
|
||||||
body = models.TextField(verbose_name=_('body of comment'), max_length=8192)
|
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):
|
def save(self, *args, **kwargs):
|
||||||
new_message = self.id
|
new_message = self.id
|
||||||
|
|
|
@ -2,6 +2,7 @@ from django.urls import re_path
|
||||||
|
|
||||||
from . import consumers
|
from . import consumers
|
||||||
|
|
||||||
|
ASGI_APPLICATION = "chat_box.routing.application"
|
||||||
websocket_urlpatterns = [
|
websocket_urlpatterns = [
|
||||||
re_path(r'ws/chat/', consumers.ChatConsumer),
|
re_path(r'ws/chat/', consumers.ChatConsumer),
|
||||||
]
|
]
|
|
@ -1,6 +1,6 @@
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from django.views.generic import ListView
|
from django.views.generic import ListView
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, JsonResponse
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
|
|
||||||
from judge.jinja2.gravatar import gravatar
|
from judge.jinja2.gravatar import gravatar
|
||||||
|
@ -28,7 +28,7 @@ class ChatView(ListView):
|
||||||
template_name = 'chat/chat.html'
|
template_name = 'chat/chat.html'
|
||||||
title = _('Chat Box')
|
title = _('Chat Box')
|
||||||
paginate_by = 50
|
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):
|
def get(self, request, *args, **kwargs):
|
||||||
page = request.GET.get('page')
|
page = request.GET.get('page')
|
||||||
|
@ -46,3 +46,25 @@ class ChatView(ListView):
|
||||||
msg.time = format_time(msg.time)
|
msg.time = format_time(msg.time)
|
||||||
|
|
||||||
return context
|
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)
|
|
@ -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 import settings
|
||||||
from django.conf.urls import include, url
|
from django.conf.urls import include, url
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
@ -372,6 +372,7 @@ urlpatterns = [
|
||||||
url(r'^$',
|
url(r'^$',
|
||||||
login_required(ChatView.as_view()),
|
login_required(ChatView.as_view()),
|
||||||
name='chat'),
|
name='chat'),
|
||||||
|
url(r'^delete/$', delete_message, name='delete_message')
|
||||||
|
|
||||||
])),
|
])),
|
||||||
]
|
]
|
||||||
|
|
|
@ -38,6 +38,9 @@
|
||||||
${user}
|
${user}
|
||||||
</a>
|
</a>
|
||||||
<span class="time">${time}</span>
|
<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>
|
</div>
|
||||||
<span class="content-message">${content} </span>
|
<span class="content-message">${content} </span>
|
||||||
</div>
|
</div>
|
||||||
|
@ -55,7 +58,9 @@
|
||||||
(function init_chatlog() {
|
(function init_chatlog() {
|
||||||
ul = $('#chat-log')
|
ul = $('#chat-log')
|
||||||
{% for msg in message %}
|
{% 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 %}
|
{% endfor %}
|
||||||
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
|
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
|
||||||
})()
|
})()
|
||||||
|
@ -97,6 +102,21 @@
|
||||||
|
|
||||||
scrollContainer($('#chat-box'), $('#loader'))
|
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() {
|
$("#chat-submit").click(function() {
|
||||||
if ($("#chat-input").val().trim()) {
|
if ($("#chat-input").val().trim()) {
|
||||||
|
@ -158,6 +178,7 @@
|
||||||
{% endblock js_media %}
|
{% endblock js_media %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
{% csrf_token %}
|
||||||
<div id="chat-area">
|
<div id="chat-area">
|
||||||
<div id="chat-box">
|
<div id="chat-box">
|
||||||
<img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif" id="loader">
|
<img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif" id="loader">
|
||||||
|
|
Loading…
Reference in a new issue