Self hosted FLOSS fitness/workout, nutrition and weight tracker
Use a custom encoder for JSON
Roland Geider5473d5b24edb
diff --git a/manager/models.py b/manager/models.pyindex b03f7f064..56268f7c2 100644--- a/manager/models.py+++ b/manager/models.py@@ -29,6 +29,8 @@ from django.core.urlresolvers import reversefrom exercises.models import Exercise+from workout_manager.helpers import DecimalJsonEncoder+logger = logging.getLogger('workout_manager.custom')@@ -221,7 +223,7 @@ class WorkoutLog(models.Model):temp[rep] = 0chart_data.append(temp)- return (entry_log, json.dumps(chart_data))+ return (entry_log, json.dumps(chart_data, cls=DecimalJsonEncoder))class UserProfile(models.Model):diff --git a/workout_manager/helpers.py b/workout_manager/helpers.pyindex 24c01aa3a..dded90ec0 100644--- a/workout_manager/helpers.py+++ b/workout_manager/helpers.py@@ -14,8 +14,10 @@## You should have received a copy of the GNU Affero General Public License-from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger+import decimal+import json+from django.core.paginator import Paginator, EmptyPage, PageNotAnIntegerdef pagination(object_list,request_page,@@ -66,3 +68,18 @@ def pagination(object_list,# OK, returnreturn({'page': paginated_page,'page_range': page_range})+++class DecimalJsonEncoder(json.JSONEncoder):+ '''+ Custom JSON encoder.++ This class is needed because we store some data as a decimal (e.g. the+ individual weight entries in the workout log) and they need to be+ processed, json.dumps() doesn't work on them+ '''+ def _iterencode(self, o, markers=None):+ if isinstance(o, decimal.Decimal):+ return (str(o) for o in [o])++ return super(DecimalJsonEncoder, self)._iterencode(o, markers)