Permalink
Browse files

Bumped to v0.7.7

Accessor now handles '' by returning the original object. i.e. Accessor('').resolve(obj) == obj
get_FOO_display() methods are now used if available -- issue #30
Made a couple of very minor code documentation changes.
  • Loading branch information...
1 parent 342a717 commit 7a4bd5ac9158ea5293a8780359b06db0e034faca @bradleyayers committed Sep 18, 2011
Showing with 31 additions and 5 deletions.
  1. +23 −2 django_tables2/rows.py
  2. +2 −0 django_tables2/utils.py
  3. +1 −1 docs/conf.py
  4. +1 −1 setup.py
  5. +4 −1 tests/utils.py
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
from itertools import imap, ifilter
import inspect
+from django.db import models
+from django.db.models.fields import FieldDoesNotExist
from django.utils.safestring import EscapeUnicode, SafeData
from django.utils.functional import curry
+from .utils import A
class BoundRow(object):
@@ -106,13 +109,31 @@ def __getitem__(self, name):
def value():
try:
- raw = bound_column.accessor.resolve(self.record)
+ # We need to take special care here to allow get_FOO_display()
+ # methods on a model to be used if available. See issue #30.
+ path, _, remainder = bound_column.accessor.rpartition('.')
+ penultimate = A(path).resolve(self.record)
+ # If the penultimate is a model and the remainder is a field
+ # using choices, use get_FOO_display().
+ if isinstance(penultimate, models.Model):
+ try:
+ field = penultimate._meta.get_field(remainder)
+ display = getattr(penultimate, 'get_%s_display' % remainder, None)
+ if field.choices and display:
+ raw = display()
+ remainder = None
+ except FieldDoesNotExist:
+ pass
+ # Fall back to just using the original accessor (we just need
+ # to follow the remainder).
+ if remainder:
+ raw = A(remainder).resolve(penultimate)
except (TypeError, AttributeError, KeyError, ValueError):
raw = None
return raw if raw is not None else bound_column.default
kwargs = {
- 'value': value, # already a function
+ 'value': value, # already a function, no need to wrap
'record': lambda: self.record,
'column': lambda: bound_column.column,
'bound_column': lambda: bound_column,
@@ -247,6 +247,8 @@ def resolve(self, context):
@property
def bits(self):
+ if self == '':
+ return ()
return self.split(self.SEPARATOR)
View
@@ -56,7 +56,7 @@
# The short X.Y version.
version = '0.7'
# The full version, including alpha/beta/rc tags.
-release = '0.7.6'
+release = '0.7.7'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
View
@@ -4,7 +4,7 @@
setup(
name='django-tables2',
- version='0.7.6',
+ version='0.7.7',
description='Table framework for Django',
author='Bradley Ayers',
View
@@ -39,7 +39,7 @@ def orderby():
def accessor():
x = Accessor('0')
Assert('B') == x.resolve('Brad')
-
+
x = Accessor('1')
Assert('r') == x.resolve('Brad')
@@ -48,3 +48,6 @@ def accessor():
x = Accessor('2.upper.__len__')
Assert(1) == x.resolve('Brad')
+
+ x = Accessor('')
+ Assert('Brad') == x.resolve('Brad')

0 comments on commit 7a4bd5a

Please sign in to comment.