Change searching to be case insensitive

This commit is contained in:
BBaoVanC 2020-10-06 19:56:32 -05:00
parent 11f21020b8
commit 97373e0ae2
No known key found for this signature in database
GPG Key ID: 6D74C8B0E7D791C2

View File

@ -10,6 +10,7 @@ from flask import Flask, jsonify, request
from flask_api import status
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import func
from libdb import Session, Location, ProductQuantity, Product, Brand, Unit, Purchase, Use
@ -34,8 +35,8 @@ def api_get_location_information(search):
"""Route to get information about a location"""
session = Session()
try:
data = session.query(Location.name, Location.description). \
filter(Location.name == search).one()
data = session.query(Location.name, Location.description).filter(
func.lower(Location.name) == func.lower(search)).one()
except NoResultFound:
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
data2 = {}
@ -51,7 +52,7 @@ def api_get_current_quantities(location):
data = {}
try:
for upc, quantity in session.query(ProductQuantity.product_upc, ProductQuantity.quantity) \
.filter(ProductQuantity.location == location).all():
.filter(func.lower(ProductQuantity.location) == func.lower(location)).all():
data[upc] = quantity
return jsonify(data)
except NoResultFound:
@ -65,13 +66,13 @@ def api_get_quantity_of_product_in_location(location, searchmethod, search):
try:
if searchmethod == 'upc':
data = session.query(ProductQuantity.quantity).filter(
ProductQuantity.location == location,
func.lower(ProductQuantity.location) == func.lower(location),
ProductQuantity.product_upc == search).one()
return jsonify(data[0])
elif searchmethod == 'name':
data = session.query(ProductQuantity.quantity).filter(
ProductQuantity.location == location,
ProductQuantity.name == search).one()
func.lower(ProductQuantity.location) == func.lower(location),
func.lower(ProductQuantity.name) == func.lower(search)).one()
return jsonify(data[0])
else:
print("invalid search method, sending 400")
@ -105,7 +106,7 @@ def api_get_product_information(searchmethod, search):
if searchmethod == 'name':
result = session.query(Product.upc, Product.brand, Product.name,
Product.size, Product.sizeunit, Product.description).filter(
Product.name == search
func.lower(Product.name) == func.lower(search)
).one()
data = {'upc': result[0], 'brand': result[1], 'name': result[2], 'size': result[3],
'sizeunit': result[4], 'description': result[5]}
@ -143,11 +144,11 @@ def api_get_brands():
@app.route("/api/v1/brand/name/<search>", methods=["GET"])
def api_get_brand_by_name(search):
"""Route to get information about a location"""
"""Route to get information about a brand"""
session = Session()
try:
data = session.query(Brand.name, Brand.description).filter(
Brand.name == search).one()
func.lower(Brand.name) == func.lower(search)).one()
except NoResultFound:
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
data2 = {'name': data.name, 'description': data.description}
@ -173,7 +174,7 @@ def api_get_unit_by_name(search):
session = Session()
try:
data = session.query(Unit.name, Unit.description).filter(
Unit.name == search).one()
func.lower(Unit.name) == func.lower(search)).one()
except NoResultFound:
return jsonify({'error': 'NO_RESULT_FOUND'}), status.HTTP_404_NOT_FOUND
data2 = {'name': data.name, 'description': data.description}