Skip to content

Commit

Permalink
Fixed #1. A function is now available to convert a summary into a jso…
Browse files Browse the repository at this point in the history
…n encoded string. This was preferred over a method to keep things nicely separated and to avoid name clashes.
  • Loading branch information
willhardy committed Oct 13, 2011
1 parent 6ccff40 commit 172eed5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rollyourown/commerce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
__authors__ = ["Will Hardy <rollyourown@willhardy.com.au>"]
__all__ = ( 'Summary', 'Extra', 'Items', 'Total',)
__all__ = ( 'Summary', 'Extra', 'Items', 'Total', 'json_summary')

from summary import Summary, Extra, Items, Total
from utils import json_summary
22 changes: 21 additions & 1 deletion rollyourown/commerce/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
from rollyourown.commerce.utils.friendly_id import FriendlyID
from rollyourown.commerce.utils.formatting import FormattedDecimal

__all__ = ('FriendlyID', 'FormattedDecimal')
__all__ = ('FriendlyID', 'FormattedDecimal', 'json_summary')

from django.utils import simplejson
from django.core.serializers.json import DjangoJSONEncoder

def json_summary(summary, fields=None):
""" serialize the given summary to JSON. """

data = {}
for items in summary._meta.items.keys():
if fields and items in fields:
data[items] = dict((i.pk, getattr(i, summary._meta.items[items].cache_amount_as)) for i in getattr(summary, items))
for extra in summary._meta.extras:
if fields and extra in fields:
data[extra] = getattr(summary, extra).amount
for total in summary._meta.totals:
if fields and total in fields:
data[total] = getattr(summary, total)

return simplejson.dumps(data, cls=DjangoJSONEncoder)

0 comments on commit 172eed5

Please sign in to comment.