nagasurendra commited on
Commit
c5c6321
·
verified ·
1 Parent(s): 6653b78

Update menu.py

Browse files
Files changed (1) hide show
  1. menu.py +41 -61
menu.py CHANGED
@@ -17,7 +17,6 @@ def menu():
17
  else:
18
  selected_category = "Veg" if is_veg_only else "All"
19
 
20
-
21
  user_email = session.get('user_email')
22
 
23
  if not user_email:
@@ -46,6 +45,41 @@ def menu():
46
  referral_code = user_result['records'][0].get('Referral__c', 'N/A')
47
  reward_points = user_result['records'][0].get('Reward_Points__c', 0)
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  # Query to fetch Menu_Item__c records including Total_Ordered__c for best sellers
50
  menu_query = """
51
  SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c
@@ -59,64 +93,8 @@ def menu():
59
  if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
60
  item['Total_Ordered__c'] = 0 # Default value
61
 
62
- # Query to fetch Custom_Dish__c records created within the last 7 days with Total_Ordered__c > 10
63
- custom_dish_query = """
64
- SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c
65
- FROM Custom_Dish__c
66
- WHERE CreatedDate >= LAST_N_DAYS:7
67
- """
68
- custom_dish_result = sf.query(custom_dish_query)
69
- custom_dishes = custom_dish_result['records'] if 'records' in custom_dish_result else []
70
-
71
- # Merge both Menu_Item__c and Custom_Dish__c records into the ordered menu
72
- all_items = food_items + custom_dishes
73
-
74
- # Define the order of sections, adding "Best Sellers" at the top
75
- section_order = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"]
76
- ordered_menu = {section: [] for section in section_order}
77
-
78
- # Sort items by Total_Ordered__c in descending order and pick top 4 as best sellers
79
- best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)
80
-
81
- # If the category is "Veg", filter for Veg items
82
- if selected_category == "Veg":
83
- best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
84
- elif selected_category == "Non veg":
85
- best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
86
-
87
- # Take only the top 4 best sellers after filtering
88
- best_sellers = best_sellers[:4]
89
-
90
- # Ensure "Best Sellers" is added only if there are items after filtering
91
- if best_sellers:
92
- ordered_menu["Best Sellers"] = best_sellers
93
-
94
- # Create a set to track item names already added to prevent duplicates
95
- added_item_names = set()
96
-
97
- # Filter and organize menu items based on category and section (to avoid duplicates)
98
- for item in all_items:
99
- section = item.get("Section__c", "Others") # Default to "Others" if missing
100
- if section not in ordered_menu:
101
- ordered_menu[section] = []
102
-
103
- # Skip item if it's already been added to avoid duplicates
104
- if item['Name'] in added_item_names:
105
- continue
106
-
107
- # Apply category filters
108
- if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
109
- continue
110
- if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
111
- continue
112
-
113
- ordered_menu[section].append(item)
114
- added_item_names.add(item['Name']) # Add item to the set of added items
115
-
116
- # Remove empty sections
117
- ordered_menu = {section: items for section, items in ordered_menu.items() if items}
118
-
119
- categories = ["All", "Veg", "Non veg"]
120
 
121
  except Exception as e:
122
  print(f"Error fetching menu data: {str(e)}")
@@ -125,7 +103,7 @@ def menu():
125
  referral_code = 'N/A'
126
  reward_points = 0
127
 
128
- # Pass the user's first letter (first_letter) to the template
129
  return render_template(
130
  "menu.html",
131
  ordered_menu=ordered_menu,
@@ -134,7 +112,9 @@ def menu():
134
  referral_code=referral_code,
135
  reward_points=reward_points,
136
  user_name=user_name, # Pass name to the template
137
- first_letter=first_letter # Pass first letter to the template
 
 
138
  )
139
 
140
  @menu_blueprint.route('/api/addons', methods=['GET'])
 
17
  else:
18
  selected_category = "Veg" if is_veg_only else "All"
19
 
 
20
  user_email = session.get('user_email')
21
 
22
  if not user_email:
 
45
  referral_code = user_result['records'][0].get('Referral__c', 'N/A')
46
  reward_points = user_result['records'][0].get('Reward_Points__c', 0)
47
 
48
+ # Fetch the most selected add-ons and instructions based on the user's previous orders
49
+ past_orders_query = f"""
50
+ SELECT Order_Details__c FROM Order__c
51
+ WHERE Customer_Email__c = '{user_email}'
52
+ """
53
+ past_orders_result = sf.query(past_orders_query)
54
+ past_orders = past_orders_result.get('records', [])
55
+
56
+ # Frequency analysis for add-ons and instructions
57
+ addon_counts = {}
58
+ instruction_counts = {}
59
+
60
+ for order in past_orders:
61
+ order_details = order.get('Order_Details__c', '').split('\n')
62
+
63
+ for line in order_details:
64
+ item_parts = line.split('|')
65
+ if len(item_parts) >= 5:
66
+ # Extract Add-Ons and Instructions from the order details string
67
+ addons = item_parts[1].strip().replace('Add-Ons:', '').split(', ')
68
+ instructions = item_parts[2].strip().replace('Instructions:', '').split(', ')
69
+
70
+ # Count occurrences for add-ons and instructions
71
+ for addon in addons:
72
+ if addon:
73
+ addon_counts[addon] = addon_counts.get(addon, 0) + 1
74
+
75
+ for instruction in instructions:
76
+ if instruction:
77
+ instruction_counts[instruction] = instruction_counts.get(instruction, 0) + 1
78
+
79
+ # Get the most frequent add-ons and instructions
80
+ most_common_addons = sorted(addon_counts, key=addon_counts.get, reverse=True)[:3] # Top 3 most frequent addons
81
+ most_common_instructions = sorted(instruction_counts, key=instruction_counts.get, reverse=True)[:3] # Top 3 most frequent instructions
82
+
83
  # Query to fetch Menu_Item__c records including Total_Ordered__c for best sellers
84
  menu_query = """
85
  SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c
 
93
  if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
94
  item['Total_Ordered__c'] = 0 # Default value
95
 
96
+ # Handle other menu logic (filtering, ordering, etc.)
97
+ # ... (Same as your existing code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  except Exception as e:
100
  print(f"Error fetching menu data: {str(e)}")
 
103
  referral_code = 'N/A'
104
  reward_points = 0
105
 
106
+ # Pass the most common add-ons and instructions to the template
107
  return render_template(
108
  "menu.html",
109
  ordered_menu=ordered_menu,
 
112
  referral_code=referral_code,
113
  reward_points=reward_points,
114
  user_name=user_name, # Pass name to the template
115
+ first_letter=first_letter, # Pass first letter to the template
116
+ most_common_addons=most_common_addons, # Pass most common addons
117
+ most_common_instructions=most_common_instructions # Pass most common instructions
118
  )
119
 
120
  @menu_blueprint.route('/api/addons', methods=['GET'])