NDOJ/chat_box/views.py

98 lines
3.1 KiB
Python
Raw Normal View History

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-06-08 20:11:07 +00:00
from django.utils import timezone
2020-01-27 20:37:52 +00:00
2020-07-03 02:50:31 +00:00
2020-03-21 04:48:04 +00:00
from judge.jinja2.gravatar import gravatar
2020-06-08 20:11:07 +00:00
from .models import Message, Profile
2020-03-21 04:48:04 +00:00
import json
2020-06-08 20:11:07 +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-05-23 23:52:19 +00:00
'author': msg.author,
2020-03-21 04:48:04 +00:00
'body': msg.body,
'image': gravatar(msg.author, 32),
2020-05-23 23:52:19 +00:00
'id': msg.id,
'css_class': msg.author.css_class,
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-06-08 23:19:09 +00:00
def get_admin_online_status():
all_admin = Profile.objects.filter(display_rank='admin')
last_five_minutes = timezone.now()-timezone.timedelta(minutes=5)
ret = []
for admin in all_admin:
is_online = False
if (admin.last_access >= last_five_minutes):
is_online = True
ret.append({'user': admin, 'is_online': is_online})
return ret
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-10-26 19:39:47 +00:00
# hard code, should be fixed later
address = f'{self.request.get_host()}/ws/chat/'
if self.request.is_secure():
context['ws_address'] = f'wss://{address}'
else:
context['ws_address'] = f'ws://{address}'
2020-03-19 05:13:55 +00:00
context['title'] = self.title
2020-06-08 20:11:07 +00:00
last_five_minutes = timezone.now()-timezone.timedelta(minutes=5)
context['online_users'] = Profile.objects \
2020-06-08 23:19:09 +00:00
.filter(display_rank='user',
last_access__gte = last_five_minutes)\
.order_by('-rating')
context['admin_status'] = get_admin_online_status()
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)