Skip to content

Commit

Permalink
* Fixed ReportGroup.attribute_name to support methods and child attri…
Browse files Browse the repository at this point in the history
…butes

* Created a new module 'utils.py' with once function to get attribute values
  from objects support methods and expression paths. It is used by
  ReportGroup.attribute_name and Widget.attribute_name
  • Loading branch information
marinho committed May 14, 2009
1 parent 10cd7b3 commit b302f48
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2009-05-14: Version 0.3.0-final
-------------------------------
* Fixed ReportGroup.attribute_name to support methods and child attributes
* Created a new module 'utils.py' with once function to get attribute values
from objects support methods and expression paths. It is used by
ReportGroup.attribute_name and Widget.attribute_name

2009-04-30: Version 0.3.0-alpha-9
---------------------------------
* Fixed a bug on group band footer calculations when there was only one object
Expand Down
2 changes: 1 addition & 1 deletion geraldo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- tests - a package with automated doc tests.
"""

VERSION = (0, 3, 0, 'alpha-9')
VERSION = (0, 3, 0, 'final')

def get_version():
return '%d.%d.%d-%s'%VERSION
Expand Down
10 changes: 0 additions & 10 deletions geraldo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
def landscape(page_size):
return page_size[1], page_size[0]

def get_attr_value(obj, attr):
"""This function returns a value from a object doesn't matters if the
attribute is a function or not"""
value = getattr(obj, attr)

if type(value) == types.MethodType:
return value()

return value

class BaseReport(object):
"""Basic Report class, inherited and used to make reports adn subreports"""

Expand Down
2 changes: 1 addition & 1 deletion geraldo/generators/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from reportlab.platypus import Paragraph, KeepInFrame
from reportlab.lib.units import cm

from geraldo.base import get_attr_value
from geraldo.utils import get_attr_value
from geraldo.widgets import Widget, Label, SystemField
from geraldo.graphics import Graphic, RoundRect, Rect, Line, Circle, Arc,\
Ellipse, Image
Expand Down
24 changes: 24 additions & 0 deletions geraldo/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def get_attr_value(obj, attr_path):
"""This function gets an attribute value from an object. If the attribute
is a method with no arguments (or arguments with default values) it calls
the method. If the expression string has a path to a child attribute, it
supports.
Examples:
attribute_name = 'name'
attribute_name = 'name.upper'
attribute_name = 'customer.name.lower'
"""
parts = attr_path.split('.')

val = getattr(obj, parts[0])

if len(parts) > 1:
val = get_attr_value(val, '.'.join(parts[1:]))

if callable(val):
val = val()

return val

24 changes: 2 additions & 22 deletions geraldo/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from reportlab.lib.colors import black

from base import BAND_WIDTH, BAND_HEIGHT, Element
from utils import get_attr_value

class Widget(Element):
"""A widget is a value representation on the report"""
Expand Down Expand Up @@ -79,35 +80,14 @@ class ObjectValue(Label):
display_format = '%s'
objects = None

def _get_part_value(self, obj, attr_path):
"""This method exists to make possible the use of an expression in
"attribute_name" attribute.
Examples:
attribute_name = 'name.upper'
attribute_name = 'customer.name'
"""
parts = attr_path.split('.')

val = getattr(obj, parts[0])

if len(parts) > 1:
val = self._get_part_value(val, '.'.join(parts[1:]))

if callable(val):
val = val()

return val

def get_object_value(self, instance=None):
"""Return the attribute value for just an object"""
instance = instance or self.instance

if self.get_value and instance:
return self.get_value(instance)

value = self._get_part_value(instance, self.attribute_name)
value = get_attr_value(instance, self.attribute_name)

# For method attributes
if type(value) == types.MethodType:
Expand Down

0 comments on commit b302f48

Please sign in to comment.