Skip to content

Commit

Permalink
Bumped to v0.7.7
Browse files Browse the repository at this point in the history
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
bradleyayers committed Sep 18, 2011
1 parent 342a717 commit 7a4bd5a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
25 changes: 23 additions & 2 deletions django_tables2/rows.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):
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions django_tables2/utils.py
Expand Up @@ -247,6 +247,8 @@ def resolve(self, context):

@property
def bits(self):
if self == '':
return ()
return self.split(self.SEPARATOR)


Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -4,7 +4,7 @@

setup(
name='django-tables2',
version='0.7.6',
version='0.7.7',
description='Table framework for Django',

author='Bradley Ayers',
Expand Down
5 changes: 4 additions & 1 deletion tests/utils.py
Expand Up @@ -39,7 +39,7 @@ def orderby():
def accessor():
x = Accessor('0')
Assert('B') == x.resolve('Brad')

x = Accessor('1')
Assert('r') == x.resolve('Brad')

Expand All @@ -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.