add database to chatbox

This commit is contained in:
cuom1999 2020-03-19 16:51:56 -06:00
parent cb8eb2689c
commit 112f2b57c3
14 changed files with 77 additions and 104 deletions

View file

@ -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')

View file

@ -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'

View file

@ -4,4 +4,4 @@ from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/', consumers.ChatConsumer),
]
]

View file

@ -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'))