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-03-21 04:48:04 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.urls import reverse
|
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-03-21 04:48:04 +00:00
|
|
|
paginator = Paginator(Message.objects.all(), paginate_by)
|
|
|
|
|
|
|
|
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
|