srilakshu012456 commited on
Commit
c719792
·
verified ·
1 Parent(s): 312f27a

Create utils.py

Browse files
Files changed (1) hide show
  1. public_api/utils.py +154 -0
public_api/utils.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # utils.py
2
+ import arrow
3
+ def format_bot_response(text, source="SYSTEM", data=None, state=None, debug="", followup="",show_export=False,type=""):
4
+ """Ensures every return has the exact same structure."""
5
+ return {
6
+ "bot_response": text,
7
+ "source": source,
8
+ "data": data,
9
+ "state": state,
10
+ "debug": debug,
11
+ "followup": followup,
12
+ "show_export": show_export,
13
+ "type":type
14
+ }
15
+
16
+ def flatten_json(y, prefix=''):
17
+ """Flatten nested JSON into dot-separated keys."""
18
+ out = {}
19
+ if isinstance(y, dict):
20
+ for k, v in y.items():
21
+ out.update(flatten_json(v, f"{prefix}{k}." if prefix else k + "."))
22
+ elif isinstance(y, list):
23
+ for i, v in enumerate(y):
24
+ out.update(flatten_json(v, f"{prefix}{i}." if prefix else f"{i}."))
25
+ else:
26
+ out[prefix[:-1]] = y # remove trailing dot
27
+ return out
28
+
29
+ def extract_fields(flat_data, mapping, formatters=None, requested_order=None):
30
+ result = {}
31
+ if formatters is None:
32
+ formatters = {}
33
+
34
+ # Determine the order: use requested_order if provided, otherwise the mapping order
35
+ display_keys = requested_order if requested_order else mapping.keys()
36
+
37
+ for display_name in display_keys:
38
+ # Get the JSON path from your mapping for this specific display name
39
+ json_path = mapping.get(display_name)
40
+ if not json_path:
41
+ continue
42
+
43
+ # Get the raw value (could be None if the key doesn't exist in the JSON)
44
+ val = flat_data.get(json_path)
45
+
46
+ # Apply Human Translation (humanize_value handles the None -> "N/A" logic)
47
+ if display_name in formatters:
48
+ try:
49
+ val = formatters[display_name](val)
50
+ except Exception:
51
+ # Keep original value if formatter crashes
52
+ pass
53
+
54
+ result[display_name] = val
55
+
56
+ return result
57
+
58
+ def humanize_value(val):
59
+ """
60
+ Handles Booleans, messy strings (NOT_REQUIRED), and preserves 0.
61
+ """
62
+ # Handle Strict Booleans (True/False)
63
+ if isinstance(val, bool):
64
+ return "Yes" if val else "No"
65
+
66
+ # Handle specific "Useless" Strings
67
+ if isinstance(val, str) and val.upper() in ['NOT_REQUIRED', 'NONE', 'TIME_NONE', '']:
68
+ return "N/A"
69
+
70
+ # Handle Numbers (preserve 0, but None becomes N/A)
71
+ if val is None:
72
+ return "N/A"
73
+
74
+ return val
75
+
76
+ # Helper for Velocity Codes (The "B" issue)
77
+ def translate_velocity(code):
78
+ velocity_map = {
79
+ 'A': 'Fast Moving',
80
+ 'B': 'Medium Moving',
81
+ 'C': 'Slow Moving'
82
+ }
83
+ # Return the mapped text, or default to the original code if not found
84
+ return velocity_map.get(code, code)
85
+
86
+ """
87
+ def extract_fields(flat_data: dict, user_fields: list, mapping: dict):
88
+ #Return only requested fields based on user-friendly mapping.
89
+ result = {}
90
+ for field in user_fields:
91
+ key = mapping.get(field.lower())
92
+ if key:
93
+ result[field] = flat_data.get(key, "Not available")
94
+ else:
95
+ result[field] = "Field not recognized"
96
+ return result
97
+ """
98
+
99
+ """
100
+ def extract_fields(flat_data, mapping, formatters=None):
101
+ result = {}
102
+ if formatters is None:
103
+ formatters = {}
104
+
105
+ for display_name, json_path in mapping.items():
106
+ val = flat_data.get(json_path)
107
+
108
+ # 2. Check if data exists
109
+ if val is None:
110
+ result[display_name] = "Not available"
111
+ continue
112
+
113
+ # 3. Apply Human Translation (if defined)
114
+ if display_name in formatters:
115
+ try:
116
+ # Call the specific formatter function for this field
117
+ val = formatters[display_name](val)
118
+ except Exception as e:
119
+ # If formatting fails, just keep original value to be safe
120
+ pass
121
+
122
+ result[display_name] = val
123
+
124
+ return result
125
+ """
126
+
127
+ def format_hold_type(val):
128
+ mapping = {"A":"Allocation Hold","C":"Critical Hold","D":"Customs Hold","M":"Non Movable","O":"RF Outbound Audit Hold","Q": "QA Hold","S":"Shipping Hold","SC":"Non status changeble"}
129
+
130
+ return mapping.get(val, val) # Returns "QA Hold" if Q, else original value
131
+
132
+ def format_reason_code(val):
133
+ mapping = {"QUAL": "Quality Hold", "OUT-AUD-HLD": "RF Outbount Audit Hold","PRDLIN-HOLD":"Production Line Hold"}
134
+ return mapping.get(val, val)
135
+
136
+
137
+
138
+ def format_backend_date(date):
139
+ # Arrow automatically recognizes the ISO format from your DB/API
140
+ date_obj = arrow.get(date)
141
+
142
+ # Format to exactly: DD-MM-YYYY HH:MM:SS AM/PM
143
+ return date_obj.format('DD-MM-YYYY hh:mm:ssA')
144
+
145
+ def translate_loc_sts(val):
146
+ mapping={
147
+ "E":"Empty",
148
+ "F":"Full",
149
+ "I":"Inventory Error",
150
+ "L":"Locked",
151
+ "N":"Pending",
152
+ "P":"Partially Full",
153
+ }
154
+ return mapping.get(val, val)