from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render, redirect
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from chat.models import ChatRoom
from gevent import queue


QUEUES = {'Test Chat Room 1': {'generic': queue.Queue(), }, }

list_of_queus = ['ssb', 'wow', 'lol', 'cs', 'destiny',
                 'mine', 'hearth', 'dota', 'diablo',
                 'local']


def check_queues():
    chatrooms = ChatRoom.objects.all()
    for chatroom in chatrooms:
        if chatroom.main in list_of_queus:
            QUEUES[chatroom.name] = {}

chatrooms = ChatRoom.objects.all()
for chatroom in chatrooms:
    QUEUES[chatroom.name] = {}


@csrf_exempt
def index(request):
    name = request.path.rsplit('/', 1)[1]
    chat_room = []
    for room in ChatRoom.objects.filter(main=name).all():
        users = len(room.subscribers.all())
        chat_room.append((room, users))
    context = {
        'chat_list': chat_room,
        'channel': name,
    }
    return render(request, 'chat/index.html', context)


@csrf_exempt
def create_room(request):
    request.user.profile.own_room = True
    request.user.profile.save()
    main = request.path.rsplit('/', 2)[-1]
    name = request.POST.get('Enter a New Room Name')
    new_room = ChatRoom()
    new_room.name = name
    new_room.owner = request.user.profile
    new_room.main = main
    new_room.save()
    QUEUES[name] = {}
    return chat_room(request, new_room.pk)


def chat_room(request, chat_room_id):
    chatroom = get_object_or_404(ChatRoom, pk=chat_room_id)
    room = ChatRoom.objects.get(pk=chat_room_id)
    context = {
        'chatroom': chatroom,
        'subs': room.subscribers.all(),
        'rooms': room.name,
        'queues': QUEUES,
    }
    if request.user.profile:
        room.add_subscriber(request.user.profile)
        QUEUES[chatroom.name][request.user.username] = queue.Queue()

    return render(request, 'chat/chat_room.html', context)


@csrf_exempt
def chat_add(request, chat_room_id):
    message = request.POST.get('message')
    chat_room = ChatRoom.objects.get(pk=chat_room_id).name
    for prof in QUEUES[chat_room]:
        msg = "{}:    {}".format(request.user.username, message)
        QUEUES[chat_room][prof].put_nowait(msg)

    return JsonResponse({'message': message})


@csrf_exempt
def chat_messages(request, chat_room_id):
    chat_room = ChatRoom.objects.get(pk=chat_room_id).name
    try:
        q = QUEUES[chat_room][request.user.username]
        msg = q.get(timeout=1)
        name = msg.split()[0][:-1]
        if request.user.profile.blocking.filter(user__username=name):
            msg = ["{}:    {}".format(name, 'blocked')]
    except queue.Empty:
        msg = []

    data = {
        'messages': msg,
    }

    return JsonResponse(data)


def delete_chatroom(request, chat_room_id):
    request.user.profile.own_room = False
    request.user.profile.save()
    if request.user.profile == ChatRoom.objects.get(pk=chat_room_id).owner:
        ChatRoom.objects.get(pk=chat_room_id).delete()
        request.user.profile.Created_room = False
    return redirect('/')

@@ -95,44 +95,8 @@
 rect
-%0Afrom django.shortcuts import render
 , ge
@@ -148,16 +148,53 @@
 esponse%0A
+from django.db import IntegrityError%0A
 from cha
@@ -1127,81 +1127,8 @@
 t):%0A
-    request.user.profile.own_room = True%0A    request.user.profile.save()%0A
     
@@ -1215,24 +1215,156 @@
 Room Name')%0A
+    try: %0A        if name.strip():%0A            request.user.profile.own_room = True%0A            request.user.profile.save()%0A        
     new_room
@@ -1377,16 +1377,24 @@
 tRoom()%0A
+        
     new_
@@ -1406,24 +1406,32 @@
 name = name%0A
+        
     new_room
@@ -1460,16 +1460,24 @@
 profile%0A
+        
     new_
@@ -1493,16 +1493,24 @@
  = main%0A
+        
     new_
@@ -1517,24 +1517,32 @@
 room.save()%0A
+        
     QUEUES%5Bn
@@ -1551,16 +1551,24 @@
 e%5D = %7B%7D%0A
+        
     retu
@@ -1570,35 +1570,43 @@
  return 
+redirect('/
 chat
-_
+/
 room
-(request, 
+/'+str(
 new_room
@@ -1609,16 +1609,110 @@
 room.pk)
+)%0A    except IntegrityError, AttributeError:%0A        pass%0A    return redirect('/chat/' + main)
 %0A%0A%0Adef c

fix typing in nothing into chat and if you create a room then go back to create another room