2020-01-27 20:37:52 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-03-16 02:23:14 +00:00
|
|
|
from django.views.generic import ListView
|
2020-05-05 18:17:42 +00:00
|
|
|
from django.http import HttpResponse, JsonResponse
|
2020-03-21 04:48:04 +00:00
|
|
|
from django.core.paginator import Paginator
|
2020-01-27 20:37:52 +00:00
|
|
|
|
2020-03-21 04:48:04 +00:00
|
|
|
from judge.jinja2.gravatar import gravatar
|
2020-03-16 02:23:14 +00:00
|
|
|
from .models import Message
|
2020-03-21 04:48:04 +00:00
|
|
|
import json
|
2020-01-27 20:37:52 +00:00
|
|
|
|
2020-03-16 02:23:14 +00:00
|
|
|
|
2020-03-20 21:34:33 +00:00
|
|
|
def format_time(time):
|
|
|
|
return time.strftime('%H:%M %p %d-%m-%Y')
|
|
|
|
|
|
|
|
|
2020-03-21 04:48:04 +00:00
|
|
|
def format_messages(messages):
|
|
|
|
msg_list = [{
|
|
|
|
'time': format_time(msg.time),
|
|
|
|
'author': str(msg.author),
|
|
|
|
'body': msg.body,
|
|
|
|
'image': gravatar(msg.author, 32),
|
|
|
|
} for msg in messages]
|
|
|
|
return json.dumps(msg_list)
|
|
|
|
|
|
|
|
|
2020-03-16 02:23:14 +00:00
|
|
|
class ChatView(ListView):
|
|
|
|
model = Message
|
2020-03-19 22:51:56 +00:00
|
|
|
context_object_name = 'message'
|
2020-03-16 02:23:14 +00:00
|
|
|
template_name = 'chat/chat.html'
|
2020-03-19 05:13:55 +00:00
|
|
|
title = _('Chat Box')
|
2020-03-20 21:34:33 +00:00
|
|
|
paginate_by = 50
|
2020-05-05 18:17:42 +00:00
|
|
|
paginator = Paginator(Message.objects.filter(hidden=False), paginate_by)
|
2020-03-21 04:48:04 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
page = request.GET.get('page')
|
|
|
|
if (page == None):
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
cur_page = self.paginator.get_page(page)
|
|
|
|
return HttpResponse(format_messages(cur_page.object_list))
|
2020-03-19 05:13:55 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2020-03-19 22:51:56 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-19 05:13:55 +00:00
|
|
|
context['title'] = self.title
|
2020-03-20 21:34:33 +00:00
|
|
|
|
2020-03-21 04:48:04 +00:00
|
|
|
for msg in context['message']:
|
2020-03-20 21:34:33 +00:00
|
|
|
msg.time = format_time(msg.time)
|
|
|
|
|
2020-03-19 05:13:55 +00:00
|
|
|
return context
|
2020-05-05 18:17:42 +00:00
|
|
|
|
|
|
|
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)
|