django-ptrack icon indicating copy to clipboard operation
django-ptrack copied to clipboard

handle uuid via json encoder

Open thenewguy opened this issue 3 years ago • 0 comments

It would be helpful if the default encoder used the DjangoJsonEncoder class for the JSON conversion to automatically handle types like uuid.

This is what happens when you pass a UUID pk through ptrack:

   File "/home/vagrant/.tox/py/lib/python3.7/site-packages/ptrack/templatetags/ptrack.py", line 20, in ptrack
     encoded_dict = {'ptrack_encoded_data': ptrack_encoder.encrypt(*args, **kwargs)}
   File "/home/vagrant/.tox/py/lib/python3.7/site-packages/ptrack/encoder.py", line 46, in encrypt
     data = json.dumps((args, kwargs))
   File "/usr/local/lib/python3.7/json/__init__.py", line 231, in dumps
     return _default_encoder.encode(obj)
   File "/usr/local/lib/python3.7/json/encoder.py", line 199, in encode
     chunks = self.iterencode(o, _one_shot=True)
   File "/usr/local/lib/python3.7/json/encoder.py", line 257, in iterencode
     return _iterencode(o, 0)
   File "/usr/local/lib/python3.7/json/encoder.py", line 179, in default
     raise TypeError(f'Object of type {o.__class__.__name__} '
 TypeError: Object of type UUID is not JSON serializable

It would be a small change to this line: https://github.com/indeedeng/django-ptrack/blob/c3b164b9d1860325cdab4ee5bc9b689d0f5ea3ff/ptrack/encoder.py#L46

thenewguy avatar May 29 '21 21:05 thenewguy