From b13f03855d8deb3fd931d7bc4a45eacf49e940e0 Mon Sep 17 00:00:00 2001 From: dsc Date: Sat, 12 Mar 2022 22:06:22 +0200 Subject: [PATCH] turn search into include, limit user results, minimum term search 2, show date of address addition --- yellow/factory.py | 4 +++- yellow/models.py | 14 ++++++++++--- yellow/routes.py | 8 +++++-- yellow/templates/base.html | 2 +- yellow/templates/includes/nav.html | 2 +- yellow/templates/includes/search.html | 3 +++ yellow/templates/includes/user_results.html | 11 ++++++++++ yellow/templates/index.html | 8 ++++++- yellow/templates/search.html | 23 +++------------------ yellow/templates/search_results.html | 23 ++++----------------- 10 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 yellow/templates/includes/search.html create mode 100644 yellow/templates/includes/user_results.html diff --git a/yellow/factory.py b/yellow/factory.py index 069a196..3cf7b88 100644 --- a/yellow/factory.py +++ b/yellow/factory.py @@ -1,5 +1,6 @@ import os import logging +from datetime import datetime import asyncio from quart import Quart, url_for, jsonify, render_template, session @@ -63,7 +64,8 @@ def create_app(): current_user = session.get('user') if current_user: current_user = User(**current_user) - return dict(user=current_user, url_login=openid.endpoint_name_login) + now = datetime.now() + return dict(user=current_user, url_login=openid.endpoint_name_login, year=now.year) @app.before_serving async def startup(): diff --git a/yellow/models.py b/yellow/models.py index 7232ada..4022d62 100644 --- a/yellow/models.py +++ b/yellow/models.py @@ -12,16 +12,24 @@ db = SqliteDatabase(settings.DB_PATH) class User(pw.Model): id = pw.UUIDField(primary_key=True) - created = pw.DateTimeField(default=datetime.now) + created: datetime = pw.DateTimeField(default=datetime.now) username = pw.CharField(unique=True, null=False) address = pw.CharField(null=True) + @property + def created_dt(self): + return self.created.strftime('%Y-%m-%d') + @staticmethod async def search(needle) -> List['User']: needle = needle.replace("*", "") - if len(needle) <= 2: + if len(needle) <= 1: raise Exception("need longer search term") - return User.select().where(User.username % f"*{needle}*") + + return User.select().where( + User.address.is_null(False), + User.username % f"*{needle}*" + ) def to_json(self, ignore_key=None): data = { diff --git a/yellow/routes.py b/yellow/routes.py index a9a1c87..7c1e6ec 100644 --- a/yellow/routes.py +++ b/yellow/routes.py @@ -53,7 +53,7 @@ async def dashboard_address_post(): async def search(): needle = request.args.get('username') if needle: - if len(needle) <= 2: + if len(needle) <= 1: raise Exception("Search term needs to be longer") users = [u for u in await User.search(needle)] @@ -62,7 +62,11 @@ async def search(): else: return await render_template('search_results.html') - users = [u for u in User.select()] + q = User.select() + q = q.where(User.address.is_null(False)) + q = q.limit(100) + + users = [u for u in q] return await render_template('search.html', users=users) diff --git a/yellow/templates/base.html b/yellow/templates/base.html index 797b8ea..3b4b52c 100644 --- a/yellow/templates/base.html +++ b/yellow/templates/base.html @@ -77,7 +77,7 @@ diff --git a/yellow/templates/includes/nav.html b/yellow/templates/includes/nav.html index dd051d8..9b940c5 100644 --- a/yellow/templates/includes/nav.html +++ b/yellow/templates/includes/nav.html @@ -9,7 +9,7 @@ Login {% else %} Logout - Dashboard + My Profile {% endif %} YellWOWPage search About diff --git a/yellow/templates/includes/search.html b/yellow/templates/includes/search.html new file mode 100644 index 0000000..7f14863 --- /dev/null +++ b/yellow/templates/includes/search.html @@ -0,0 +1,3 @@ +
+ +
diff --git a/yellow/templates/includes/user_results.html b/yellow/templates/includes/user_results.html new file mode 100644 index 0000000..7c6fca7 --- /dev/null +++ b/yellow/templates/includes/user_results.html @@ -0,0 +1,11 @@ +
+ {% for user in users %} +
+
+ {{user.username}} + Added: {{ user.created_dt }} +
+ {{user.address}} +
+ {% endfor %} +
diff --git a/yellow/templates/index.html b/yellow/templates/index.html index 502bd16..4036a6c 100644 --- a/yellow/templates/index.html +++ b/yellow/templates/index.html @@ -10,7 +10,13 @@
The first addresses library - - from the community to the community + from the community, for the community. +
+

+
+ + +
diff --git a/yellow/templates/search.html b/yellow/templates/search.html index f651820..8ee47e8 100644 --- a/yellow/templates/search.html +++ b/yellow/templates/search.html @@ -5,20 +5,9 @@
-
- -
+ {% include 'includes/search.html' %} -
- {% for user in users %} -
-
- {{user.username}} -
- {{user.address}} -
- {% endfor %} -
+ {% include 'includes/user_results.html' %}