html-over-websockets-with-d.../app/website/consumers.py

141 lines
4.4 KiB
Python
Raw Normal View History

2021-11-12 18:59:01 +01:00
import json
from channels.generic.websocket import AsyncWebsocketConsumer
from asgiref.sync import sync_to_async
2021-11-20 23:55:39 +01:00
from .views import page_talks, page_about, page_single_talk, page_results, page_profiles
2021-11-12 18:59:01 +01:00
2021-11-13 09:07:55 +01:00
class WebsiteConsumer(AsyncWebsocketConsumer):
2021-11-12 18:59:01 +01:00
async def connect(self):
2021-11-13 09:07:55 +01:00
"""Connect user"""
2021-11-12 18:59:01 +01:00
self.room_name = self.scope["url_route"]["kwargs"]["room_id"]
self.room_group_name = "root_%s" % self.room_name
# Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
# Send success connection
await self.accept()
# Send Home page
await self.channel_layer.group_send(
self.room_group_name,
{
2021-11-12 22:16:42 +01:00
"type": "send_page_talks",
"page": 1,
2021-11-13 09:07:55 +01:00
},
2021-11-12 18:59:01 +01:00
)
async def disconnect(self, close_code):
2021-11-13 09:07:55 +01:00
"""Cliente se desconecta"""
2021-11-12 18:59:01 +01:00
# Leave room group
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
async def receive(self, text_data):
2021-11-12 21:25:19 +01:00
# Load data
data = json.loads(text_data)
# Change page
if data["action"] == "page":
# Talks
if data["value"] == "talks":
2021-11-12 22:16:42 +01:00
page = 1
if "page" in data:
page = int(data["page"])
2021-11-12 21:25:19 +01:00
await self.channel_layer.group_send(
self.room_group_name,
{
2021-11-12 22:16:42 +01:00
"type": "send_page_talks",
"page": page,
2021-11-13 09:07:55 +01:00
},
)
# Single talk
if data["value"] == "single-talk":
await self.channel_layer.group_send(
2021-11-21 09:34:54 +01:00
self.room_group_name,
{
2021-11-13 09:07:55 +01:00
"type": "send_page_single_talk",
2021-11-21 09:34:54 +01:00
"id": data["id"],
},
2021-11-12 21:25:19 +01:00
)
2021-11-20 23:55:39 +01:00
# Profiles
if data["value"] == "profiles":
await self.channel_layer.group_send(
self.room_group_name, {"type": "send_page_profiles"}
)
2021-11-21 12:47:12 +01:00
# Chat
if data["value"] == "chat":
await self.channel_layer.group_send(
self.room_group_name, {
"type": "send_page_chat",
}
)
2021-11-12 21:25:19 +01:00
# About
if data["value"] == "about":
await self.channel_layer.group_send(
2021-11-13 09:07:55 +01:00
self.room_group_name, {"type": "send_page_about"}
2021-11-12 21:25:19 +01:00
)
2021-11-20 20:29:44 +01:00
# Search
if data["value"] == "search-talks":
await self.channel_layer.group_send(
2021-11-21 09:34:54 +01:00
self.room_group_name,
{
2021-11-20 20:29:44 +01:00
"type": "send_page_search",
"search": data["search"],
2021-11-21 09:34:54 +01:00
},
2021-11-20 20:29:44 +01:00
)
2021-11-12 21:25:19 +01:00
# Pages
2021-11-12 18:59:01 +01:00
2021-11-20 23:55:39 +01:00
def _get_talks(self, page=1):
2021-11-12 22:16:42 +01:00
return page_talks(page=page)
2021-11-12 18:59:01 +01:00
2021-11-12 21:25:19 +01:00
async def send_page_talks(self, event):
2021-11-13 09:07:55 +01:00
"""Send Home page"""
2021-11-12 22:16:42 +01:00
html = await sync_to_async(self._get_talks)(event["page"])
2021-11-12 21:25:19 +01:00
await self.send(text_data=html)
2021-11-13 09:07:55 +01:00
def _get_single_talk(self, id):
return page_single_talk(id)
async def send_page_single_talk(self, event):
"""Send Single talk page"""
html = await sync_to_async(self._get_single_talk)(event["id"])
await self.send(text_data=html)
2021-11-20 23:55:39 +01:00
def _get_profiles(self):
return page_profiles()
async def send_page_profiles(self, event):
"""Send Profiles page"""
html = await sync_to_async(self._get_profiles)()
await self.send(text_data=html)
2021-11-21 12:47:12 +01:00
def _get_chat(self):
return page_chat()
async def send_page_chat(self, event):
"""Send Chat page"""
html = await sync_to_async(self._get_chat)()
await self.send(text_data=html)
2021-11-12 21:25:19 +01:00
def _get_about(self):
return page_about()
async def send_page_about(self, event):
2021-11-13 09:07:55 +01:00
"""Send About page"""
2021-11-12 21:25:19 +01:00
html = await sync_to_async(self._get_about)()
2021-11-13 09:07:55 +01:00
await self.send(text_data=html)
2021-11-20 20:29:44 +01:00
def _get_results(self, search):
return page_results(search)
async def send_page_search(self, event):
"""Send results talks"""
if event["search"] != "":
html = await sync_to_async(self._get_results)(event["search"])
else:
2021-11-20 23:55:39 +01:00
html = await sync_to_async(self._get_talks)()
2021-11-20 20:29:44 +01:00
await self.send(text_data=html)