diff --git a/example/example_reports/reports.py b/example/example_reports/reports.py index a91e538..c308698 100644 --- a/example/example_reports/reports.py +++ b/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""" @@ -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. diff --git a/example/settings.py b/example/settings.py index 3639254..91b27f5 100644 --- a/example/settings.py +++ b/example/settings.py @@ -49,4 +49,5 @@ 'django.contrib.sites', 'django.contrib.admin', 'reportengine', + 'example_reports', ) diff --git a/reportengine/__init__.py b/reportengine/__init__.py index 93a2dd3..c307778 100644 --- a/reportengine/__init__.py +++ b/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 = {} diff --git a/reportengine/base.py b/reportengine/base.py index b5ff942..fb72ee8 100644 --- a/reportengine/base.py +++ b/reportengine/base.py @@ -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): @@ -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 @@ -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