Update post
This commit is contained in:
parent
ae05e3a6f1
commit
901226c700
@ -1,12 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
from channels.generic.websocket import WebsocketConsumer
|
from channels.generic.websocket import WebsocketConsumer
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
|
from apps.back.models import Post
|
||||||
|
|
||||||
class BlogConsumer(WebsocketConsumer):
|
class BlogConsumer(WebsocketConsumer):
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
''' Cliente se conecta '''
|
''' Cliente se conecta '''
|
||||||
print('cooooooooooooonectando')
|
|
||||||
# Recoge el nombre de la sala
|
# Recoge el nombre de la sala
|
||||||
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
|
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
|
||||||
self.room_group_name = "blog_%s" % self.room_name
|
self.room_group_name = "blog_%s" % self.room_name
|
||||||
@ -17,17 +17,6 @@ class BlogConsumer(WebsocketConsumer):
|
|||||||
# Informa al cliente del éxito
|
# Informa al cliente del éxito
|
||||||
self.accept()
|
self.accept()
|
||||||
|
|
||||||
# Send message to WebSocket
|
|
||||||
self.send(
|
|
||||||
text_data=json.dumps(
|
|
||||||
{
|
|
||||||
"selector": "#articles",
|
|
||||||
"position": "appendChild",
|
|
||||||
"html": render_to_string('blog/articles.html', {'pag': 1})
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def disconnect(self, close_code):
|
def disconnect(self, close_code):
|
||||||
''' Cliente se desconecta '''
|
''' Cliente se desconecta '''
|
||||||
# Leave room group
|
# Leave room group
|
||||||
@ -36,31 +25,23 @@ class BlogConsumer(WebsocketConsumer):
|
|||||||
def receive(self, text_data):
|
def receive(self, text_data):
|
||||||
''' Cliente envía información y nosotros la recibimos '''
|
''' Cliente envía información y nosotros la recibimos '''
|
||||||
text_data_json = json.loads(text_data)
|
text_data_json = json.loads(text_data)
|
||||||
name = text_data_json["name"]
|
selector = text_data_json["selector"]
|
||||||
text = text_data_json["text"]
|
template = text_data_json["template"]
|
||||||
|
data = text_data_json["data"]
|
||||||
|
|
||||||
# Enviamos el mensaje a la sala
|
# Database
|
||||||
self.channel_layer.group_send(
|
if template == "partials/blog/all_articles.html":
|
||||||
self.room_group_name,
|
data["posts"] = Post.objects.all()[:5]
|
||||||
{
|
|
||||||
"type": "chat_message",
|
|
||||||
"name": name,
|
|
||||||
"text": text
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
def chat_message(self, event):
|
if template == "partials/blog/single.html":
|
||||||
''' Recibimos información de la sala '''
|
data["post"] = Post.objects.get(data['id'])
|
||||||
name = event["name"]
|
|
||||||
text = event["text"]
|
|
||||||
|
|
||||||
# Send message to WebSocket
|
# Send message to WebSocket
|
||||||
self.send(
|
self.send(
|
||||||
text_data=json.dumps(
|
text_data=json.dumps(
|
||||||
{
|
{
|
||||||
"type": "chat_message",
|
"selector": selector,
|
||||||
"name": name,
|
"html": render_to_string(template, data)
|
||||||
"text": text
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
@ -13,9 +13,6 @@ class Post(models.Model):
|
|||||||
status = models.IntegerField(choices=STATUS, default=0)
|
status = models.IntegerField(choices=STATUS, default=0)
|
||||||
created_on = models.DateTimeField(auto_now_add=True)
|
created_on = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
class Meta:
|
|
||||||
ordering = ['-created_on']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
@ -25,8 +22,5 @@ class Comment(models.Model):
|
|||||||
post = models.ForeignKey(Post, on_delete= models.CASCADE)
|
post = models.ForeignKey(Post, on_delete= models.CASCADE)
|
||||||
created_on = models.DateTimeField(auto_now_add=True)
|
created_on = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
class Meta:
|
|
||||||
ordering = ['-created_on']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
@ -14,42 +14,85 @@
|
|||||||
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-icons.min.css">
|
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-icons.min.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="container">
|
||||||
<nav id="nav">
|
<nav id="nav">
|
||||||
<ul class="nav">
|
<ul class="tab tab-block">
|
||||||
<li class="nav-item">
|
<li class="tab-item">
|
||||||
<a href="#">All</a>
|
<a id="link-all" href="#">All</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="tab-item">
|
||||||
<a href="#">About</a>
|
<a id="link-about" href="#">About</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
<main id="main"></main>
|
<main id="main">
|
||||||
|
<div id="articles">
|
||||||
|
{% for post in posts %}
|
||||||
|
<article>
|
||||||
|
<h2>{{ post.title }}</h2>
|
||||||
|
<p>Author: {{ post.author }}</p>
|
||||||
|
<p><button class="btn">More</button></p>
|
||||||
|
<hr>
|
||||||
|
</article>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<ul class="pagination">
|
||||||
|
<li class="page-item disabled">
|
||||||
|
<a href="#" tabindex="-1">Previous</a>
|
||||||
|
</li>
|
||||||
|
<li class="page-item">
|
||||||
|
<a href="#">Next</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
<script>
|
<script>
|
||||||
const CHAT_SOCKET = new WebSocket('ws://my-demo.localhost/ws/blog/{{ CHANNEL}}/');
|
document.$CHAT_SOCKET = new WebSocket('ws://my-demo.localhost/ws/blog/{{ CHANNEL}}/');
|
||||||
|
|
||||||
// Conectado
|
// Conectado
|
||||||
CHAT_SOCKET.addEventListener('open', () => {
|
document.$CHAT_SOCKET.addEventListener('open', () => {
|
||||||
console.log('Conectado');
|
console.log('Connect');
|
||||||
});
|
});
|
||||||
// Desconectado
|
// Desconectado
|
||||||
CHAT_SOCKET.addEventListener('close', () => {
|
document.$CHAT_SOCKET.addEventListener('close', () => {
|
||||||
console.log('Desconectado');
|
console.log('Disconnect');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Recibir mensaje
|
// Recibir mensaje y dibujar
|
||||||
CHAT_SOCKET.addEventListener('message', (event) => {
|
document.$CHAT_SOCKET.addEventListener('message', (event) => {
|
||||||
console.log('Recibido nuevo mensaje');
|
console.log('New HTML');
|
||||||
const MI_NUEVA_DATA = JSON.parse(event.data);
|
const NEW_DATA = JSON.parse(event.data);
|
||||||
});
|
//document.querySelector(NEW_DATA.selector).innerHTML = NEW_DATA.html;
|
||||||
|
|
||||||
// Desconectado
|
|
||||||
CHAT_SOCKET.addEventListener('error', (event) => {
|
const rangeHTML = document.createRange().createContextualFragment(NEW_DATA.html);
|
||||||
console.log('error');
|
document.querySelector(NEW_DATA.selector).innerHTML = '';
|
||||||
console.log(event)
|
document.querySelector(NEW_DATA.selector).appendChild(rangeHTML);
|
||||||
|
|
||||||
|
|
||||||
|
console.log(rangeHTML)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
document.querySelector('#link-about').addEventListener('click', (event) => {
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
document.$CHAT_SOCKET.send(JSON.stringify({
|
||||||
|
selector: "#main",
|
||||||
|
template: "partials/website/about.html",
|
||||||
|
data: {}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelector('#link-all').addEventListener('click', (event) => {
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
document.$CHAT_SOCKET.send(JSON.stringify({
|
||||||
|
selector: "#main",
|
||||||
|
template: "partials/blog/all_articles.html",
|
||||||
|
data: {}
|
||||||
|
}));
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
<div id="articles">
|
||||||
|
{% for post in posts %}
|
||||||
|
<article>
|
||||||
|
<h2>{{ post.title }}</h2>
|
||||||
|
<p>Author: {{ post.author }}</p>
|
||||||
|
<p><button data-id="{{ post.id }}" class="btn">More</button></p>
|
||||||
|
<hr>
|
||||||
|
</article>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<ul class="pagination">
|
||||||
|
<li class="page-item disabled">
|
||||||
|
<a href="#" tabindex="-1">Previous</a>
|
||||||
|
</li>
|
||||||
|
<li class="page-item">
|
||||||
|
<a href="#">Next</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const buttons = document.querySelectorAll('.btn');
|
||||||
|
|
||||||
|
buttons.forEach((button) => {
|
||||||
|
button.addEventListener('click', event => {
|
||||||
|
document.$CHAT_SOCKET.send(JSON.stringify({
|
||||||
|
selector: "#main",
|
||||||
|
template: "partials/blog/single.html",
|
||||||
|
data: {
|
||||||
|
id: event.target.dataset.id
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
@ -0,0 +1,5 @@
|
|||||||
|
<article>
|
||||||
|
<h1>{{ post.title }}</h1>
|
||||||
|
<h2>{{ post.author }}</h2>
|
||||||
|
<div>{{ post.content }}</div>
|
||||||
|
</article>
|
@ -0,0 +1,5 @@
|
|||||||
|
<h2>About page</h2>
|
||||||
|
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nam molestias ex vitae officiis, autem repellendus, ea doloribus voluptates. Odit accusamus alias soluta animi cumque, aspernatur, beatae repudiandae non laudantium sunt?</p>
|
||||||
|
<p>Necessitatibus at hic ducimus quam numquam aspernatur fugit quia molestiae. Ipsum quibusdam quae placeat nemo? Eaque, vel repellendus ea tenetur non veniam dicta? Itaque ipsum tempora inventore, assumenda dignissimos ratione.</p>
|
||||||
|
<p>Dicta voluptatem repudiandae exercitationem possimus iure, cumque, minus, odit accusamus, fugiat nulla ratione nobis ad harum. Recusandae obcaecati eligendi, amet officiis iusto porro saepe quaerat, cum labore molestiae? Praesentium, omnis!</p>
|
||||||
|
<p>Voluptate nostrum qui porro est magni excepturi maxime, modi nobis nesciunt quaerat soluta error alias quis voluptatibus iusto hic minus ipsam. Repudiandae quisquam quaerat illo eos quo error, ipsa nemo.</p>
|
@ -1,7 +1,9 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from apps.back.models import Post
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
def all_articles(request):
|
def all_articles(request):
|
||||||
return render(request, 'layouts/main.html', {
|
return render(request, 'layouts/main.html', {
|
||||||
"CHANNEL": uuid.uuid4().hex[:20].upper()
|
"CHANNEL": uuid.uuid4().hex[:20].upper(),
|
||||||
|
"posts": Post.objects.all()[:5]
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user