diff --git a/cope2n-api/fwd_api/utils/file.py b/cope2n-api/fwd_api/utils/file.py index 92142da..44a2e9b 100644 --- a/cope2n-api/fwd_api/utils/file.py +++ b/cope2n-api/fwd_api/utils/file.py @@ -19,6 +19,9 @@ from ..celery_worker.client_connector import c_connector import imagesize import csv +from openpyxl import load_workbook +from openpyxl.styles import Font, Border, Side, PatternFill, NamedStyle + def validate_feedback_file(csv_file_path): required_columns = ['redemptionNumber', 'requestId', 'imeiNumber', 'imeiNumber2', 'Purchase Date', 'retailer', 'Sold to party', 'timetakenmilli'] missing_columns = [] @@ -327,21 +330,104 @@ def build_media_url_v2(media_id: str, user_id: int, sub_id: int, u_sync_id: str) token = image_authenticator.generate_img_token_v2(user_id, sub_id, u_sync_id) return f'{settings.BASE_URL}/api/ctel/v2/media/request/{media_id}/?token={token}' -def json2xlsx(input: json): - """_summary_ - Args: - input (json): - : [{ - Subs: Jan, # Subtotal name - Metadata: {num_imei: 1, - ...: ...} - Data: [{num_imei: 1, - ...: ...}] - }] - OR - input (json): - : [] - Return xlsx (object) - """ - pass \ No newline at end of file +def get_value(_dict, keys): + keys = keys.split('.') + value = _dict + for key in keys: + if not key in value.keys(): + return "-" + else: + value = value.get(key, {}) + + if value != 0: + return value + else: + return "-" + + +def dict2xlsx(input: json): + red = "FF0000" + black = "000000" + green = "E2EFDA" + yellow = "FFF2CC" + gray = "D0CECE" + font_black = Font(name="Calibri", size=11, color=black) + font_black_bold = Font(name="Calibri", size=11, color=black, bold=True) + font_red = Font(name="Calibri", size=11, color=red) + thin = Side(border_style="thin", color=black) + border = Border(left=thin, right=thin, top=thin, bottom=thin) + fill_green = PatternFill(start_color=green, end_color=green, fill_type = "solid") + fill_yellow = PatternFill(start_color=yellow, end_color=yellow, fill_type = "solid") + fill_gray = PatternFill(start_color=gray, end_color=gray, fill_type = "solid") + normal_cell = NamedStyle(name="normal_cell", font=font_black, border=border) + normal_cell_red = NamedStyle(name="normal_cell_red", font=font_red, border=border) + + wb = load_workbook(filename = 'report.xlsx') + ws = wb['Sheet1'] + + mapping = { + 'A': 'subs', + 'B': 'extraction_date', + 'C': 'num_imei', + 'D': 'num_invoice', + 'E': 'total_images', + 'F': 'images_quality.successful', + 'G': 'images_quality.successful_percent', + 'H': 'images_quality.bad', + 'I': 'images_quality.bad_percent', + 'J': 'average_accuracy_rate.imei', + 'K': 'average_accuracy_rate.purchase_date', + 'L': 'average_accuracy_rate.retailer_name', + 'M': 'average_processing_time.imei', + 'N': 'average_processing_time.invoice', + 'O': 'usage.imei', + 'P': 'usage.invoice', + } + + start_index = 5 + + for subtotal in input: + ws['A' + str(start_index)] = subtotal['subs'] + ws['A' + str(start_index)].font = font_black + ws['A' + str(start_index)].border = border + ws['A' + str(start_index)].fill = fill_gray + + ws['B' + str(start_index)] = subtotal['extraction_date'] + ws['B' + str(start_index)].font = font_black_bold + ws['B' + str(start_index)].border = border + ws['B' + str(start_index)].fill = fill_green + + ws['C' + str(start_index)].border = border + ws['D' + str(start_index)].border = border + + for key in ['E', 'F', 'G', 'H', 'I']: + ws[key + str(start_index)] = get_value(subtotal, mapping[key]) + ws[key + str(start_index)].font = font_black + ws[key + str(start_index)].border = border + ws[key + str(start_index)].fill = fill_yellow + + for key in ['J', 'K', 'L', 'M', 'N']: + ws[key + str(start_index)] = get_value(subtotal, mapping[key]) + ws[key + str(start_index)].font = font_black + ws[key + str(start_index)].border = border + ws[key + str(start_index)].fill = fill_gray + + start_index += 1 + + for record in subtotal['data']: + for key in mapping.keys(): + value = get_value(record, mapping[key]) + ws[key + str(start_index)] = value + print(type(value)) + if 'average_accuracy_rate' in mapping[key] and type(value) in [int, float] and value < 95: + ws[key + str(start_index)].style = normal_cell_red + elif 'average_processing_time' in mapping[key] and type(value) in [int, float] and value > 2.0: + ws[key + str(start_index)].style = normal_cell_red + elif 'bad_percent' in mapping[key] and type(value) in [int, float] and value > 10: + ws[key + str(start_index)].style = normal_cell_red + else : + ws[key + str(start_index)].style = normal_cell + start_index += 1 + + return wb diff --git a/cope2n-api/report.xlsx b/cope2n-api/report.xlsx new file mode 100644 index 0000000..fc5b37d Binary files /dev/null and b/cope2n-api/report.xlsx differ