Add authentication to chatbox

This commit is contained in:
cuom1999 2020-06-15 15:19:00 -05:00
parent 3876896b97
commit 7f9245570f
2 changed files with 12 additions and 9 deletions

View file

@ -5,6 +5,7 @@ from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.core import serializers from django.core import serializers
from judge.jinja2.gravatar import gravatar
from judge.models.profile import Profile from judge.models.profile import Profile
@ -32,8 +33,15 @@ class ChatConsumer(AsyncWebsocketConsumer):
async def receive(self, text_data): async def receive(self, text_data):
text_data_json = json.loads(text_data) text_data_json = json.loads(text_data)
message = text_data_json['message'] message = text_data_json['message']
message_saved = save_data_and_return(message) author = self.scope['user']
author = Profile.objects.get(id=author.id)
message['author'] = author.username
message['css_class'] = author.css_class
message['image'] = gravatar(author, 32)
message_saved = save_data_and_return(message, author)
message['time'] = message_saved[0]['fields']['time'] message['time'] = message_saved[0]['fields']['time']
message['id'] = message_saved[0]['pk'] message['id'] = message_saved[0]['pk']
@ -56,10 +64,9 @@ class ChatConsumer(AsyncWebsocketConsumer):
# return time # return time
def save_data_and_return(message): def save_data_and_return(message, author):
new_message = Message(body=message['body'], new_message = Message(body=message['body'],
author=Profile.objects author=author,
.get(pk=message['author_id']),
) )
new_message.save() new_message.save()
json_data = serializers.serialize("json", json_data = serializers.serialize("json",

View file

@ -141,10 +141,6 @@
message = { message = {
'body': body, 'body': body,
'image': img,
'author': '{{request.profile}}',
'author_id': {{request.profile.id}},
'css_class': '{{request.profile.css_class}}',
} }
chatSocket.send(JSON.stringify({ chatSocket.send(JSON.stringify({