Repository: frappe/frappe

Issue:
Auto Email Report CSV Format Broken
When downloading (or sending) an auto email report with format "CSV" the following error occurs:

```
raceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 66, in application
    response = frappe.api.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/api.py", line 56, in handle
    return frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 21, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 56, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1007, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/doctype/auto_email_report/auto_email_report.py", line 153, in download
    data = auto_email_report.get_report_content()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/doctype/auto_email_report/auto_email_report.py", line 61, in get_report_content
    filters = self.filters, as_dict=True)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/report/report.py", line 152, in get_data
    order_by = _format(self.ref_doctype, 'modified') + ' desc'
TypeError: _format() takes exactly 1 argument (2 given)
```

We're using the latest frappe 11.0.3-beta.21.


Discussion:
https://github.com/frappe/frappe/blob/develop/frappe/core/doctype/report/report.py#L152
Found another issue here: https://github.com/frappe/frappe/blob/develop/frappe/core/doctype/report/report.py#L128

When I create a report using the report builder, the JSON contains the report columns as `fields` but in `report.py` it is expected to be `columns`. 
```json
{"fields": [["name", "Timesheet"], ["status", "Timesheet"], ["hours", "Timesheet Detail"], ["notes", "Timesheet Detail"], ["docstatus", "Timesheet"]], "add_totals_row": true, "order_by": "`tabTimesheet`.`modified` desc", "add_total_row": 1, "filters": [["Timesheet", "status", "!=", "Billed", false], ["Timesheet", "status", "not in", ["Cancelled", null], false], ["Timesheet", "customer", "=", "XXXXXX"]]}
```
Another one here: https://github.com/frappe/frappe/blob/a7de780715514e9b6082f373288a1d5bc63fac00/frappe/templates/emails/auto_email_report.html#L4

If no columns are specified (as is if you are using type "CSV") this will raise the following error:
```jinja
{% set max_width = '100%' if columns|length > 3 else '600px' %}
```
```
File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/./templates/emails/auto_email_report.html", line 4, in top-level template code
    {% set max_width = '100%' if columns|length > 3 else '600px' %}
TypeError: object of type 'NoneType' has no len()
```


Resolving patch:
diff --git a/frappe/core/doctype/report/report.py b/frappe/core/doctype/report/report.py
--- a/frappe/core/doctype/report/report.py
+++ b/frappe/core/doctype/report/report.py
@@ -127,6 +127,8 @@ def get_data(self, filters=None, limit=None, user=None, as_dict=False):
 
 			if params.get('columns'):
 				columns = params.get('columns')
+			elif params.get('fields'):
+				columns = params.get('fields')
 			else:
 				columns = [['name', self.ref_doctype]]
 				for df in frappe.get_meta(self.ref_doctype).fields:
@@ -149,7 +151,7 @@ def _format(parts):
 			if params.get('sort_by'):
 				order_by = _format(params.get('sort_by').split('.')) + ' ' + params.get('sort_order')
 			else:
-				order_by = _format(self.ref_doctype, 'modified') + ' desc'
+				order_by = _format([self.ref_doctype, 'modified']) + ' desc'
 
 			if params.get('sort_by_next'):
 				order_by += ', ' + _format(params.get('sort_by_next').split('.')) + ' ' + params.get('sort_order_next')


Test patch: