-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #1. A function is now available to convert a summary into a jso…
…n encoded string. This was preferred over a method to keep things nicely separated and to avoid name clashes.
- Loading branch information
Showing
2 changed files
with
23 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|