Add search

This commit is contained in:
Andros Fenollosa 2021-11-20 20:29:44 +01:00
parent 7a53b7ddf5
commit cd79957fe6
4 changed files with 65 additions and 7 deletions

1
TODO
View File

@ -1,3 +1,2 @@
- Search
- List profiles - List profiles
- Chat - Chat

View File

@ -1,7 +1,7 @@
import json import json
from channels.generic.websocket import AsyncWebsocketConsumer from channels.generic.websocket import AsyncWebsocketConsumer
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
from .views import page_talks, page_about, page_single_talk from .views import page_talks, page_about, page_single_talk, page_results
class WebsiteConsumer(AsyncWebsocketConsumer): class WebsiteConsumer(AsyncWebsocketConsumer):
@ -61,6 +61,15 @@ class WebsiteConsumer(AsyncWebsocketConsumer):
self.room_group_name, {"type": "send_page_about"} self.room_group_name, {"type": "send_page_about"}
) )
# Search
if data["value"] == "search-talks":
await self.channel_layer.group_send(
self.room_group_name, {
"type": "send_page_search",
"search": data["search"],
}
)
# Pages # Pages
def _get_talks(self, page): def _get_talks(self, page):
@ -86,3 +95,15 @@ class WebsiteConsumer(AsyncWebsocketConsumer):
"""Send About page""" """Send About page"""
html = await sync_to_async(self._get_about)() html = await sync_to_async(self._get_about)()
await self.send(text_data=html) await self.send(text_data=html)
def _get_results(self, search):
return page_results(search)
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"])
await self.send(text_data=html)

View File

@ -1,5 +1,25 @@
{% load slippers %} {% load slippers %}
<main id="main" data-scroll-to-top="true"> <main id="main" data-scroll-to-top="true">
{# Search #}
<form>
<input type="hidden" name="action" value="page">
<input type="hidden" name="value" value="search-talks">
<label>
<input
autofocus="autofocus"
onfocus="const miVal = this.value; this.value= ''; this.value = miVal"
type="search"
name="search"
hx-ws="send"
hx-trigger="keyup changed delay:1000ms"
value="{{ search }}"
>
</label>
</form>
{# End search #}
{# List talks #}
{% for talk in talks %} {% for talk in talks %}
<form> <form>
<input type="hidden" name="action" value="page"> <input type="hidden" name="action" value="page">
@ -23,9 +43,15 @@
</a> </a>
</form> </form>
{% endfor %} {% endfor %}
{# Pagination #} {# End List talks #}
<div class="loading" >
<p>Page {{ page }}</p> {# Paginator #}
{% #link text="Next page" action="page" value="talks" page=next_page %}{% /link %} {% if not search %}
</div> <div class="loading" >
<p>Page {{ page }}</p>
{% #link text="Next page" action="page" value="talks" page=next_page %}{% /link %}
</div>
{% endif %}
{# End Paginator #}
</main> </main>

View File

@ -41,3 +41,15 @@ def page_single_talk(id):
def page_about(): def page_about():
return render_to_string("pages/about.html", {}) return render_to_string("pages/about.html", {})
def page_results(search):
return render_to_string(
"pages/talks.html",
{
"talks": Talk.objects.filter(
title__icontains=search.lower()
).order_by("title"),
"search": search,
},
)