NDOJ/chat_box/views.py

165 lines
4.4 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
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
2021-07-02 06:38:48 +00:00
from django.db.models import Case, BooleanField
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()
2021-07-02 06:38:48 +00:00
context['status_sections'] = get_status_context(self.request)
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)\
2021-07-02 06:38:48 +00:00
.annotate(is_online=Case(default=True,output_field=BooleanField()))\
2021-06-19 03:26:43 +00:00
.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
2021-07-02 06:38:48 +00:00
def get_status_context(request):
friend_list = request.profile.get_friends()
all_user_status = get_user_online_status()
friend_status = []
user_status = []
for user in all_user_status:
if user.username in friend_list:
friend_status.append(user)
else:
user_status.append(user)
return [
{
'title': 'Admins',
'user_list': get_admin_online_status(),
},
{
'title': 'Following',
'user_list': friend_status,
},
{
'title': 'Users',
'user_list': user_status,
},
]
2021-06-19 03:26:43 +00:00
@login_required
def online_status_ajax(request):
return render(request, 'chat/online_status.html', {
2021-07-02 06:38:48 +00:00
'status_sections': get_status_context(request),
2021-06-19 03:26:43 +00:00
})