From 92d94af520eb58d6b20c104031ddcff32f6e759b Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Fri, 20 Mar 2020 22:48:04 -0600 Subject: [PATCH] almost chatbox --- chat_box/consumers.py | 10 +++-- chat_box/views.py | 27 +++++++++++- dmoj/urls.py | 6 ++- resources/base.scss | 4 +- resources/chatbox.scss | 5 ++- resources/widgets.scss | 4 +- templates/chat/chat.html | 91 +++++++++++++++++++++++++++------------- 7 files changed, 107 insertions(+), 40 deletions(-) diff --git a/chat_box/consumers.py b/chat_box/consumers.py index fd7b63f..4d6599b 100644 --- a/chat_box/consumers.py +++ b/chat_box/consumers.py @@ -2,6 +2,9 @@ import json from channels.generic.websocket import AsyncWebsocketConsumer from .models import Message from .views import format_time +from django.urls import reverse +from django.http import HttpResponse, HttpResponseRedirect + from judge.models.profile import Profile @@ -14,7 +17,7 @@ class ChatConsumer(AsyncWebsocketConsumer): # Join room group await self.channel_layer.group_add( self.room_group_name, - self.channel_name + self.channel_name, ) await self.accept() @@ -23,7 +26,7 @@ class ChatConsumer(AsyncWebsocketConsumer): # Leave room group await self.channel_layer.group_discard( self.room_group_name, - self.channel_name + self.channel_name, ) # Receive message from WebSocket @@ -58,6 +61,5 @@ def save_data_and_get_time(message): .get(id=message['author_id']), ) new_message.save() + HttpResponseRedirect(reverse('chat')) return new_message.time - - diff --git a/chat_box/views.py b/chat_box/views.py index 221560b..294f535 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -1,25 +1,50 @@ from django.utils.translation import gettext as _ from django.views.generic import ListView +from django.http import HttpResponse, HttpResponseRedirect +from django.shortcuts import render +from django.core.paginator import Paginator +from django.urls import reverse +from judge.jinja2.gravatar import gravatar from .models import Message +import json def format_time(time): return time.strftime('%H:%M %p %d-%m-%Y') +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) + + class ChatView(ListView): model = Message context_object_name = 'message' template_name = 'chat/chat.html' title = _('Chat Box') paginate_by = 50 + 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)) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['title'] = self.title - for msg in context['message']: + for msg in context['message']: msg.time = format_time(msg.time) return context diff --git a/dmoj/urls.py b/dmoj/urls.py index 0df902b..5f29558 100644 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -4,7 +4,7 @@ from django.conf.urls import include, url from django.contrib import admin from django.contrib.auth import views as auth_views from django.contrib.sitemaps.views import sitemap -from django.http import Http404, HttpResponsePermanentRedirect +from django.http import Http404, HttpResponsePermanentRedirect, HttpResponseRedirect from django.templatetags.static import static from django.urls import reverse from django.utils.functional import lazystr @@ -369,7 +369,9 @@ urlpatterns = [ url(r'^custom_checker_sample/', about.custom_checker_sample, name='custom_checker_sample'), url(r'^chat/', include([ - url(r'^$', login_required(ChatView.as_view()), name='chat'), + url(r'^$', + login_required(ChatView.as_view()), + name='chat'), ])), ] diff --git a/resources/base.scss b/resources/base.scss index 1a648e7..bd7b002 100644 --- a/resources/base.scss +++ b/resources/base.scss @@ -219,11 +219,11 @@ header { #nav-shadow { height: 2px; - background: linear-gradient(#63c8f9, transparent); + background: linear-gradient(#7dc7ff, transparent); } #nav-container { - background: linear-gradient(#c1e9fd, #63c8f9); + background: #7dc7ff; // opacity: 0.77; // filter: alpha(opacity=77) diff --git a/resources/chatbox.scss b/resources/chatbox.scss index 6677e0c..3839fce 100644 --- a/resources/chatbox.scss +++ b/resources/chatbox.scss @@ -7,6 +7,7 @@ overflow-wrap: break-word; overflow-y: scroll; } + #loader { display: block; margin-left: auto; @@ -16,6 +17,7 @@ #chat-log { padding: 0; + padding-top: 2em; width: 100%; } @@ -61,8 +63,9 @@ .clear { clear: both; } -.message { +.content-message { word-wrap: break-word; + white-space: pre-line; } diff --git a/resources/widgets.scss b/resources/widgets.scss index fb8787b..88768dc 100644 --- a/resources/widgets.scss +++ b/resources/widgets.scss @@ -305,8 +305,8 @@ ul.pagination { .active-page > { a { z-index: 2; - color: #FFF; - background-color: $highlight_blue; + color: black; + background-color: #7dc7ff; border-color: transparent; cursor: default; } diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 927d332..62e697c 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -7,6 +7,8 @@