Skip to content

Commit

Permalink
Changed SQLReports to use row_sql instead of rows_sql and updated uni…
Browse files Browse the repository at this point in the history
…t tests.
  • Loading branch information
kmooney committed Apr 3, 2013
1 parent b02af94 commit d06a9a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions reportengine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class SQLReport(Report):
"""
A subclass of Report, used with raw SQL.
"""
rows_sql=None # sql statement with named parameters in python syntax (e.g. "%(age)s" )
row_sql=None # sql statement with named parameters in python syntax (e.g. "%(age)s" )
aggregate_sql=None # sql statement that brings in aggregates. pulls from column name and value for first row only
query_params=[] # list of tuples, (name,label,datatype) where datatype is a mapping to a registerd filtercontrol

Expand Down Expand Up @@ -251,10 +251,10 @@ def get_row_sql(self, filters, order_by):
:param filters: A dictionary of filters to apply to this sql.
:param order_by: This is ignored, but may be used by subclasses.
:return: The text-replaced SQL, or none if self.rows_sql doesn't exist.
:return: The text-replaced SQL, or none if self.row_sql doesn't exist.
"""
if self.rows_sql:
return self.rows_sql % filters
if self.row_sql:
return self.row_sql % filters
return None

def get_aggregate_sql(self, filters):
Expand Down
8 changes: 6 additions & 2 deletions tests/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from reportengine.filtercontrols import StartsWithFilterControl
from models import Customer, SaleItem


class CustomerReport(base.ModelReport):
"""An example of a model report"""
verbose_name = "User Report"
Expand All @@ -17,6 +18,7 @@ class CustomerReport(base.ModelReport):

register(CustomerReport)


class CustomerSalesReport(base.SQLReport):
"""A SQL Report to show sales by customer"""

Expand All @@ -30,7 +32,7 @@ class CustomerSalesReport(base.SQLReport):

list_filter=['first_name', 'last_name']

rows_sql = """
row_sql = """
SELECT first_name, last_name, SUM(total) as total FROM tests_sale
INNER JOIN tests_customer ON tests_sale.customer_id = tests_customer.id
WHERE first_name = '%(first_name)s' AND last_name = '%(last_name)s'
Expand All @@ -39,6 +41,7 @@ class CustomerSalesReport(base.SQLReport):
"""
register(CustomerSalesReport)


class SaleItemReport(base.QuerySetReport):
verbose_name = "Sales Items, filtered by customer"

Expand All @@ -51,6 +54,7 @@ class SaleItemReport(base.QuerySetReport):
def get_queryset(self, *args, **kwargs):
return SaleItem.objects.all()


class CustomerByStamp(base.DateSQLReport):
"""A Date SQL report to show customers by timestamp"""

Expand All @@ -60,7 +64,7 @@ class CustomerByStamp(base.DateSQLReport):

labels = ('first_name', 'last_name', 'stamp')

rows_sql = """
row_sql = """
SELECT first_name, last_name, stamp FROM tests_customer
WHERE stamp < '%(date__lt)s' AND stamp >= '%(date__gte)s';
"""
Expand Down

0 comments on commit d06a9a9

Please sign in to comment.