From 55dcd63654cfd1807b9efc46b4fc857b48f5079f Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Wed, 14 Jul 2021 18:57:25 +0200 Subject: [PATCH] support search on contact page --- .../dashboard/alias_contact_manager.html | 46 +++++++++++++++---- app/dashboard/views/alias_contact_manager.py | 31 +++++++++++-- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/app/dashboard/templates/dashboard/alias_contact_manager.html b/app/dashboard/templates/dashboard/alias_contact_manager.html index af638620..55e8c043 100644 --- a/app/dashboard/templates/dashboard/alias_contact_manager.html +++ b/app/dashboard/templates/dashboard/alias_contact_manager.html @@ -51,19 +51,47 @@

- -
- - {{ new_contact_form.csrf_token }} +
+
- + + + {{ new_contact_form.csrf_token }} + {{ new_contact_form.email(class="form-control", placeholder="First Last ", autofocus=True) }} + {{ render_field_errors(new_contact_form.email) }} +
+ Where do you want to send the email? +
+ + +
- {{ new_contact_form.email(class="form-control", placeholder="First Last ", autofocus=True) }} - {{ render_field_errors(new_contact_form.email) }} - - +
+
+
+ + + +
+ + {% if query %} + {% if highlight_contact_id %} + Reset + {% else %} + Reset + {% endif %} + {% endif %} +
+
+
{% for contact_info in contact_infos %} diff --git a/app/dashboard/views/alias_contact_manager.py b/app/dashboard/views/alias_contact_manager.py index 13a0effa..e34a0110 100644 --- a/app/dashboard/views/alias_contact_manager.py +++ b/app/dashboard/views/alias_contact_manager.py @@ -60,7 +60,9 @@ class ContactInfo(object): latest_email_log: EmailLog -def get_contact_infos(alias: Alias, page=0, contact_id=None) -> [ContactInfo]: +def get_contact_infos( + alias: Alias, page=0, contact_id=None, query: str = "" +) -> [ContactInfo]: """if contact_id is set, only return the contact info for this contact""" sub = ( db.session.query( @@ -115,6 +117,14 @@ def get_contact_infos(alias: Alias, page=0, contact_id=None) -> [ContactInfo]: ) ) + if query: + q = q.filter( + or_( + Contact.website_email.ilike(f"%{query}%"), + Contact.name.ilike(f"%{query}%"), + ) + ) + if contact_id: q = q.filter(Contact.id == contact_id) @@ -153,6 +163,8 @@ def alias_contact_manager(alias_id): if request.args.get("page"): page = int(request.args.get("page")) + query = request.args.get("query") or "" + # sanity check if not alias: flash("You do not have access to this page", "warning") @@ -248,7 +260,18 @@ def alias_contact_manager(alias_id): url_for("dashboard.alias_contact_manager", alias_id=alias_id) ) - contact_infos = get_contact_infos(alias, page) + elif request.form.get("form-name") == "search": + query = request.form.get("query") + return redirect( + url_for( + "dashboard.alias_contact_manager", + alias_id=alias_id, + query=query, + highlight_contact_id=highlight_contact_id, + ) + ) + + contact_infos = get_contact_infos(alias, page, query=query) last_page = len(contact_infos) < PAGE_LIMIT # if highlighted contact isn't included, fetch it @@ -256,7 +279,8 @@ def alias_contact_manager(alias_id): contact_ids = [contact_info.contact.id for contact_info in contact_infos] if highlight_contact_id and highlight_contact_id not in contact_ids: contact_infos = ( - get_contact_infos(alias, contact_id=highlight_contact_id) + contact_infos + get_contact_infos(alias, contact_id=highlight_contact_id, query=query) + + contact_infos ) return render_template( @@ -267,4 +291,5 @@ def alias_contact_manager(alias_id): highlight_contact_id=highlight_contact_id, page=page, last_page=last_page, + query=query, )