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-05-06 04:51:16 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.forms.models import model_to_dict
|
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
|
|
|
|
2020-03-21 04:48:04 +00:00
|
|
|
def format_messages(messages):
|
|
|
|
msg_list = [{
|
2020-05-23 23:13:42 +00:00
|
|
|
'time': msg.time,
|
2020-03-21 04:48:04 +00:00
|
|
|
'author': str(msg.author),
|
|
|
|
'body': msg.body,
|
|
|
|
'image': gravatar(msg.author, 32),
|
2020-05-06 04:51:16 +00:00
|
|
|
'id': msg.id
|
2020-03-21 04:48:04 +00:00
|
|
|
} for msg in messages]
|
2020-05-23 23:13:42 +00:00
|
|
|
return json.dumps(msg_list, default=str)
|
2020-03-21 04:48:04 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-06 04:51:16 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return Message.objects.filter(hidden=False)
|
|
|
|
|
2020-03-21 04:48:04 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
page = request.GET.get('page')
|
|
|
|
if (page == None):
|
2020-05-06 04:51:16 +00:00
|
|
|
# return render(request, 'chat/chat.html', {'message': format_messages(Message.objects.all())})
|
2020-03-21 04:48:04 +00:00
|
|
|
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-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:
|
2020-05-06 04:51:16 +00:00
|
|
|
messid = int(request.POST.get('messid'))
|
2020-05-05 18:17:42 +00:00
|
|
|
all_mess = Message.objects.all()
|
|
|
|
|
|
|
|
for mess in all_mess:
|
2020-05-06 04:51:16 +00:00
|
|
|
if mess.id == messid:
|
2020-05-05 18:17:42 +00:00
|
|
|
mess.hidden = True
|
|
|
|
mess.save()
|
2020-05-23 23:13:42 +00:00
|
|
|
new_elt = {'time': mess.time, 'content': mess.body}
|
2020-05-05 18:17:42 +00:00
|
|
|
ret = new_elt
|
2020-05-06 04:51:16 +00:00
|
|
|
break
|
2020-05-05 18:17:42 +00:00
|
|
|
|
|
|
|
return JsonResponse(ret)
|
|
|
|
|
2020-05-22 17:16:42 +00:00
|
|
|
return JsonResponse(ret)
|