2021-03-07 18:15:29 +01:00
|
|
|
import json
|
2021-03-10 23:36:26 +01:00
|
|
|
from channels.generic.websocket import WebsocketConsumer
|
|
|
|
from django.template.loader import render_to_string
|
2021-03-13 10:39:24 +01:00
|
|
|
from apps.back.models import Post, Comment
|
2021-03-21 14:24:02 +01:00
|
|
|
from asgiref.sync import async_to_sync
|
2021-03-07 18:15:29 +01:00
|
|
|
|
2021-03-10 23:36:26 +01:00
|
|
|
class BlogConsumer(WebsocketConsumer):
|
2021-03-07 18:15:29 +01:00
|
|
|
|
|
|
|
def connect(self):
|
2021-03-21 14:24:02 +01:00
|
|
|
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
|
|
|
|
self.room_group_name = "blog"
|
|
|
|
async_to_sync(self.channel_layer.group_add)(self.room_group_name, self.channel_name)
|
2021-03-08 21:25:34 +01:00
|
|
|
self.accept()
|
2021-03-07 18:15:29 +01:00
|
|
|
|
|
|
|
def disconnect(self, close_code):
|
2021-03-21 14:24:02 +01:00
|
|
|
async_to_sync(self.channel_layer.group_discard)(self.room_group_name, self.channel_name)
|
2021-03-12 21:21:01 +01:00
|
|
|
pass
|
2021-03-07 18:15:29 +01:00
|
|
|
|
|
|
|
def receive(self, text_data):
|
2021-03-21 14:24:02 +01:00
|
|
|
send = False
|
2021-03-07 18:15:29 +01:00
|
|
|
text_data_json = json.loads(text_data)
|
2021-03-12 00:09:19 +01:00
|
|
|
selector = text_data_json["selector"]
|
|
|
|
template = text_data_json["template"]
|
|
|
|
data = text_data_json["data"]
|
2021-03-07 18:15:29 +01:00
|
|
|
|
2021-03-12 00:09:19 +01:00
|
|
|
# Database
|
|
|
|
if template == "partials/blog/all_articles.html":
|
2021-03-12 21:16:23 +01:00
|
|
|
pag = data['pag'] if 'pag' in data else 1
|
|
|
|
amount = 3
|
|
|
|
start = pag - 1
|
|
|
|
end = start + amount
|
|
|
|
data["posts"] = Post.objects.all()[start:end]
|
|
|
|
data['pag'] = pag
|
2021-03-07 18:15:29 +01:00
|
|
|
|
2021-03-12 00:09:19 +01:00
|
|
|
if template == "partials/blog/single.html":
|
2021-03-21 12:53:47 +01:00
|
|
|
# Set data
|
2021-03-12 21:21:01 +01:00
|
|
|
data["post"] = Post.objects.get(pk=data['id'])
|
2021-03-21 12:53:47 +01:00
|
|
|
data["comments"] = Comment.objects.filter(post__id=data['id']).order_by("-created_on").all()
|
|
|
|
# Add new comment
|
|
|
|
data["newName"] = data['newName'] if 'newName' in data else ''
|
|
|
|
data["newMessage"] = data['newMessage'] if 'newMessage' in data else ''
|
|
|
|
newComment = 'newComment' in data
|
|
|
|
# Validation
|
|
|
|
error = (len(data["newName"]) == 0 or len(data["newMessage"]) == 0) and newComment
|
|
|
|
data["error"] = error
|
|
|
|
if newComment and not error:
|
|
|
|
# Insert
|
|
|
|
Comment(
|
|
|
|
name=data["newName"],
|
|
|
|
body=data["newMessage"],
|
|
|
|
post=Post.objects.get(pk=data['id'])
|
|
|
|
).save()
|
|
|
|
# Clean form
|
|
|
|
data["newName"] = ''
|
|
|
|
data["newMessage"] = ''
|
2021-03-21 14:24:02 +01:00
|
|
|
# Send Broadcast
|
|
|
|
async_to_sync(self.channel_layer.group_send)(
|
|
|
|
self.room_group_name,
|
|
|
|
{
|
|
|
|
"type": "blog.new.comment",
|
|
|
|
"text": {
|
|
|
|
"selector": selector,
|
|
|
|
"template": template,
|
|
|
|
"html": render_to_string(template, data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
send = True
|
2021-03-07 18:15:29 +01:00
|
|
|
|
|
|
|
# Send message to WebSocket
|
2021-03-21 14:24:02 +01:00
|
|
|
if not send:
|
|
|
|
self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
|
|
|
"selector": selector,
|
|
|
|
"template": template,
|
|
|
|
"html": render_to_string(template, data)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def blog_new_comment(self, event):
|
2021-03-08 21:25:34 +01:00
|
|
|
self.send(
|
2021-03-07 18:15:29 +01:00
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
2021-03-21 14:24:02 +01:00
|
|
|
"selector": event["text"]["selector"],
|
|
|
|
"template": event["text"]["template"],
|
|
|
|
"html": event["text"]["html"]
|
2021-03-07 18:15:29 +01:00
|
|
|
}
|
|
|
|
)
|
2021-03-21 12:53:47 +01:00
|
|
|
)
|