I have a form to input Line coordinates at 127.0.0.1:8000/dashboard/ and an "Ok" button to submit the coordinates. The coordinates are posted at 127.0.0.1:8000/api/line/ by calling the view LineDisplay(). Here I want to push the Line coordinates back to 127.0.01:8000/dashboard/.
I have done the following so far:
urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^api/line/$',views.LineDisplay.as_view()),
]
view.py:
class LineDisplay(APIView):
"""
Display the most recent line
"""
def get(self, request, format=None):
lines = Line.objects.all()
serializer = LineSerializer(lines, many=True)
return Response(serializer.data)
def post(self, request, format=None):
lines = Line.objects.all()
for line in lines:
line.delete();
serializer = LineSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
info = ""
info += "Line Coordinates are: "
lines = Line.objects.all()
for line in lines:
info += "x1:" + str(line.x1)
info += " y1:" + str(line.y1)
info += " x2:" + str(line.x2)
info += " y2:" + str(line.y2)
print info
Channel('repeat-me').send({'info': info, 'status': True})
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
consumers.py
import json
# In consumers.py
from channels import Group
# Connected to websocket.connect
def ws_add(message):
Group("chat").add(message.reply_channel)
# Connected to websocket.receive
def ws_message(message):
print "Receive Message now"
Group("chat").send({
"text": json.dumps({'status': False})
})
# Connected to websocket.disconnect
def ws_disconnect(message):
Group("chat").discard(message.reply_channel)
def repeat_me(message):
Group("chat").send({
"text": json.dumps({'status': message.content['status'], 'info':
message.content['info']})
})
Similarly, I have added added the following code to : routing.py
from channels.routing import route
from .consumers import ws_add, ws_message, ws_disconnect, repeat_me
channel_routing = [
route("websocket.connect", ws_add),
route("websocket.receive", ws_message),
route("websocket.disconnect", ws_disconnect),
route("repeat-me", repeat_me),
]
The following lines have been added to settings.py:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "TrainingInduct.routing.channel_routing",
},
}
Currently, I don't know how to deal with the group "chat". I don't even need a group. What are the remaining things to be done in order to get the line coordinates to be displayed at 127.0.0.1:8000/dashboard/ as soon as a new line is posted?
Note: The Line coordinates are getting properly POSTED to /api/line/ I think I might have to write a server code in order to get the data from the channel and push it back, am I right? Thanks.