def flatten_report_for_csv(report):
    """
    Flattens the data structure returned by `watson.report()` for a csv export.

    Dates are formatted in a way that Excel (default csv module dialect) can
    handle them (i.e. YYYY-MM-DD HH:mm:ss).

    The result is a list of dictionaries where each element can contain two
    different things:

    1. The total `time` spent in a project during the report interval. In this
       case, the `tag` value will be empty.
    2. The partial `time` spent in a tag and project during the report
       interval. In this case, the `tag` value will contain a tag associated
       with the project.

    The sum of all elements where `tag` is empty corresponds to the total time
    of the report.
    """
    result = []
    datetime_from = report['timespan']['from'].format('YYYY-MM-DD HH:mm:ss')
    datetime_to = report['timespan']['to'].format('YYYY-MM-DD HH:mm:ss')
    for project in report['projects']:
        result.append({
            'from': datetime_from,
            'to': datetime_to,
            'project': project['name'],
            'tag': '',
            'time': project['time']
        })
        for tag in project['tags']:
            result.append({
                'from': datetime_from,
                'to': datetime_to,
                'project': project['name'],
                'tag': tag['name'],
                'time': tag['time']
            })
    return result