Skip to content

Commit

Permalink
updated example, moved datesql report into base
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolajbaer committed Jun 21, 2010
1 parent 71b7e55 commit 170997c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
10 changes: 7 additions & 3 deletions example/example_reports/reports.py
@@ -1,7 +1,7 @@
import reportengine
from django.contrib.auth.models import User
from reportengine.filtercontrols import StartsWithFilterControl
from outputformats import *
from reportengine.outputformats import *

class UserReport(reportengine.ModelReport):
"""An example of a model report"""
Expand Down Expand Up @@ -51,17 +51,21 @@ def get_rows(self,filters={},order_by=None):

reportengine.register(AppsReport)

class AdminActivityReport(reportengine.SQLReport):
class AdminActivityReport(reportengine.DateSQLReport):
row_sql="""select username,user_id,count(*),min(action_time),max(action_time)
from django_admin_log
inner join auth_user on auth_user.id = django_admin_log.user_id
where is_staff = 1
and action_time >= '%(date__gte)s'
and action_time < '%(date__lt)s'
group by user_id;
"""
aggregate_sql="""select avg(count) as average,max(count) as max,min(count) as min
from (
select count(user_id) as count
from django_admin_log
from django_admin_log
where action_time >= '%(date__gte)s'
and action_time < '%(date__lt)s'
group by user_id
)"""
# TODO adding parameters to the sql report is.. hard.
Expand Down
1 change: 1 addition & 0 deletions example/settings.py
Expand Up @@ -49,4 +49,5 @@
'django.contrib.sites',
'django.contrib.admin',
'reportengine',
'example_reports',
)
2 changes: 1 addition & 1 deletion reportengine/__init__.py
@@ -1,6 +1,6 @@
'''Duplicated from django-reporting by vitalik, but with my own twist -nb '''
import imp
from base import Report,ModelReport,QuerySetReport,SQLReport
from base import Report,ModelReport,QuerySetReport,SQLReport,DateSQLReport

# TODO make this seperate from vitalik's registry methods
_registry = {}
Expand Down
13 changes: 12 additions & 1 deletion reportengine/base.py
Expand Up @@ -7,6 +7,7 @@
from django.db.models.fields import FieldDoesNotExist
from filtercontrols import *
from outputformats import *
import datetime

# Pulled from vitalik's Django-reporting
def get_model_field(model, name):
Expand All @@ -24,6 +25,8 @@ def get_lookup_field(model, original, lookup):

class Report(object):
verbose_name="Abstract Report"
namespace = "Default"
slug ="base"
labels = None
per_page=100
can_show_all=True
Expand Down Expand Up @@ -125,5 +128,13 @@ def get_rows(self,filters={},order_by=None):

return rows,agg

# TODO build AnnotatedReport that deals with .annotate functions in ORM
class DateSQLReport(SQLReport):
aggregate_sql=None
query_params=[("date","Date","datetime")]
date_field="date"
default_mask={
"date__gte":lambda: (datetime.datetime.today() -datetime.timedelta(days=30)).strftime("%Y-%m-%d"),
"date__lt":lambda: (datetime.datetime.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d"),
}

# TODO build AnnotatedReport that deals with .annotate functions in ORM

0 comments on commit 170997c

Please sign in to comment.