diff --git a/apps/back/consumers.py b/apps/back/consumers.py index 724a729..b51afdd 100644 --- a/apps/back/consumers.py +++ b/apps/back/consumers.py @@ -1,12 +1,12 @@ import json from channels.generic.websocket import WebsocketConsumer from django.template.loader import render_to_string +from apps.back.models import Post class BlogConsumer(WebsocketConsumer): def connect(self): ''' Cliente se conecta ''' - print('cooooooooooooonectando') # Recoge el nombre de la sala self.room_name = self.scope["url_route"]["kwargs"]["room_name"] self.room_group_name = "blog_%s" % self.room_name @@ -17,17 +17,6 @@ class BlogConsumer(WebsocketConsumer): # Informa al cliente del éxito 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): ''' Cliente se desconecta ''' # Leave room group @@ -36,31 +25,23 @@ class BlogConsumer(WebsocketConsumer): def receive(self, text_data): ''' Cliente envía información y nosotros la recibimos ''' text_data_json = json.loads(text_data) - name = text_data_json["name"] - text = text_data_json["text"] + selector = text_data_json["selector"] + template = text_data_json["template"] + data = text_data_json["data"] - # Enviamos el mensaje a la sala - self.channel_layer.group_send( - self.room_group_name, - { - "type": "chat_message", - "name": name, - "text": text - } - ) + # Database + if template == "partials/blog/all_articles.html": + data["posts"] = Post.objects.all()[:5] - def chat_message(self, event): - ''' Recibimos información de la sala ''' - name = event["name"] - text = event["text"] + if template == "partials/blog/single.html": + data["post"] = Post.objects.get(data['id']) # Send message to WebSocket self.send( text_data=json.dumps( { - "type": "chat_message", - "name": name, - "text": text + "selector": selector, + "html": render_to_string(template, data) } ) - ) + ) \ No newline at end of file diff --git a/apps/back/models.py b/apps/back/models.py index 7d5a550..7041726 100644 --- a/apps/back/models.py +++ b/apps/back/models.py @@ -13,9 +13,6 @@ class Post(models.Model): status = models.IntegerField(choices=STATUS, default=0) created_on = models.DateTimeField(auto_now_add=True) - class Meta: - ordering = ['-created_on'] - def __str__(self): return self.title @@ -25,8 +22,5 @@ class Comment(models.Model): post = models.ForeignKey(Post, on_delete= models.CASCADE) created_on = models.DateTimeField(auto_now_add=True) - class Meta: - ordering = ['-created_on'] - def __str__(self): return self.name diff --git a/apps/front/templates/layouts/main.html b/apps/front/templates/layouts/main.html index b33c4ca..edfd0f6 100644 --- a/apps/front/templates/layouts/main.html +++ b/apps/front/templates/layouts/main.html @@ -14,42 +14,85 @@ - -
+
+ +
+
+ {% for post in posts %} +
+

{{ post.title }}

+

Author: {{ post.author }}

+

+
+
+ {% endfor %} +
+ +
+
diff --git a/apps/front/templates/partials/blog/all_articles.html b/apps/front/templates/partials/blog/all_articles.html index e69de29..e58df16 100644 --- a/apps/front/templates/partials/blog/all_articles.html +++ b/apps/front/templates/partials/blog/all_articles.html @@ -0,0 +1,34 @@ +
+ {% for post in posts %} +
+

{{ post.title }}

+

Author: {{ post.author }}

+

+
+
+ {% endfor %} +
+ + + \ No newline at end of file diff --git a/apps/front/templates/partials/blog/single.html b/apps/front/templates/partials/blog/single.html index e69de29..95ef4d9 100644 --- a/apps/front/templates/partials/blog/single.html +++ b/apps/front/templates/partials/blog/single.html @@ -0,0 +1,5 @@ +
+

{{ post.title }}

+

{{ post.author }}

+
{{ post.content }}
+
diff --git a/apps/front/templates/partials/website/about.html b/apps/front/templates/partials/website/about.html index e69de29..dd63573 100644 --- a/apps/front/templates/partials/website/about.html +++ b/apps/front/templates/partials/website/about.html @@ -0,0 +1,5 @@ +

About page

+

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?

+

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.

+

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!

+

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.

\ No newline at end of file diff --git a/apps/front/views.py b/apps/front/views.py index 36a63ab..d002238 100644 --- a/apps/front/views.py +++ b/apps/front/views.py @@ -1,7 +1,9 @@ from django.shortcuts import render +from apps.back.models import Post import uuid def all_articles(request): return render(request, 'layouts/main.html', { - "CHANNEL": uuid.uuid4().hex[:20].upper() + "CHANNEL": uuid.uuid4().hex[:20].upper(), + "posts": Post.objects.all()[:5] })