|
1 |
| -from argparse import Namespace |
| 1 | +from datetime import datetime, timedelta |
| 2 | +import os |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import click |
| 6 | +from pytz import timezone |
2 | 7 |
|
3 |
| -from compiler_admin import RESULT_SUCCESS |
4 | 8 | from compiler_admin.services.toggl import TOGGL_COLUMNS, download_time_entries
|
5 | 9 |
|
6 | 10 |
|
7 |
| -def download(args: Namespace, *extras): |
8 |
| - params = dict( |
9 |
| - start_date=args.start, end_date=args.end, output_path=args.output, output_cols=TOGGL_COLUMNS, billable=args.billable |
10 |
| - ) |
| 11 | +TZINFO = timezone(os.environ.get("TZ_NAME", "America/Los_Angeles")) |
11 | 12 |
|
12 |
| - if args.client_ids: |
13 |
| - params.update(dict(client_ids=args.client_ids)) |
14 |
| - if args.project_ids: |
15 |
| - params.update(dict(project_ids=args.project_ids)) |
16 |
| - if args.task_ids: |
17 |
| - params.update(dict(task_ids=args.task_ids)) |
18 |
| - if args.user_ids: |
19 |
| - params.update(dict(user_ids=args.user_ids)) |
20 | 13 |
|
21 |
| - download_time_entries(**params) |
| 14 | +def local_now(): |
| 15 | + return datetime.now(tz=TZINFO) |
| 16 | + |
| 17 | + |
| 18 | +def prior_month_end(): |
| 19 | + now = local_now() |
| 20 | + first = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
| 21 | + return first - timedelta(days=1) |
| 22 | + |
| 23 | + |
| 24 | +def prior_month_start(): |
| 25 | + end = prior_month_end() |
| 26 | + return end.replace(day=1) |
22 | 27 |
|
23 |
| - return RESULT_SUCCESS |
| 28 | + |
| 29 | +@click.command() |
| 30 | +@click.option( |
| 31 | + "--start", |
| 32 | + metavar="YYYY-MM-DD", |
| 33 | + default=prior_month_start(), |
| 34 | + callback=lambda ctx, param, val: datetime.strptime(val, "%Y-%m-%d %H:%M:%S%z"), |
| 35 | + help="The start date of the reporting period. Defaults to the beginning of the prior month.", |
| 36 | +) |
| 37 | +@click.option( |
| 38 | + "--end", |
| 39 | + metavar="YYYY-MM-DD", |
| 40 | + default=prior_month_end(), |
| 41 | + callback=lambda ctx, param, val: datetime.strptime(val, "%Y-%m-%d %H:%M:%S%z"), |
| 42 | + help="The end date of the reporting period. Defaults to the end of the prior month.", |
| 43 | +) |
| 44 | +@click.option( |
| 45 | + "--output", |
| 46 | + help="The path to the file where downloaded data should be written. Defaults to a path calculated from the date range.", |
| 47 | +) |
| 48 | +@click.option( |
| 49 | + "--all", |
| 50 | + "billable", |
| 51 | + is_flag=True, |
| 52 | + default=True, |
| 53 | + help="Download all time entries. The default is to download only billable time entries.", |
| 54 | +) |
| 55 | +@click.option( |
| 56 | + "-c", |
| 57 | + "--client", |
| 58 | + "client_ids", |
| 59 | + multiple=True, |
| 60 | + help="An ID for a Toggl Client to filter for in reports. Can be supplied more than once.", |
| 61 | + metavar="CLIENT_ID", |
| 62 | + type=int, |
| 63 | +) |
| 64 | +@click.option( |
| 65 | + "-p", |
| 66 | + "--project", |
| 67 | + "project_ids", |
| 68 | + help="An ID for a Toggl Project to filter for in reports. Can be supplied more than once.", |
| 69 | + metavar="PROJECT_ID", |
| 70 | + multiple=True, |
| 71 | + type=int, |
| 72 | +) |
| 73 | +@click.option( |
| 74 | + "-t", |
| 75 | + "--task", |
| 76 | + "task_ids", |
| 77 | + help="An ID for a Toggl Project Task to filter for in reports. Can be supplied more than once.", |
| 78 | + metavar="TASK_ID", |
| 79 | + multiple=True, |
| 80 | + type=int, |
| 81 | +) |
| 82 | +@click.option( |
| 83 | + "-u", |
| 84 | + "--user", |
| 85 | + "user_ids", |
| 86 | + help="An ID for a Toggl User to filter for in reports. Can be supplied more than once.", |
| 87 | + metavar="USER_ID", |
| 88 | + multiple=True, |
| 89 | + type=int, |
| 90 | +) |
| 91 | +def download( |
| 92 | + start: datetime, |
| 93 | + end: datetime, |
| 94 | + output: str = "", |
| 95 | + billable: bool = True, |
| 96 | + client_ids: List[int] = [], |
| 97 | + project_ids: List[int] = [], |
| 98 | + task_ids: List[int] = [], |
| 99 | + user_ids: List[int] = [], |
| 100 | +): |
| 101 | + """ |
| 102 | + Download a Toggl time report in CSV format. |
| 103 | + """ |
| 104 | + if not output: |
| 105 | + output = f"Toggl_time_entries_{start.strftime('%Y-%m-%d')}_{end.strftime('%Y-%m-%d')}.csv" |
| 106 | + |
| 107 | + params = dict(start_date=start, end_date=end, output_path=output, output_cols=TOGGL_COLUMNS) |
| 108 | + |
| 109 | + if billable: |
| 110 | + params.update(dict(billable=billable)) |
| 111 | + if client_ids: |
| 112 | + params.update(dict(client_ids=client_ids)) |
| 113 | + if project_ids: |
| 114 | + params.update(dict(project_ids=project_ids)) |
| 115 | + if task_ids: |
| 116 | + params.update(dict(task_ids=task_ids)) |
| 117 | + if user_ids: |
| 118 | + params.update(dict(user_ids=user_ids)) |
| 119 | + |
| 120 | + click.echo("Downloading Toggl time entries with parameters:") |
| 121 | + for k, v in params.items(): |
| 122 | + click.echo(f" {k}: {v}") |
| 123 | + |
| 124 | + download_time_entries(**params) |
0 commit comments