[
{
"label": "Foo",
"pk": 5,
"message": [
{
"handle": "Bruce",
"message": "Yo"
},
{
"handle": "Sol",
"message": "Hey"
}
]
},
{
"label": "Bar",
"pk": 10,
"message": [
{
"handle": "Sol",
"message": "Hi"
},
{
"handle": "Alfred",
"message": "Yo"
}
]
}
]
@channel_session
def ws_connect(message):
prefix, label = message['path'].strip('/').split('/')
room = Room.objects.get(label=label)
chathistory = room.messages.all().order_by('timestamp')
Group('chat-' + label).add(message.reply_channel)
message.channel_session['room'] = room.label
message.reply_channel.send({'text': json.dumps([msg.as_dict() for msg in chathistory.all()])})
class Room(models.Model):
name = models.TextField()
label = models.SlugField(unique=True)
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())
@property
def formatted_timestamp(self):
return self.timestamp.strftime("%H:%M:%S")
def as_dict(self):
return {'pk':self.pk, 'handle': self.handle, 'message': self.message, 'timestamp': self.formatted_timestamp}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/153fb077-46b8-4e13-ab6d-d0a7d4f4dd65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/70247961-2d6f-49e4-bf30-e5c2975416d1%40googlegroups.com.
@channel_session
def ws_connect(message):
listrooms = User.objects.filter(chat__isnull=False).values('chat')
# `listrooms` gives the PK fields of the chat rooms
chathistory = room.messages.all().order_by('timestamp')
Group('chat-' + listrooms).add(message.reply_channel)
message.channel_session['listrooms'] = list(set(message.channel_session['listrooms']).union([room.id]))
message.reply_channel.send({'text': json.dumps([msg.as_dict() for msg in chathistory.all()])})
@channel_session
def ws_receive(message):
listrooms = User.objects.filter(chat__isnull=False).values('chat')
data = json.loads(message['text'])
m = listrooms.messages.create(handle=data['handle'], message=data['message'])
Group('chat-'+ listrooms).send({'text': json.dumps([m.as_dict()])})
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/529472b1-9323-45d8-951e-f5729472bb66%40googlegroups.com.
@channel_session
def ws_connect(message):
room_id = Tag.objects.filter(owner=1).filter(chat__isnull=False).values('chat__label').distinct()
print('I see values here', room_id)
for i in room_id:
try:
room = Room.objects.get(label=i['chat__label'])
chathistory = room.messages.all().order_by('timestamp')
Group('chat-' ).add(message.reply_channel)
message.channel_session['room'] = room
message.reply_channel.send({'text': json.dumps([msg.as_dict() for msg in chathistory.all()])})
except Exception as e:
print (str(e), ' Never happened')
pass
@channel_session
def ws_receive(message):
label = message.channel_session['room']
room = Room.objects.get(label=label)
data = json.loads(message['text'])
m = room.messages.create(handle=data['handle'], message=data['message'])
Group('chat-'+label).send({'text': json.dumps([m.as_dict()])})
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5453a756-2e87-47be-8e81-4359e200d0db%40googlegroups.com.