Untitled

 avatar
unknown
plain_text
9 months ago
8.7 kB
13
Indexable
@turbo.route('/submit/', methods=['GET']) #after submit button is clicked, this function is called
  def search_result():
    try:
      
      itemnmbr = request.args.get('itemnmbr') #this statement looks for a name 'itemnmbr' in the URL string and stores the value as itemnmbr variable.
      if itemnmbr is not None:
        oven_no = itemnmbr.strip().upper()
      else:
        oven_no = itemnmbr


      models = [m for m in request.args.getlist('oven_model') if m] #requesting a list of parameter. it can be empty as well so only taking parameter that has values
      version = [v for v in request.args.getlist('oven_version') if v]
      country = [c for c in request.args.getlist('oven_country') if c]
      phase = [e for e in request.args.getlist('oven_phase') if e]
      voltage = [v for v in request.args.getlist('oven_voltage') if v]
      amphs = [a for a in request.args.getlist('oven_amph') if a]
      hz = [h for h in request.args.getlist('oven_hz') if h]
      customer = [c for c in request.args.getlist('oven_customer') if c]
      color = [c for c in request.args.getlist('oven_color') if c]

      feature = [f for f in request.args.getlist('oven_feature') if f]

      order = request.args.get('sort_by', '') #requesting in which order the user wants the results to be sorted


      df = get_alloven(conn)
      split_data = df["ITEMDESC"].str.split(",", expand = True) #splitting the column ITEMDESC into separate columns by using ',' as the separator
      split_data= split_data.map(lambda x: x.strip() if isinstance(x, str) else x) #stripping leading or trailing spaces if the value is string
      df['MODEL'] = split_data[1] #giving a name to the separated column based on their location in the original ITEMDESC column.
      df['VERSION'] = split_data[2]
      df['COUNTRY'] = split_data[3]
      df['CUSTOMER'] = split_data[4]
      df['COLOR'] = split_data[5]
      df['PHASE'] = split_data[6]
      df['VOLTAGE'] = split_data[7]
      df['AMPHS'] = split_data[8]
      df['HZ'] = split_data[9]
      df['FEATURE'] = split_data[10]
    #df[['Model', 'version', 'country','customer', 'color', 'phase', 'voltage', 'amphs', 'hz', 'feature']] = df["ITEMDESC"].str.split(",", expand = True)
    
    # 4. Get the selected model from the request.
      selected_model = request.args.get('oven_model')
      print("Your oven model is", selected_model)
    
    # 5. Correctly filter the DataFrame using boolean indexing.
    # Check if the 'Model' column value matches the selected model (case-insensitively).
      if selected_model:
          filtered_df = df[df['MODEL'].str.strip().str.upper() == selected_model.upper()]
      else:
        # If no model is selected, return an empty list or all versions.
        filtered_df = df
        
      
    
    # 6. Extract unique, non-null versions from the filtered DataFrame.
    # The .str.strip() is used to remove any whitespace around the strings.
      updated_filters = {
      'versions': sorted(filtered_df['VERSION'].str.strip().dropna().unique().tolist()),
        'countries': sorted(filtered_df['COUNTRY'].str.strip().dropna().unique().tolist()),
        'customers' : sorted(filtered_df['CUSTOMER'].str.strip().dropna().unique().tolist()),
        'colors': sorted(filtered_df['COLOR'].str.strip().dropna().unique().tolist()),
        'phases': sorted(filtered_df['PHASE'].str.strip().dropna().unique().tolist()),
        'voltages': sorted(filtered_df['VOLTAGE'].str.strip().dropna().unique().tolist()),
        'amphs': sorted(filtered_df['AMPHS'].str.strip().dropna().unique().tolist()),
        'hz': sorted(filtered_df['HZ'].str.strip().dropna().unique().tolist()),
        'features': sorted(filtered_df['FEATURE'].str.strip().dropna().unique().tolist())
    }
      
      print(updated_filters)

      if version:
         filtered_df = df[df['MODEL'].str.strip().str.upper() == selected_model.upper()]
      else:
        # If no model is selected, return an empty list or all versions.
        filtered_df = df
        
      
    
    # 6. Extract unique, non-null versions from the filtered DataFrame.
    # The .str.strip() is used to remove any whitespace around the strings.
      updated_filters = {
      
        'countries': sorted(filtered_df['COUNTRY'].str.strip().dropna().unique().tolist()),
        'customers' : sorted(filtered_df['CUSTOMER'].str.strip().dropna().unique().tolist()),
        'colors': sorted(filtered_df['COLOR'].str.strip().dropna().unique().tolist()),
        'phases': sorted(filtered_df['PHASE'].str.strip().dropna().unique().tolist()),
        'voltages': sorted(filtered_df['VOLTAGE'].str.strip().dropna().unique().tolist()),
        'amphs': sorted(filtered_df['AMPHS'].str.strip().dropna().unique().tolist()),
        'hz': sorted(filtered_df['HZ'].str.strip().dropna().unique().tolist()),
        'features': sorted(filtered_df['FEATURE'].str.strip().dropna().unique().tolist())
    }
      
   
        


      results = [] #creating an empty list named results
      search_attempted = False #setting a flag
      if itemnmbr: #this condition function gives value to the results list based on if user used search bar or side bar
        search_attempted = True
        results = get_ovens_by_itemnmbr(oven_no, conn) #if searchbar is used, results will have a list of that one oven passed as a itemnmbr parameter and the condition will be exited
      elif models or version or country or phase or voltage or amphs or hz or customer or color or feature: #if sidebar is used, this condition will be executed
        search_attempted = True
        results = get_ovens_by_model(models, version, country, phase, voltage, amphs, hz, customer, color, feature, order, conn)
      else:
        results = [] #if none of the condition is true, it will return an empty list

            # 🔹 Decide which filter lists to send to the template


      if selected_model:
          # use updated filters for dynamic narrowing
          print("Dynamic filter mode: using updated filters")
          omodel, _, _, _, _, _, _, _, _, _ = get_dropdown_lists(conn)
          oversion = updated_filters['versions']
          ocountry = updated_filters['countries']
          ocustomer = updated_filters['customers']
          ocolor = updated_filters['colors']
          ophase = updated_filters['phases']
          ovoltage = updated_filters['voltages']
          oamphs = updated_filters['amphs']
          ohz = updated_filters['hz']
          ofeature = updated_filters['features']
      else:
          # use full default lists when no model is chosen
          print("Full filter mode: using default get_dropdown_lists")
          omodel, oversion, ocountry, ophase, ovoltage, oamphs, ohz, ocustomer, ocolor, ofeature = get_dropdown_lists(conn)

      if version:
          # use updated filters for dynamic narrowing
          print("Dynamic filter mode: using updated filters")
          omodel, oversion, _, _, _, _, _, _, _, _ = get_dropdown_lists(conn)
          
          ocountry = updated_filters['countries']
          ocustomer = updated_filters['customers']
          ocolor = updated_filters['colors']
          ophase = updated_filters['phases']
          ovoltage = updated_filters['voltages']
          oamphs = updated_filters['amphs']
          ohz = updated_filters['hz']
          ofeature = updated_filters['features']
      else:
          # use full default lists when no model is chosen
          print("Full filter mode: using default get_dropdown_lists")
          omodel, oversion, ocountry, ophase, ovoltage, oamphs, ohz, ocustomer, ocolor, ofeature = get_dropdown_lists(conn)

      conn.close()

      return render_template(
          'lookup.html',
          results=results,
          search_attempted=search_attempted,
          oven_models=omodel,
          oven_versions=oversion,
          oven_countries=ocountry,
          oven_phases=ophase,
          oven_voltages=ovoltage,
          oven_amphs=oamphs,
          oven_hzs=ohz,
          oven_customers=ocustomer,
          oven_colors=ocolor,
          oven_features=ofeature,
          selected_model=models,
          selected_version=version,
          selected_country=country,
          selected_phase=phase,
          selected_voltage=voltage,
          selected_amph=amphs,
          selected_hz=hz,
          selected_customer=customer,
          selected_color=color,
          selected_feature=feature,
          selected_order=order,
          selected_itemnmbr=oven_no
      )
Editor is loading...
Leave a Comment