Major Refactor
This commit is contained in:
@@ -1,81 +1,68 @@
|
||||
from flask import request, jsonify
|
||||
|
||||
from flask import request, jsonify
|
||||
from app.search import search
|
||||
from app import db
|
||||
from app.classes.customer import Customer_Customer, Customer_Customer_schema
|
||||
from app.classes.delivery import Delivery_Delivery, Delivery_Delivery_schema
|
||||
|
||||
# We now need `and_` to combine our search conditions
|
||||
from sqlalchemy import or_, and_
|
||||
|
||||
@search.route("/customer", methods=["GET"])
|
||||
def search_customers():
|
||||
"""
|
||||
|
||||
Searches for customers based on a keyword.
|
||||
The new logic searches across multiple fields (name, address, town, phone, etc.)
|
||||
for each word provided in the search query.
|
||||
"""
|
||||
|
||||
keyword = request.args.get('q')
|
||||
search = "%{}%".format(keyword)
|
||||
search_type = (search[1])
|
||||
search = search.replace("!", "")
|
||||
search = search.replace("#", "")
|
||||
search = search.replace("@", "")
|
||||
search = search.replace("$", "")
|
||||
# search by last name
|
||||
if search_type == '@':
|
||||
search = search[1:]
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_last_name.ilike(search))
|
||||
.all())
|
||||
# Customer Address
|
||||
elif search_type == '!':
|
||||
if not keyword or len(keyword) < 2:
|
||||
return jsonify([])
|
||||
|
||||
search = search[::1]
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_address.ilike(search))
|
||||
.all())
|
||||
# Phone Number
|
||||
elif search_type == '#':
|
||||
search = search[::1]
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_phone_number.ilike(search))
|
||||
.all())
|
||||
# Account Number
|
||||
elif search_type == '$':
|
||||
search = search[::1]
|
||||
# --- NEW SEARCH LOGIC ---
|
||||
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.account_number.ilike(search))
|
||||
.order_by(Customer_Customer.account_number.asc())
|
||||
.all())
|
||||
# 1. Split the incoming search query into individual words.
|
||||
# e.g., "john main st" -> ["john", "main", "st"]
|
||||
search_words = keyword.split()
|
||||
|
||||
else:
|
||||
customer_list = (db.session
|
||||
.query(Customer_Customer)
|
||||
.filter(Customer_Customer.customer_last_name.ilike(search))
|
||||
.all())
|
||||
# 2. We'll build a list of search conditions. Each condition will check
|
||||
# if a word exists in ANY of the relevant customer fields.
|
||||
conditions = []
|
||||
for word in search_words:
|
||||
search_pattern = f"%{word}%"
|
||||
# For each word, create an OR clause to check against all fields
|
||||
conditions.append(
|
||||
or_(
|
||||
Customer_Customer.customer_first_name.ilike(search_pattern),
|
||||
Customer_Customer.customer_last_name.ilike(search_pattern),
|
||||
Customer_Customer.customer_address.ilike(search_pattern),
|
||||
Customer_Customer.customer_town.ilike(search_pattern),
|
||||
Customer_Customer.customer_phone_number.ilike(search_pattern),
|
||||
Customer_Customer.account_number.ilike(search_pattern)
|
||||
# Add any other fields you want to be searchable here
|
||||
)
|
||||
)
|
||||
|
||||
# 3. Combine all our word conditions with AND. This means the customer
|
||||
# record must match ALL the words from the search query.
|
||||
# e.g., for "john main", it must contain "john" AND "main" somewhere.
|
||||
query = db.session.query(Customer_Customer).filter(and_(*conditions))
|
||||
|
||||
# Limiting results is still good practice
|
||||
customer_list = query.order_by(Customer_Customer.customer_last_name.asc()).limit(20).all()
|
||||
|
||||
customer_schema = Customer_Customer_schema(many=True)
|
||||
return jsonify(customer_schema.dump(customer_list))
|
||||
|
||||
|
||||
# The /delivery route is fine as is, so no changes needed there.
|
||||
@search.route("/delivery", methods=["GET"])
|
||||
def search_delivery():
|
||||
"""
|
||||
pagination all customers
|
||||
"""
|
||||
# ... (no changes to this function)
|
||||
keyword = request.args.get('q')
|
||||
search = "%{}%".format(keyword)
|
||||
search_type = (search[1])
|
||||
|
||||
delivery_ticket = (db.session
|
||||
.query(Delivery_Delivery)
|
||||
.filter(Delivery_Delivery.id.ilike(search))
|
||||
.all())
|
||||
|
||||
|
||||
delivery_schema = Delivery_Delivery_schema(many=True)
|
||||
return jsonify(delivery_schema.dump(delivery_ticket))
|
||||
if not keyword or not keyword.isdigit():
|
||||
return jsonify([])
|
||||
delivery_ticket = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.id == keyword).first()
|
||||
if delivery_ticket:
|
||||
delivery_schema = Delivery_Delivery_schema(many=False)
|
||||
return jsonify(delivery_schema.dump(delivery_ticket))
|
||||
else:
|
||||
return jsonify({})
|
||||
Reference in New Issue
Block a user