diff --git a/chat_box/consumers.py b/chat_box/consumers.py index d200968..8e9db85 100644 --- a/chat_box/consumers.py +++ b/chat_box/consumers.py @@ -1,17 +1,19 @@ import json - from channels.generic.websocket import AsyncWebsocketConsumer +from .models import Message + +from judge.models.profile import Profile class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): - self.room_name = 'common' + self.room_name = 'room' self.room_group_name = 'chat_%s' % self.room_name # Join room group await self.channel_layer.group_add( self.room_group_name, - self.channel_name, + self.channel_name ) await self.accept() @@ -20,7 +22,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 @@ -40,8 +42,23 @@ class ChatConsumer(AsyncWebsocketConsumer): # Receive message from room group async def chat_message(self, event): message = event['message'] - + time = save_data_and_get_time(message) + message['time'] = format_time(time) # Send message to WebSocket await self.send(text_data=json.dumps({ 'message': message, })) + + +# return time +def save_data_and_get_time(message): + new_message = Message(body=message['body'], + author=Profile.objects + .get(id=message['author_id']), + ) + new_message.save() + return new_message.time + + +def format_time(time): + return time.strftime('%H:%M %p %d-%m-%Y') \ No newline at end of file diff --git a/chat_box/models.py b/chat_box/models.py index 748e933..b895f8d 100644 --- a/chat_box/models.py +++ b/chat_box/models.py @@ -1,5 +1,3 @@ -# based on https://github.com/narrowfail/django-channels-chat - from asgiref.sync import async_to_sync from channels.layers import get_channel_layer from django.db import models @@ -18,19 +16,10 @@ class Message(models.Model): time = models.DateTimeField(verbose_name=_('posted time'), auto_now_add=True) body = models.TextField(verbose_name=_('body of comment'), max_length=8192) - def notify_ws_clients(self): - notification = { - 'type': 'recieve_group_message', - 'message': '{}'.format(self.id) - } - channel_layer = get_channel_layer() - def save(self, *args, **kwargs): new_message = self.id self.body = self.body.strip() super(Message, self).save(*args, **kwargs) - if new_message is None: - self.notify_ws_clients() class Meta: app_label = 'chat_box' diff --git a/chat_box/routing.py b/chat_box/routing.py index f420f4f..6a8fe06 100644 --- a/chat_box/routing.py +++ b/chat_box/routing.py @@ -4,4 +4,4 @@ from . import consumers websocket_urlpatterns = [ re_path(r'ws/chat/', consumers.ChatConsumer), -] +] \ No newline at end of file diff --git a/chat_box/views.py b/chat_box/views.py index d507c98..6a339a2 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -1,31 +1,17 @@ -from django.http import HttpResponseRedirect from django.utils.translation import gettext as _ from django.views.generic import ListView -from django.urls import reverse -from django.utils import timezone - from .models import Message class ChatView(ListView): model = Message - context_object_name = 'messages' + context_object_name = 'message' template_name = 'chat/chat.html' title = _('Chat Box') + paginate_by = 10 def get_context_data(self, **kwargs): - context = super(ChatView, self).get_context_data(**kwargs) + context = super().get_context_data(**kwargs) context['title'] = self.title return context - - def get_queryset(self): - return None - - -def send(request): - new_message = Message(body=request.POST['message'], - author=request.profile, - time=timezone.now()) - new_message.save() - return HttpResponseRedirect(reverse('chat')) diff --git a/dmoj/routing.py b/dmoj/routing.py index 42238b7..72e0379 100644 --- a/dmoj/routing.py +++ b/dmoj/routing.py @@ -1,13 +1,12 @@ -import chat_box.routing from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter - +import chat_box.routing application = ProtocolTypeRouter({ # (http->django views is added by default) 'websocket': AuthMiddlewareStack( URLRouter( - chat_box.routing.websocket_urlpatterns, - ), + chat_box.routing.websocket_urlpatterns + ) ), -}) +}) \ No newline at end of file diff --git a/dmoj/settings.py b/dmoj/settings.py index 3f2b4cb..1040998 100644 --- a/dmoj/settings.py +++ b/dmoj/settings.py @@ -503,6 +503,8 @@ TESTCASE_VISIBLE_LENGTH = 60 DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240 DATA_UPLOAD_MAX_MEMORY_SIZE = 2621440 +MESSAGES_TO_LOAD = 15 + ASGI_APPLICATION = 'dmoj.routing.application' CHANNEL_LAYERS = { 'default': { @@ -511,4 +513,4 @@ CHANNEL_LAYERS = { "hosts": [('0.0.0.0', 6379)], }, }, -} +} \ No newline at end of file diff --git a/dmoj/urls.py b/dmoj/urls.py index c13ab03..0df902b 100644 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -1,4 +1,4 @@ -from chat_box.views import ChatView, send +from chat_box.views import ChatView from django.conf import settings from django.conf.urls import include, url from django.contrib import admin @@ -10,6 +10,8 @@ from django.urls import reverse from django.utils.functional import lazystr from django.utils.translation import ugettext_lazy as _ from django.views.generic import RedirectView +from django.contrib.auth.decorators import login_required + from judge.feed import AtomBlogFeed, AtomCommentFeed, AtomProblemFeed, BlogFeed, CommentFeed, ProblemFeed from judge.forms import CustomAuthenticationForm @@ -25,7 +27,6 @@ from judge.views.select2 import AssigneeSelect2View, CommentSelect2View, Contest ContestUserSearchSelect2View, OrganizationSelect2View, ProblemSelect2View, TicketUserSelect2View, \ UserSearchSelect2View, UserSelect2View - admin.autodiscover() register_patterns = [ @@ -368,8 +369,8 @@ urlpatterns = [ url(r'^custom_checker_sample/', about.custom_checker_sample, name='custom_checker_sample'), url(r'^chat/', include([ - url(r'^$', ChatView.as_view(), name='chat'), - url(r'send$', send, name='send_message') + url(r'^$', login_required(ChatView.as_view()), name='chat'), + ])), ] diff --git a/resources/base.scss b/resources/base.scss index 688ff31..1a648e7 100644 --- a/resources/base.scss +++ b/resources/base.scss @@ -219,7 +219,7 @@ header { #nav-shadow { height: 2px; - background: linear-gradient($widget_black, transparent); + background: linear-gradient(#63c8f9, transparent); } #nav-container { @@ -281,7 +281,7 @@ nav { height: 18px; &:link { - color: #FFF; + color: #9c3706; } &:hover { diff --git a/resources/icons/logo.png b/resources/icons/logo.png index 2ac6656..9eb296a 100644 Binary files a/resources/icons/logo.png and b/resources/icons/logo.png differ diff --git a/resources/icons/logo.svg b/resources/icons/logo.svg deleted file mode 100644 index 3a3e1ab..0000000 --- a/resources/icons/logo.svg +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/resources/table.scss b/resources/table.scss index f0901d6..5570ff2 100644 --- a/resources/table.scss +++ b/resources/table.scss @@ -42,7 +42,7 @@ $table_header_rounding: 6px; th { height: 2em; - color: #2b333b; + color: white; background-color: $widget_black; border-color: #cccccc; border-width: 1px 1px 0 0; diff --git a/resources/vars.scss b/resources/vars.scss index b599307..bd43b26 100644 --- a/resources/vars.scss +++ b/resources/vars.scss @@ -1,5 +1,5 @@ $highlight_blue: white; -$widget_black: #63c8f9;//68BBE3, add8e6 +$widget_black: #00007d;//68BBE3, add8e6 $border_gray: #ccc; $background_gray: #ededed; $background_light_gray: #fafafa; diff --git a/resources/widgets.scss b/resources/widgets.scss index a819d77..fb8787b 100644 --- a/resources/widgets.scss +++ b/resources/widgets.scss @@ -419,7 +419,7 @@ ul.select2-selection__rendered { margin: 0 -5px; background: $widget_black; border-radius: $widget_border_radius $widget_border_radius 0 0; - color: #2b333b; + color: white; padding-top: 5px; padding-bottom: 5px; padding-left: 7px; @@ -427,7 +427,7 @@ ul.select2-selection__rendered { } .sidebox h3 .fa { - color: #2b333b; + color: white; float: right; margin: 0.2em 0.4em 0 0; } diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 826c093..1a2e17a 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -1,7 +1,7 @@ -{% if request.user.is_authenticated %} {% extends "base.html" %} - {% block js_media %} + +