reverted search
This commit is contained in:
@@ -1,111 +1,81 @@
|
|||||||
|
|
||||||
from flask import request, jsonify
|
from flask import request, jsonify
|
||||||
|
|
||||||
from app.search import search
|
from app.search import search
|
||||||
from app import db
|
from app import db
|
||||||
from app.classes.customer import Customer_Customer, Customer_Customer_schema
|
from app.classes.customer import Customer_Customer, Customer_Customer_schema
|
||||||
from app.classes.delivery import Delivery_Delivery, Delivery_Delivery_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"])
|
@search.route("/customer", methods=["GET"])
|
||||||
def search_customers():
|
def search_customers():
|
||||||
"""
|
"""
|
||||||
Searches for customers based on a keyword.
|
|
||||||
Supports special prefixes: @ for last name, ! for address, # for phone, $ for account.
|
|
||||||
Otherwise searches across multiple fields for each word provided in the search query.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
keyword = request.args.get('q')
|
keyword = request.args.get('q')
|
||||||
if not keyword or len(keyword) < 2:
|
search = "%{}%".format(keyword)
|
||||||
return jsonify([])
|
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 == '!':
|
||||||
|
|
||||||
# Check for special search prefixes
|
search = search[::1]
|
||||||
search_type = None
|
customer_list = (db.session
|
||||||
if keyword[0] in '!@#$':
|
.query(Customer_Customer)
|
||||||
search_type = keyword[0]
|
.filter(Customer_Customer.customer_address.ilike(search))
|
||||||
keyword = keyword[1:] # remove the prefix for the search term
|
.all())
|
||||||
|
# Phone Number
|
||||||
if search_type:
|
elif search_type == '#':
|
||||||
search_pattern = f"%{keyword}%"
|
search = search[::1]
|
||||||
if search_type == '@':
|
customer_list = (db.session
|
||||||
customer_list = (db.session
|
.query(Customer_Customer)
|
||||||
.query(Customer_Customer)
|
.filter(Customer_Customer.customer_phone_number.ilike(search))
|
||||||
.filter(Customer_Customer.customer_last_name.ilike(search_pattern))
|
.all())
|
||||||
.order_by(Customer_Customer.customer_last_name.asc())
|
# Account Number
|
||||||
.limit(20)
|
elif search_type == '$':
|
||||||
.all())
|
search = search[::1]
|
||||||
elif search_type == '!':
|
|
||||||
customer_list = (db.session
|
|
||||||
.query(Customer_Customer)
|
|
||||||
.filter(Customer_Customer.customer_address.ilike(search_pattern))
|
|
||||||
.order_by(Customer_Customer.customer_address.asc())
|
|
||||||
.limit(20)
|
|
||||||
.all())
|
|
||||||
elif search_type == '#':
|
|
||||||
customer_list = (db.session
|
|
||||||
.query(Customer_Customer)
|
|
||||||
.filter(Customer_Customer.customer_phone_number.ilike(search_pattern))
|
|
||||||
.order_by(Customer_Customer.customer_phone_number.asc())
|
|
||||||
.limit(20)
|
|
||||||
.all())
|
|
||||||
elif search_type == '$':
|
|
||||||
customer_list = (db.session
|
|
||||||
.query(Customer_Customer)
|
|
||||||
.filter(Customer_Customer.account_number.ilike(search_pattern))
|
|
||||||
.order_by(Customer_Customer.account_number.asc())
|
|
||||||
.limit(20)
|
|
||||||
.all())
|
|
||||||
else:
|
|
||||||
# fallback, though shouldn't happen
|
|
||||||
return jsonify([])
|
|
||||||
|
|
||||||
customer_schema = Customer_Customer_schema(many=True)
|
|
||||||
return jsonify(customer_schema.dump(customer_list))
|
|
||||||
|
|
||||||
# --- NEW SEARCH LOGIC ---
|
|
||||||
|
|
||||||
# 1. Split the incoming search query into individual words.
|
customer_list = (db.session
|
||||||
# e.g., "john main st" -> ["john", "main", "st"]
|
.query(Customer_Customer)
|
||||||
search_words = keyword.split()
|
.filter(Customer_Customer.account_number.ilike(search))
|
||||||
|
.order_by(Customer_Customer.account_number.asc())
|
||||||
|
.all())
|
||||||
|
|
||||||
# 2. We'll build a list of search conditions. Each condition will check
|
else:
|
||||||
# if a word exists in ANY of the relevant customer fields.
|
customer_list = (db.session
|
||||||
conditions = []
|
.query(Customer_Customer)
|
||||||
for word in search_words:
|
.filter(Customer_Customer.customer_last_name.ilike(search))
|
||||||
search_pattern = f"%{word}%"
|
.all())
|
||||||
# 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)
|
customer_schema = Customer_Customer_schema(many=True)
|
||||||
return jsonify(customer_schema.dump(customer_list))
|
return jsonify(customer_schema.dump(customer_list))
|
||||||
|
|
||||||
# The /delivery route is fine as is, so no changes needed there.
|
|
||||||
@search.route("/delivery", methods=["GET"])
|
@search.route("/delivery", methods=["GET"])
|
||||||
def search_delivery():
|
def search_delivery():
|
||||||
# ... (no changes to this function)
|
"""
|
||||||
|
pagination all customers
|
||||||
|
"""
|
||||||
keyword = request.args.get('q')
|
keyword = request.args.get('q')
|
||||||
if not keyword or not keyword.isdigit():
|
search = "%{}%".format(keyword)
|
||||||
return jsonify([])
|
search_type = (search[1])
|
||||||
delivery_ticket = db.session.query(Delivery_Delivery).filter(Delivery_Delivery.id == keyword).first()
|
|
||||||
if delivery_ticket:
|
delivery_ticket = (db.session
|
||||||
delivery_schema = Delivery_Delivery_schema(many=False)
|
.query(Delivery_Delivery)
|
||||||
return jsonify(delivery_schema.dump(delivery_ticket))
|
.filter(Delivery_Delivery.id.ilike(search))
|
||||||
else:
|
.all())
|
||||||
return jsonify({})
|
|
||||||
|
|
||||||
|
delivery_schema = Delivery_Delivery_schema(many=True)
|
||||||
|
return jsonify(delivery_schema.dump(delivery_ticket))
|
||||||
Reference in New Issue
Block a user