Add profile

This commit is contained in:
Andros Fenollosa 2021-11-20 23:55:39 +01:00
parent cd79957fe6
commit 912cd4eea4
7 changed files with 110 additions and 34 deletions

2
TODO
View File

@ -1,2 +1,2 @@
- List profiles
- List profiles with talks
- Chat

View File

@ -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)

View File

@ -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'),
),
]

View File

@ -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/")

View File

@ -0,0 +1,28 @@
{% load static %}
<main id="main" data-scroll-to-top="true">
{# List Profiles #}
<div style="display: flex; flex-wrap: wrap; justify-content: space-between; ">
{% for profile in profiles %}
<article style="max-width: 12rem">
<header>
<h2>
{{ profile.full_name }}
</h2>
<p>
<img src="{{ profile.avatar.url }}" alt="{{ profile.full_name }}">
</p>
</header>
<h3>Talks</h3>
<ul>
{% for talk in profile.talk_profile_set.all %}
<li>
{{ talk.title }}
</li>
{% endfor %}
</ul>
<footer>Email: {{ profile.email }}</footer>
</article>
{% endfor %}
</div>
{# End List talks #}
</main>

View File

@ -1,3 +1,4 @@
{% load static %}
{% load slippers %}
<main id="main" data-scroll-to-top="true">
@ -7,12 +8,11 @@
<input type="hidden" name="value" value="search-talks">
<label>
<input
autofocus="autofocus"
onfocus="const miVal = this.value; this.value= ''; this.value = miVal"
id="search"
type="search"
name="search"
hx-ws="send"
hx-trigger="keyup changed delay:1000ms"
hx-trigger="keyup changed delay:1s"
value="{{ search }}"
>
</label>
@ -20,6 +20,7 @@
{# End search #}
{# List talks #}
{% if talks %}
{% for talk in talks %}
<form>
<input type="hidden" name="action" value="page">
@ -43,6 +44,9 @@
</a>
</form>
{% endfor %}
{% else %}
<h1>No talks found.</h1>
{% endif %}
{# End List talks #}
{# Paginator #}
@ -54,4 +58,5 @@
{% endif %}
{# End Paginator #}
<script src="{% static "js/talks.js" %}"></script>
</main>

View File

@ -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", {})