Changes as per requested from code review

Move the serialize method to the billing_schema
Update variable names
Improve tests
Fix bug in command
This commit is contained in:
Rebecca Law
2018-05-08 12:09:29 +01:00
parent ea3523199a
commit fd6e5f39cf
5 changed files with 71 additions and 55 deletions

View File

@@ -1,3 +1,5 @@
from datetime import datetime
create_or_update_free_sms_fragment_limit_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST annual billing schema",
@@ -8,3 +10,17 @@ create_or_update_free_sms_fragment_limit_schema = {
},
"required": ["free_sms_fragment_limit"]
}
def serialize_ft_billing_remove_emails(data):
results = []
billed_notifications = [x for x in data if x.notification_type != 'email']
for notifications in billed_notifications:
json_result = {
"month": (datetime.strftime(notifications.month, "%B")),
"notification_type": notifications.notification_type,
"billing_units": int(notifications.billable_units),
"rate": float(notifications.rate),
}
results.append(json_result)
return results

View File

@@ -1,25 +1,30 @@
from datetime import datetime
import json
from datetime import datetime
from flask import Blueprint, jsonify, request
from app.billing.billing_schemas import (
create_or_update_free_sms_fragment_limit_schema,
serialize_ft_billing_remove_emails
)
from app.dao.annual_billing_dao import (
dao_get_free_sms_fragment_limit_for_year,
dao_get_all_free_sms_fragment_limit,
dao_create_or_update_annual_billing_for_year,
dao_update_annual_billing_for_future_years
)
from app.dao.date_util import get_current_financial_year_start_year
from app.dao.date_util import get_months_for_financial_year
from app.dao.fact_billing_dao import fetch_monthly_billing_for_year
from app.dao.monthly_billing_dao import (
get_billing_data_for_financial_year,
get_monthly_billing_by_notification_type
)
from app.dao.date_util import get_months_for_financial_year
from app.errors import InvalidRequest
from app.errors import register_errors
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE
from app.utils import convert_utc_to_bst
from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year,
dao_get_all_free_sms_fragment_limit,
dao_create_or_update_annual_billing_for_year,
dao_update_annual_billing_for_future_years)
from app.billing.billing_schemas import create_or_update_free_sms_fragment_limit_schema
from app.errors import InvalidRequest
from app.schema_validation import validate
from app.dao.date_util import get_current_financial_year_start_year
from app.utils import convert_utc_to_bst
billing_blueprint = Blueprint(
'billing',
@@ -38,7 +43,7 @@ def get_yearly_usage_by_monthly_from_ft_billing(service_id):
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400
results = fetch_monthly_billing_for_year(service_id=service_id, year=year)
data = serialize_ft_billing(results)
data = serialize_ft_billing_remove_emails(results)
return jsonify(data)
@@ -204,17 +209,3 @@ def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, fin
free_sms_fragment_limit,
financial_year_start
)
def serialize_ft_billing(data):
results = []
no_emails = [x for x in data if x.notification_type != 'email']
for d in no_emails:
j = {
"month": (datetime.strftime(d.month, "%B")),
"notification_type": d.notification_type,
"billing_units": int(d.billable_units),
"rate": float(d.rate),
}
results.append(j)
return results