This repository has been archived by the owner. It is now read-only.
Switch branches/tags
Nothing to show
Find file Copy path
ae14d55 Mar 5, 2016
1 contributor

Users who have contributed to this file

42 lines (35 sloc) 1.25 KB
import random
import string
from django.db import transaction
from django.shortcuts import render, redirect
import haikunator
from .models import Room
def about(request):
return render(request, "chat/about.html")
def new_room(request):
Randomly create a new room, and redirect to it.
new_room = None
while not new_room:
with transaction.atomic():
label = haikunator.haikunate()
if Room.objects.filter(label=label).exists():
new_room = Room.objects.create(label=label)
return redirect(chat_room, label=label)
def chat_room(request, label):
Room view - show the room, with latest messages.
The template for this view has the WebSocket business to send and stream
messages, so see the template for where the magic happens.
# If the room with the given label doesn't exist, automatically create it
# upon first visit (a la etherpad).
room, created = Room.objects.get_or_create(label=label)
# We want to show the last 50 messages, ordered most-recent-last
messages = reversed(room.messages.order_by('-timestamp')[:50])
return render(request, "chat/room.html", {
'room': room,
'messages': messages,