Skip to content

Commit

Permalink
Removed Django dependency and created a method for date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinho Brandão committed Jan 22, 2009
1 parent a8f4074 commit bf3a2e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
13 changes: 12 additions & 1 deletion geraldo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from reportlab.lib.pagesizes import A4
from reportlab.lib.colors import black

REPORT_PAGE_BREAK = 'report-page-break'
try:
from django.template.defaultfilters import date as format_date
except ImportError:
format_date = None

def landscape(page_size):
return page_size[1], page_size[0]
Expand Down Expand Up @@ -59,6 +62,14 @@ def get_objects_list(self):

return [object for object in self.queryset]

def format_date(self, date, expression):
"""Use a Django template filter or a manually declared function to
return formatted datetime"""
if format_date:
return format_date(date, expression)

raise Exception('If Django is not found, you must declare your own date formatting method in your Report class')

class Report(BaseReport):
"""Class to be inherited and used to make reports"""
# Report properties
Expand Down
10 changes: 5 additions & 5 deletions geraldo/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from reportlab.lib.units import cm
from reportlab.lib.colors import black

from django.template.defaultfilters import date

BAND_WIDTH = 'band-width'

class Widget(object):
Expand Down Expand Up @@ -186,17 +184,19 @@ def text(self):
if self.get_value:
return self.get_value(self.expression, fields)

return self.expression%SystemFieldDict(fields)
return self.expression%SystemFieldDict(self, fields)

class SystemFieldDict(dict):
widget = None
fields = None

def __init__(self, fields):
def __init__(self, widget, fields):
self.widget = widget
self.fields = fields or {}

def __getitem__(self, key):
if key.startswith('now:'):
return date(
return self.widget.report.format_date(
self.fields.get('current_datetime', datetime.datetime.now()),
key[4:]
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# django-geraldo setup
# Geraldo setup

from distutils.core import setup
from geraldo import get_version
Expand Down

0 comments on commit bf3a2e7

Please sign in to comment.