-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
37 lines (34 loc) · 1.32 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from django.shortcuts import render, redirect
from chat.models import Room,Message
from django.http import HttpResponse,JsonResponse
# Create your views here.
def home(request):
return render(request,'home.html')
def room(request,room):
username = request.GET.get('username')
room_details = Room.objects.get(name=room)
return render(request,'room.html', {
'username':username,
'room': room,
'room_details': room_details
})
def checkview(request):
room = request.POST['room_name']
username = request.POST['username']
if Room.objects.filter(name=room).exists():
return redirect('/'+room+'/?username='+username)
else:
new_room = Room.objects.create(name=room)
new_room.save()
return redirect('/'+room+'/?username='+username)
def send(request):
message = request.POST['message']
username = request.POST['username']
room_id = request.POST['room_id']
new_message = Message.objects.create(value=message,user=username,room=room_id)
new_message.save()
return HttpResponse('Message sent Successfully')
def getMessages(request , room):
room_details = Room.objects.get(name=room)
messages = Message.objects.filter(room=room_details.id)
return JsonResponse({"messages":list(messages.values())})