I wrote this piece of code to eliminate the need for multiple
if then statements based on the filters from the incoming REST request.
If the request has category in it, then the query has to be filtered by category, the same for
product_location. I wrote a single statement instead of having to check for the filters and have multiple queries.
product_category is equal to
All Category, then a filter need not be applied, etc.
query = \ 'select AVG(stock_date - order_date) as c,product_category from data_table_data where stock_date between :s1 and :s2 ' \ + (('and product_category = :category ' if category != 'All Category' else '')) + (('and product_location = :loc ' if store_location != 'All Stores' else '')) \ + 'group by product_category' data = engine.execute(text(query), s1=startDate, s2=endDate, category=category, loc=store_location).fetchall()