diff --git a/chat_box/models.py b/chat_box/models.py index b895f8d..315073f 100644 --- a/chat_box/models.py +++ b/chat_box/models.py @@ -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 diff --git a/chat_box/routing.py b/chat_box/routing.py index 6a8fe06..a91c8ad 100644 --- a/chat_box/routing.py +++ b/chat_box/routing.py @@ -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), -] \ No newline at end of file +] diff --git a/chat_box/views.py b/chat_box/views.py index d92af6d..84cc58d 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -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) \ No newline at end of file diff --git a/dmoj/urls.py b/dmoj/urls.py index 5f29558..33798d9 100644 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -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') ])), ] diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 62e697c..97356d5 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -38,6 +38,9 @@ ${user} ${time} + {% if request.user.is_staff %} + Delete + {% endif %} @@ -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 %}