Skip to content

Commit

Permalink
adds support to dotted syntax to the 'fields' parameter of export_as_...
Browse files Browse the repository at this point in the history
it's now possible ti use export_as_csv(fields=['obj.fk.field'.....
  • Loading branch information
saxix committed Sep 11, 2013
1 parent 2370f9b commit 3645099
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
7 changes: 7 additions & 0 deletions adminactions/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from .graph import TestGraph # NOQA
from .api import TestExportAsCsv, TestExportAsExcel, TestExportQuerySetAsCsv, TestExportQuerySetAsExcel

import warnings
warnings.filterwarnings("ignore",
append=True,
category=DeprecationWarning,
message=".*AUTH_PROFILE_MODULE.*")


if getattr(settings, 'ENABLE_SELENIUM', True):
try:
import selenium # NOQA
Expand Down
20 changes: 16 additions & 4 deletions adminactions/tests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,34 @@ class TestExportQuerySetAsCsv(TestCase):
def test_queryset_values(self):
fields = ['codename', 'content_type__app_label']
header = ['Name', 'Application']
qs = Permission.objects.filter(codename='add_user').values('codename', 'content_type__app_label')
mem = StringIO.StringIO()
export_as_csv(queryset=qs, fields=fields, header=header, out=mem)
with self.assertNumQueries(1):
qs = Permission.objects.filter(codename='add_user').values('codename', 'content_type__app_label')
export_as_csv(queryset=qs, fields=fields, header=header, out=mem)
mem.seek(0)
csv_dump = mem.read()
self.assertEquals(csv_dump.decode('utf8'), u'"Name";"Application"\r\n"add_user";"auth"\r\n')

def test_callable_method(self):
fields = ['codename', 'natural_key']
qs = Permission.objects.filter(codename='add_user')
mem = StringIO.StringIO()
export_as_csv(queryset=qs, fields=fields, out=mem)
with self.assertNumQueries(2):
qs = Permission.objects.filter(codename='add_user')
export_as_csv(queryset=qs, fields=fields, out=mem)
mem.seek(0)
csv_dump = mem.read()
self.assertEquals(csv_dump.decode('utf8'), u'"add_user";"(u\'add_user\', u\'auth\', u\'user\')"\r\n')

def test_deep_attr(self):
fields = ['codename', 'content_type.app_label']
mem = StringIO.StringIO()
with self.assertNumQueries(1):
qs = Permission.objects.select_related().filter(codename='add_user')
export_as_csv(queryset=qs, fields=fields, out=mem)
mem.seek(0)
csv_dump = mem.read()
self.assertEquals(csv_dump.decode('utf8'), u'"add_user";"auth"\r\n')


class TestExportAsCsv(unittest.TestCase):
def test_export_as_csv(self):
Expand Down
1 change: 1 addition & 0 deletions adminactions/tests/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def assert_profile(user):
p = None
try:
user.get_profile()
#warnings.filters.pop()
except ObjectDoesNotExist:
app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
model = models.get_model(app_label, model_name)
Expand Down
31 changes: 28 additions & 3 deletions adminactions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,40 @@ def get_copy_of_instance(instance):
return instance.__class__.objects.get(pk=instance.pk)


def get_attr(obj, attr, default=None):
"""Recursive get object's attribute. May use dot notation.
>>> class C(object): pass
>>> a = C()
>>> a.b = C()
>>> a.b.c = 4
>>> get_attr(a, 'b.c')
4
>>> get_attr(a, 'b.c.y', None)
>>> get_attr(a, 'b.c.y', 1)
1
"""
if '.' not in attr:
ret = getattr(obj, attr, default)
else:
L = attr.split('.')
ret = get_attr(getattr(obj, L[0], default), '.'.join(L[1:]), default)

if isinstance(ret, BaseException):
raise ret
return ret

def getattr_or_item(obj, name):
try:
return getattr(obj, name)
ret = get_attr(obj, name, AttributeError())
except AttributeError:
try:
return obj[name]
ret = obj[name]
except KeyError:
raise AttributeError("%s object has no attribute/item '%s'" % (obj.__class__.__name__, name))

return ret

def get_field_value(obj, field, usedisplay=True, raw_callable=False):
"""
Expand Down

0 comments on commit 3645099

Please sign in to comment.