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)