From 828075ae23312abefd423d2b8b56d0e2b58b55ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 18 Jan 2021 11:25:32 +0100 Subject: [PATCH] misc: add variant deserialization method for dates/phones --- src/authentic2_auth_fedict/__init__.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/authentic2_auth_fedict/__init__.py b/src/authentic2_auth_fedict/__init__.py index bb0fe58..d20ce62 100644 --- a/src/authentic2_auth_fedict/__init__.py +++ b/src/authentic2_auth_fedict/__init__.py @@ -74,6 +74,15 @@ class Plugin(object): def attribute_kinds(self): from . import fields + def attribute_json_loads(x): + if not x: + return x + try: + return json.loads(x) + except json.JSONDecodeError: + # "compatibility" with native date/phone kinds + return x + return [ { 'label': _('National Register Number'), @@ -85,14 +94,14 @@ class Plugin(object): { 'label': _('Date'), 'serialize': json.dumps, - 'deserialize': json.loads, + 'deserialize': attribute_json_loads, 'name': 'date', 'field_class': fields.DateField, }, { 'label': _('Date'), 'serialize': json.dumps, - 'deserialize': json.loads, + 'deserialize': attribute_json_loads, 'name': 'fedict_date', 'field_class': fields.DateField, }, @@ -113,14 +122,14 @@ class Plugin(object): { 'label': _('Phone number'), 'serialize': json.dumps, - 'deserialize': json.loads, + 'deserialize': attribute_json_loads, 'name': 'phone', 'field_class': fields.NumPhoneField, }, { 'label': _('Phone number'), 'serialize': json.dumps, - 'deserialize': json.loads, + 'deserialize': attribute_json_loads, 'name': 'fedict_phone', 'field_class': fields.NumPhoneField, },