take advantage of click's type validation/coercion

(saves us having to write the stuff ourselves). Also adds a small
click plugin to do datetime parsing.

Sample output:

```
[leohemsted:~/dev/api]$ flask command create_provider_rates --help
Usage: flask command create_provider_rates [OPTIONS]

  Backfill rates for a given provider

Options:
  -p, --provider_name [mmg|firetext|ses]
                                  [required]
  -c, --cost FLOAT                Cost (pence) per message including decimals
                                  [required]
  -d, --valid_from DATE           [required]
  --help                          Show this message and exit.
[leohemsted:~/dev/api]$ flask command create_provider_rates -p ses -c 1.234 -d invalid
Usage: flask command create_provider_rates [OPTIONS]

Error: Invalid value for "-d" / "--valid_from": Could not parse datetime string "invalid" formatted as %Y-%m-%dT%H:%M:%S
```
This commit is contained in:
Leo Hemsted
2017-11-28 11:10:19 +00:00
parent 721202b44f
commit 5b7118973e
3 changed files with 8 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ import functools
import flask
from flask import current_app
import click
from click_datetime import Datetime as click_dt
from app import db
from app.dao.monthly_billing_dao import (
@@ -49,17 +50,14 @@ class notify_command:
@notify_command()
@click.option('-p', '--provider_name', required=True, help='Provider name', type=click.Choice(PROVIDERS))
@click.option('-p', '--provider_name', required=True, type=click.Choice(PROVIDERS))
@click.option('-c', '--cost', required=True, help='Cost (pence) per message including decimals', type=float)
@click.option('-d', '--valid_from', required=True, help="Date (%Y-%m-%dT%H:%M:%S) valid from")
@click.option('-d', '--valid_from', required=True, type=click_dt(format='%Y-%m-%dT%H:%M:%S'))
def create_provider_rates(provider_name, cost, valid_from):
"""
Backfill rates for a given provider
"""
cost = Decimal(cost)
valid_from = datetime.strptime('%Y-%m-%dT%H:%M:%S', valid_from)
dao_create_provider_rates(provider_name, valid_from, cost)
@@ -194,7 +192,7 @@ def link_inbound_numbers_to_service():
@notify_command()
@click.option('-y', '--year', required=True, help="Use for integer value for year, e.g. 2017", type=int)
@click.option('-y', '--year', required=True, help="e.g. 2017", type=int)
def populate_monthly_billing(year):
"""
Populate monthly billing table for all services for a given year.
@@ -228,14 +226,12 @@ def populate_monthly_billing(year):
@notify_command()
@click.option('-s', '--start_date', required=True, help="Date (%Y-%m-%d) start date inclusive")
@click.option('-e', '--end_date', required=True, help="Date (%Y-%m-%d) end date inclusive")
@click.option('-s', '--start_date', required=True, help="start date inclusive", type=click_dt(format='%Y-%m-%d'))
@click.option('-e', '--end_date', required=True, help="end date inclusive", type=click_dt(format='%Y-%m-%d'))
def backfill_processing_time(start_date, end_date):
"""
Send historical performance platform stats.
"""
start_date = datetime.strptime(start_date, '%Y-%m-%d')
end_date = datetime.strptime(end_date, '%Y-%m-%d')
delta = end_date - start_date