N
N
nurzhannogerbek2017-03-21 18:30:58
Django
nurzhannogerbek, 2017-03-21 18:30:58

Group chat in Django channels?

Trying to create a group chat using Django Channels. There is a model Project (Project). Every project should have a group chat where project members can have a discussion. Unfortunately there was an error and I have no idea where exactly I made the mistake. What am I doing wrong, what have I missed? And how to create a group chat in Django so that there is a binding in a specific project by its id (in my case, project_code).
models.py :

class Room(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)

    def __str__(self):
        return self.code

    def get_absolute_url(self):
        return reverse('project:chat_room', args=[self.project_code])


class Message(models.Model):
    room = models.ForeignKey(Room, related_name='messages')
    handle = models.TextField()
    message = models.TextField()
    timestamp = models.DateTimeField(default=timezone.now, db_index=True)

    def __unicode__(self):
        return '[{timestamp}] {handle}: {message}'.format(**self.as_dict())

    def __str__(self):
        return self.__unicode__()

    @property
    def formatted_timestamp(self):
        return self.timestamp.strftime('%b %-d %-I:%M %p')

    def as_dict(self):
        return {'handle': self.handle, 'message': self.message, 'timestamp': self.formatted_timestamp}

URLs.py :
url(r'^(?P<project_code>[0-9a-f-]+)/chat_room/$',  chat_room, name='chat_room'),

views.py:
@login_required
def chat_room(request, project_code):
    room, created = Room.objects.get_or_create(project=project_code)
    messages = reversed(room.messages.order_by('-timestamp')[:50])
    return render(request, "project/chat/room.html", {'room': room, 'messages': messages,})

routing.py :
channel_routing = [
    route('websocket.connect', ws_connect),
    route("websocket.receive", ws_receive),
    route("websocket.disconnect", ws_disconnect),
]

consumers.py :
@channel_session
def ws_connect(message):
    prefix, project_code = message['path'].strip('/')
    room = Room.objects.get(project=project_code)
    message.reply_channel.send({"accept": True})
    Group('chat-' + project_code).add(message.reply_channel)
    message.channel_session['room'] = room.project


@channel_session
def ws_receive(message):
    project_code = message.channel_session['room']
    room = Room.objects.get(project=project_code)
    data = json.loads(message['text'])
    m = room.messages.create(handle=data['handle'], message=data['message'])
    Group('chat-' + project_code).send({'text': json.dumps(m.as_dict())})


@channel_session
def ws_disconnect(message):
    project_code = message.channel_session['room']
    Group('chat-' + project_code).discard(message.reply_channel)

settings.py :
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
        "ROUTING": "RMS.routing.channel_routing",
    },
}

ERROR:
Traceback (most recent call last):
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 473, in get_or_create
return self.get(**lookup), False
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 385, in get
self.model._meta.object_name
project.models.DoesNotExist: Room matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\ handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e , request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\channels\handler.py", line 237, in process_exception_by_middleware
return super(AsgiHandler, self). process_exception_by_middleware(exception, request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
return view_func(request, *args , **kwargs)
File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 176, in chat_room
room, created = Room.objects.get_or_create(project=project_code)
File "C:\Users \Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(* args, **kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 475, in get_or_create
return self._create_object_from_params(lookup, params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 505, in _create_object_from_params
obj = self.create(* *params)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 397, in create
obj = self. model(**kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 537, in __init__
setattr (self, field.name, rel_obj)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\fields\related_descriptors.py", line 211, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'1744d0bc-e439-4562-9c29-4e7b2451f39c'": "Room.project" must be a "Project" instance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-03-21
@dmtrrr

> redis.exceptions.ConnectionError: Does
n't that say anything?
Failed to connect to redis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question