Django-Reporter
Categories
Skills
http://github.com/bkonkle/django-reporter
April, 2010 - Present
At Pegasus we send daily, weekly, and monthly email reports out to several people. We have a quite complex codebase, so we need these reports to be as flexible as possible. Previously, we were creating one-off executable report scripts and collecting them in a directory on the main web server to be hit by cron periodically. This involved a lot of boilerplate code and duplication, and became difficult to manage long-term - especially when we switched from a single-site to a multi-site structure and a lot of the reports broke.
My solution was to make the reports class-based, so that we could implement certain methods on each report that handled what makes those reports different while letting the base class handle what stays the same. We subclass the BaseReport class, implement methods for the subject line and default recipients, and then implement a method that pulls our data and sets it up in a nested list for a CSV writer to take care of.
To make the reports easy to run, I made a management command that would handle the invocation of the reports. It takes the desired report frequency, optional date and recipient overrides, and establishes the report class with that information. The default behavior is to take the data, convert it to csv, and email it to the desired recipients. The command also provides options to save the report to a file, or print it to stdout.