From 912cd4eea409372a59fe0fd51549677ec48a74be Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 20 Nov 2021 23:55:39 +0100 Subject: [PATCH] Add profile --- TODO | 2 +- app/website/consumers.py | 21 +++++-- .../migrations/0002_alter_talk_author.py | 19 +++++++ app/website/models.py | 2 +- app/website/templates/pages/profiles.html | 28 +++++++++ app/website/templates/pages/talks.html | 57 ++++++++++--------- app/website/views.py | 15 ++++- 7 files changed, 110 insertions(+), 34 deletions(-) create mode 100644 app/website/migrations/0002_alter_talk_author.py create mode 100644 app/website/templates/pages/profiles.html diff --git a/TODO b/TODO index 12e17f8..920391b 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,2 @@ -- List profiles +- List profiles with talks - Chat \ No newline at end of file diff --git a/app/website/consumers.py b/app/website/consumers.py index 623dd43..1253048 100644 --- a/app/website/consumers.py +++ b/app/website/consumers.py @@ -1,7 +1,7 @@ import json from channels.generic.websocket import AsyncWebsocketConsumer from asgiref.sync import sync_to_async -from .views import page_talks, page_about, page_single_talk, page_results +from .views import page_talks, page_about, page_single_talk, page_results, page_profiles class WebsiteConsumer(AsyncWebsocketConsumer): @@ -55,6 +55,12 @@ class WebsiteConsumer(AsyncWebsocketConsumer): "id": data["id"], } ) + # Profiles + if data["value"] == "profiles": + await self.channel_layer.group_send( + self.room_group_name, {"type": "send_page_profiles"} + ) + # About if data["value"] == "about": await self.channel_layer.group_send( @@ -72,7 +78,7 @@ class WebsiteConsumer(AsyncWebsocketConsumer): # Pages - def _get_talks(self, page): + def _get_talks(self, page=1): return page_talks(page=page) async def send_page_talks(self, event): @@ -88,6 +94,14 @@ class WebsiteConsumer(AsyncWebsocketConsumer): html = await sync_to_async(self._get_single_talk)(event["id"]) await self.send(text_data=html) + 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) + def _get_about(self): return page_about() @@ -101,9 +115,8 @@ class WebsiteConsumer(AsyncWebsocketConsumer): async def send_page_search(self, event): """Send results talks""" - print(event) if event["search"] != "": html = await sync_to_async(self._get_results)(event["search"]) else: - html = await sync_to_async(self._get_talks)(event["page"]) + html = await sync_to_async(self._get_talks)() await self.send(text_data=html) diff --git a/app/website/migrations/0002_alter_talk_author.py b/app/website/migrations/0002_alter_talk_author.py new file mode 100644 index 0000000..579bcfe --- /dev/null +++ b/app/website/migrations/0002_alter_talk_author.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.9 on 2021-11-20 22:38 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('website', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='talk', + name='author', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='talk_profile', to='website.profile', verbose_name='Autor'), + ), + ] diff --git a/app/website/models.py b/app/website/models.py index d1eb733..0f21cfd 100644 --- a/app/website/models.py +++ b/app/website/models.py @@ -48,7 +48,7 @@ class Talk(models.Model): Profile, on_delete=models.SET_NULL, null=True, - related_name="author", + related_name="talk_profile", verbose_name="Autor", ) image = models.ImageField(verbose_name="Imagen", upload_to="uploads/talks/") diff --git a/app/website/templates/pages/profiles.html b/app/website/templates/pages/profiles.html new file mode 100644 index 0000000..f787c4e --- /dev/null +++ b/app/website/templates/pages/profiles.html @@ -0,0 +1,28 @@ +{% load static %} +
+ {# List Profiles #} +
+ {% for profile in profiles %} +
+
+

+ {{ profile.full_name }} +

+

+ {{ profile.full_name }} +

+
+

Talks

+
    + {% for talk in profile.talk_profile_set.all %} +
  • + {{ talk.title }} +
  • + {% endfor %} +
+
Email: {{ profile.email }}
+
+ {% endfor %} +
+ {# End List talks #} +
diff --git a/app/website/templates/pages/talks.html b/app/website/templates/pages/talks.html index cd8a25a..9e94d56 100644 --- a/app/website/templates/pages/talks.html +++ b/app/website/templates/pages/talks.html @@ -1,3 +1,4 @@ +{% load static %} {% load slippers %}
@@ -7,12 +8,11 @@ @@ -20,29 +20,33 @@ {# End search #} {# List talks #} - {% for talk in talks %} -
- - - - -
-
-
-

- -

-

{{ talk.title }}

-
-
-

- {{ talk.content|truncatechars:150 }} -

-
Author {{ talk.author.full_name }} - {{ talk.category.name }}
-
-
-
- {% endfor %} + {% if talks %} + {% for talk in talks %} +
+ + + + +
+
+
+

+ +

+

{{ talk.title }}

+
+
+

+ {{ talk.content|truncatechars:150 }} +

+
Author {{ talk.author.full_name }} - {{ talk.category.name }}
+
+
+
+ {% endfor %} + {% else %} +

No talks found.

+ {% endif %} {# End List talks #} {# Paginator #} @@ -54,4 +58,5 @@ {% endif %} {# End Paginator #} +
diff --git a/app/website/views.py b/app/website/views.py index fdb999d..2b33322 100644 --- a/app/website/views.py +++ b/app/website/views.py @@ -2,7 +2,7 @@ from django.shortcuts import render from django.template.loader import render_to_string from random import randint from django.conf import settings -from .models import Talk +from .models import Talk, Profile from asgiref.sync import sync_to_async @@ -20,10 +20,12 @@ def index(request): def page_talks(page=1): TALK_PER_PAGE = 5 + START = TALK_PER_PAGE * (page - 1) + END = TALK_PER_PAGE * page return render_to_string( "pages/talks.html", { - "talks": Talk.objects.order_by("title")[: TALK_PER_PAGE * page], + "talks": Talk.objects.order_by("title")[START:END], "page": page, "next_page": page + 1, }, @@ -38,6 +40,15 @@ def page_single_talk(id): }, ) +def page_profiles(): + return render_to_string( + "pages/profiles.html", + { + "profiles": Profile.objects.order_by("full_name"), + }, + ) + + def page_about(): return render_to_string("pages/about.html", {})