add chat model
This commit is contained in:
parent
4c9f1e1508
commit
25325b4fd9
5 changed files with 79 additions and 11 deletions
|
@ -1,3 +0,0 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
30
chat_box/migrations/0001_initial.py
Normal file
30
chat_box/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
# Generated by Django 2.2.9 on 2020-01-28 05:07
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('judge', '0100_auto_20200127_0059'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Message',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('time', models.DateTimeField(auto_now_add=True, verbose_name='posted time')),
|
||||
('body', models.TextField(max_length=8192, verbose_name='body of comment')),
|
||||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='judge.Profile', verbose_name='user')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'message',
|
||||
'verbose_name_plural': 'messages',
|
||||
'ordering': ('-time',),
|
||||
},
|
||||
),
|
||||
]
|
|
@ -1,3 +1,45 @@
|
|||
from django.db import models
|
||||
# based on https://github.com/narrowfail/django-channels-chat
|
||||
|
||||
# Create your models here.
|
||||
from asgiref.sync import async_to_sync
|
||||
from channels.layers import get_channel_layer
|
||||
from django.db import models
|
||||
from django.db.models import CASCADE
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
from judge.models.profile import Profile
|
||||
|
||||
|
||||
__all__ = ['Message']
|
||||
|
||||
|
||||
class Message(models.Model):
|
||||
author = models.ForeignKey(Profile, verbose_name=_('user'), on_delete=CASCADE)
|
||||
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):
|
||||
# inform client that there is a new message
|
||||
notification = {
|
||||
'type': 'recieve_group_message',
|
||||
'message': '{}'.format(self.id)
|
||||
}
|
||||
channel_layer = get_channel_layer()
|
||||
# print("user.id {}".format(self.user.id))
|
||||
# print("user.id {}".format(self.recipient.id))
|
||||
|
||||
async_to_sync(channel_layer.group_send)("{}".format(self.user.id), notification)
|
||||
async_to_sync(channel_layer.group_send)("{}".format(self.recipient.id), notification)
|
||||
|
||||
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'
|
||||
verbose_name = 'message'
|
||||
verbose_name_plural = 'messages'
|
||||
ordering = ('-time',)
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
from django.shortcuts import render
|
||||
from django.utils.translation import gettext as _
|
||||
from django.views import View
|
||||
|
||||
|
||||
def chat(request):
|
||||
return render(request, 'chat/chat.html', {
|
||||
'title': _('Chat Box'),
|
||||
})
|
||||
class ChatView(View):
|
||||
template_name = 'chat.html'
|
|
@ -1,4 +1,4 @@
|
|||
from chat_box.views import chat
|
||||
from chat_box.views import ChatView
|
||||
from django.conf import settings
|
||||
from django.conf.urls import include, url
|
||||
from django.contrib import admin
|
||||
|
@ -367,7 +367,7 @@ urlpatterns = [
|
|||
|
||||
url(r'^custom_checker_sample/', about.custom_checker_sample, name='custom_checker_sample'),
|
||||
|
||||
url(r'chat/', chat, name='chat'),
|
||||
url(r'chat/', ChatView.as_view(), name='chat'),
|
||||
]
|
||||
|
||||
favicon_paths = ['apple-touch-icon-180x180.png', 'apple-touch-icon-114x114.png', 'android-chrome-72x72.png',
|
||||
|
|
Loading…
Reference in a new issue