Merge pull request #7313 from netchampfaris/dynamic-filters-auto-email

feat: Dynamic Date filters in Auto Email Report
This commit is contained in:
Rushabh Mehta 2019-04-20 14:33:30 +05:30 committed by GitHub
commit ce5e0b6453
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 971 additions and 733 deletions

View file

@ -99,6 +99,14 @@ frappe.ui.form.on('Auto Email Report', {
dialog.show();
dialog.set_values(filters);
})
// populate dynamic date field selection
let date_fields = report_filters
.filter(df => df.fieldtype === 'Date')
.map(df => ({ label: df.label, value: df.fieldname }));
frm.set_df_property('from_date_field', 'options', date_fields);
frm.set_df_property('to_date_field', 'options', date_fields);
frm.toggle_display('dynamic_report_filters_section', date_fields.length > 0);
}
}
});

File diff suppressed because it is too large Load diff

View file

@ -5,14 +5,13 @@
from __future__ import unicode_literals
import calendar
import json
from datetime import timedelta
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import (format_time, get_link_to_form, get_url_to_report,
global_date_format, now, now_datetime, validate_email_address)
global_date_format, now, now_datetime, validate_email_address, today, add_to_date)
from frappe.utils.csvutils import to_csv
from frappe.utils.xlsxutils import make_xlsx
@ -58,10 +57,14 @@ class AutoEmailReport(Document):
'''Returns file in for the report in given format'''
report = frappe.get_doc('Report', self.report)
self.filters = frappe.parse_json(self.filters) if self.filters else {}
if self.report_type=='Report Builder' and self.data_modified_till:
self.filters = json.loads(self.filters) if self.filters else {}
self.filters['modified'] = ('>', now_datetime() - timedelta(hours=self.data_modified_till))
if self.report_type != 'Report Builder' and self.dynamic_date_filters_set():
self.prepare_dynamic_filters()
columns, data = report.get_data(limit=self.no_of_rows or 100, user = self.user,
filters = self.filters, as_dict=True)
@ -121,6 +124,24 @@ class AutoEmailReport(Document):
def get_file_name(self):
return "{0}.{1}".format(self.report.replace(" ", "-").replace("/", "-"), self.format.lower())
def prepare_dynamic_filters(self):
self.filters = frappe.parse_json(self.filters)
to_date = today()
from_date_value = {
'Daily': ('days', -1),
'Weekly': ('weeks', -1),
'Monthly': ('months', -1),
'Quarterly': ('months', -3),
'Half Yearly': ('months', -6),
'Yearly': ('years', -1)
}[self.dynamic_date_period]
from_date = add_to_date(to_date, **{from_date_value[0]: from_date_value[1]})
self.filters[self.from_date_field] = from_date
self.filters[self.to_date_field] = to_date
def send(self):
if self.filter_meta and not self.filters:
frappe.throw(_("Please set filters value in Report Filter table."))
@ -150,6 +171,9 @@ class AutoEmailReport(Document):
reference_name = self.name
)
def dynamic_date_filters_set(self):
return self.dynamic_date_period and self.from_date_field and self.to_date_field
@frappe.whitelist()
def download(name):
'''Download report locally'''

View file

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import frappe
import unittest, json
from frappe.utils import get_link_to_form
from frappe.utils import get_link_to_form, today, add_to_date
# test_records = frappe.get_test_records('Auto Email Report')
@ -13,17 +13,7 @@ class TestAutoEmailReport(unittest.TestCase):
def test_auto_email(self):
frappe.delete_doc('Auto Email Report', 'Permitted Documents For User')
auto_email_report = frappe.get_doc(dict(
doctype='Auto Email Report',
report='Permitted Documents For User',
report_type='Script Report',
user='Administrator',
enabled=1,
email_to='test@example.com',
format='HTML',
frequency='Daily',
filters=json.dumps(dict(user='Administrator', doctype='DocType'))
)).insert()
auto_email_report = get_auto_email_report()
data = auto_email_report.get_report_content()
@ -38,3 +28,34 @@ class TestAutoEmailReport(unittest.TestCase):
data = auto_email_report.get_report_content()
def test_dynamic_date_filters(self):
auto_email_report = get_auto_email_report()
auto_email_report.dynamic_date_period = 'Weekly'
auto_email_report.from_date_field = 'from_date'
auto_email_report.to_date_field = 'to_date'
auto_email_report.prepare_dynamic_filters()
self.assertEqual(auto_email_report.filters['from_date'], add_to_date(today(), weeks=-1))
self.assertEqual(auto_email_report.filters['to_date'], today())
def get_auto_email_report():
if not frappe.db.exists('Auto Email Report', 'Permitted Documents For User'):
auto_email_report = frappe.get_doc(dict(
doctype='Auto Email Report',
report='Permitted Documents For User',
report_type='Script Report',
user='Administrator',
enabled=1,
email_to='test@example.com',
format='HTML',
frequency='Daily',
filters=json.dumps(dict(user='Administrator', doctype='DocType'))
)).insert()
else:
auto_email_report = frappe.get_doc('Auto Email Report', 'Permitted Documents For User')
return auto_email_report