From 86b3d60c8f825c5ef1812a0d296405f2286fa181 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Wed, 20 Apr 2022 18:00:30 +0100 Subject: [PATCH] Refactor billing_schemas to use list comprehension --- app/billing/billing_schemas.py | 43 ++++++++++++++++------------------ 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/app/billing/billing_schemas.py b/app/billing/billing_schemas.py index 9c3ae9f46..2ee122f24 100644 --- a/app/billing/billing_schemas.py +++ b/app/billing/billing_schemas.py @@ -12,30 +12,27 @@ create_or_update_free_sms_fragment_limit_schema = { } -def serialize_ft_billing_remove_emails(data): - results = [] - billed_notifications = [x for x in data if x.notification_type != 'email'] - for notification in billed_notifications: - json_result = { - "month": (datetime.strftime(notification.month, "%B")), - "notification_type": notification.notification_type, - "billing_units": notification.billable_units, - "rate": float(notification.rate), - "postage": notification.postage, +def serialize_ft_billing_remove_emails(rows): + return [ + { + "month": (datetime.strftime(row.month, "%B")), + "notification_type": row.notification_type, + "billing_units": row.billable_units, + "rate": float(row.rate), + "postage": row.postage, } - results.append(json_result) - return results + for row in rows + if row.notification_type != 'email' + ] -def serialize_ft_billing_yearly_totals(data): - yearly_totals = [] - for total in data: - json_result = { - "notification_type": total.notification_type, - "billing_units": total.billable_units, - "rate": float(total.rate), - "letter_total": float(total.billable_units * total.rate) if total.notification_type == 'letter' else 0 +def serialize_ft_billing_yearly_totals(rows): + return [ + { + "notification_type": row.notification_type, + "billing_units": row.billable_units, + "rate": float(row.rate), + "letter_total": float(row.billable_units * row.rate) if row.notification_type == 'letter' else 0, } - yearly_totals.append(json_result) - - return yearly_totals + for row in rows + ]