Working contacts page

master
Drew Bednar 1 year ago
parent da23ea767e
commit 4bab4b8499

@ -1,7 +1,15 @@
from flask import Blueprint
from flask import redirect
from flask import render_template
from flask import request
from flask_login import current_user
from flask_login import login_required
from sqlalchemy import and_
from sqlalchemy import or_
from sqlalchemy import select
from htmx_contact import Session
from htmx_contact.models import Contact
bp = Blueprint("main", __name__, url_prefix="/")
@ -14,4 +22,26 @@ def index():
@bp.route("/contacts", methods=["GET"])
@login_required
def contacts():
return render_template("contacts.html", message="Hello HTMX")
search = request.args.get("q")
if search is not None:
stmt = (
select(Contact)
.where(
and_(
Contact.user_id == current_user.id,
or_(
Contact.first_name.like(f"%{search}%"),
Contact.last_name.like(f"%{search}%"),
Contact.email.like(f"%{search}%"),
),
)
)
.order_by(Contact.last_name, Contact.first_name)
)
else:
stmt = select(Contact).where(Contact.user_id == current_user.id).order_by(Contact.last_name, Contact.first_name)
with Session() as session:
results = session.scalars(stmt).all()
return render_template("contacts.html", contacts=results)

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock %}</title>
<title>{% block title %}Contact.app{% endblock %}</title>
<link rel="apple-touch-icon" sizes="180x180" href="{{ url_for('static',filename='favicon/apple-touch-icon.png')}}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ url_for('static',filename='favicon/favicon-32x32.png')}}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ url_for('static',filename='favicon/favicon-16x16.png')}}">

@ -1,5 +1,28 @@
{% extends 'base.html' %}
{% block content %}
<h1>{{ message }}</h1>
<form action="/contacts" method="get">
<label for="search">Search Term</label>
<input id="search" type="text" name="q", value="{{ request.args.get('q') or '' }}">
<input type="submit", value="Search">
</form>
<table>
<thead>
<th>First</th>
<th>Last</th>
<th>Phone</th>
<th>Email</th>
</thead>
<tbody>
{% for contact in contacts %}
<tr>
<td>{{ contact.first_name }}</td>
<td>{{ contact.last_name }}</td>
<td>{{ contact.phone }}</td>
<td>{{ contact.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

Loading…
Cancel
Save