From d8e5d6eedc76b6d4480b0fd275d82799663c22a6 Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Sat, 27 Sep 2025 11:50:44 -0400 Subject: [PATCH] reverted search --- app/search/views.py | 148 ++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 89 deletions(-) diff --git a/app/search/views.py b/app/search/views.py index a0c5dbb..7315062 100755 --- a/app/search/views.py +++ b/app/search/views.py @@ -1,111 +1,81 @@ - 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. - 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') - if not keyword or len(keyword) < 2: - return jsonify([]) + 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 == '!': - # Check for special search prefixes - search_type = None - if keyword[0] in '!@#$': - search_type = keyword[0] - keyword = keyword[1:] # remove the prefix for the search term - - if search_type: - search_pattern = f"%{keyword}%" - if search_type == '@': - customer_list = (db.session - .query(Customer_Customer) - .filter(Customer_Customer.customer_last_name.ilike(search_pattern)) - .order_by(Customer_Customer.customer_last_name.asc()) - .limit(20) - .all()) - 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 --- + 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] - # 1. Split the incoming search query into individual words. - # e.g., "john main st" -> ["john", "main", "st"] - search_words = keyword.split() + customer_list = (db.session + .query(Customer_Customer) + .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 - # 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 - ) - ) + else: + customer_list = (db.session + .query(Customer_Customer) + .filter(Customer_Customer.customer_last_name.ilike(search)) + .all()) - # 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(): - # ... (no changes to this function) + """ + pagination all customers + """ keyword = request.args.get('q') - 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({}) + 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)) \ No newline at end of file