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
|
2021-06-19 03:26:43 +00:00
|
|
|
from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest
|
2020-03-21 04:48:04 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-06-19 03:26:43 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
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
|
2021-06-19 03:26:43 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2020-01-27 20:37:52 +00:00
|
|
|
|
2021-06-19 03:26:43 +00:00
|
|
|
import datetime
|
2020-07-03 02:50:31 +00:00
|
|
|
|
2021-06-19 03:26:43 +00:00
|
|
|
from judge import event_poster as event
|
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
|
|
|
|
2020-03-16 02:23:14 +00:00
|
|
|
class ChatView(ListView):
|
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
|
2021-06-19 03:26:43 +00:00
|
|
|
messages = Message.objects.filter(hidden=False)
|
|
|
|
paginator = Paginator(messages, paginate_by)
|
2020-03-21 04:48:04 +00:00
|
|
|
|
2020-05-06 04:51:16 +00:00
|
|
|
def get_queryset(self):
|
2021-06-19 03:26:43 +00:00
|
|
|
return self.messages
|
2020-05-06 04:51:16 +00:00
|
|
|
|
2020-03-21 04:48:04 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
page = request.GET.get('page')
|
2021-06-19 03:26:43 +00:00
|
|
|
if page == None:
|
2020-03-21 04:48:04 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
cur_page = self.paginator.get_page(page)
|
2021-06-19 03:26:43 +00:00
|
|
|
|
|
|
|
return render(request, 'chat/message_list.html', {
|
|
|
|
'object_list': 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
|
|
|
|
2020-03-19 05:13:55 +00:00
|
|
|
context['title'] = self.title
|
2021-06-19 03:26:43 +00:00
|
|
|
context['last_msg'] = event.last()
|
|
|
|
context['online_users'] = get_user_online_status()
|
2020-06-08 23:19:09 +00:00
|
|
|
context['admin_status'] = get_admin_online_status()
|
2021-06-19 03:26:43 +00:00
|
|
|
context['today'] = timezone.now().strftime("%d-%m-%Y")
|
2020-03-19 05:13:55 +00:00
|
|
|
return context
|
2020-05-05 18:17:42 +00:00
|
|
|
|
2021-06-19 03:26:43 +00:00
|
|
|
|
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:
|
2021-06-19 03:26:43 +00:00
|
|
|
try:
|
|
|
|
messid = int(request.POST.get('message'))
|
|
|
|
mess = Message.objects.get(id=messid)
|
|
|
|
except:
|
|
|
|
return HttpResponseBadRequest()
|
2020-05-05 18:17:42 +00:00
|
|
|
|
2021-06-19 03:26:43 +00:00
|
|
|
mess.hidden = True
|
|
|
|
mess.save()
|
2020-05-05 18:17:42 +00:00
|
|
|
|
|
|
|
return JsonResponse(ret)
|
|
|
|
|
2020-05-22 17:16:42 +00:00
|
|
|
return JsonResponse(ret)
|
2021-06-19 03:26:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def post_message(request):
|
|
|
|
ret = {'msg': 'posted'}
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
return JsonResponse(ret)
|
|
|
|
|
|
|
|
new_message = Message(author=request.profile,
|
|
|
|
body=request.POST['body'])
|
|
|
|
new_message.save()
|
|
|
|
|
|
|
|
event.post('chat', {
|
|
|
|
'type': 'new_message',
|
|
|
|
'message': new_message.id,
|
|
|
|
})
|
|
|
|
|
|
|
|
return JsonResponse(ret)
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def chat_message_ajax(request):
|
|
|
|
if request.method != 'GET':
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
message_id = request.GET['message']
|
|
|
|
except KeyError:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
try:
|
|
|
|
message = Message.objects.filter(hidden=False).get(id=message_id)
|
|
|
|
except Message.DoesNotExist:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
return render(request, 'chat/message.html', {
|
|
|
|
'message': message,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_online_status():
|
|
|
|
last_five_minutes = timezone.now()-timezone.timedelta(minutes=5)
|
|
|
|
return Profile.objects \
|
|
|
|
.filter(display_rank='user',
|
|
|
|
last_access__gte = last_five_minutes)\
|
|
|
|
.order_by('-rating')
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def online_status_ajax(request):
|
|
|
|
return render(request, 'chat/online_status.html', {
|
|
|
|
'online_users': get_user_online_status(),
|
|
|
|
'admin_status': get_admin_online_status(),
|
|
|
|
})
|