toconnell / kdm-manager

An interactive campaign manager for the game "Monster", by Kingdom Death. Development blog and release notes at https://blog.kdm-manager.com This project has no affiliation with Kingdom Death and is a totally independent, fan-maintained project.
http://kdm-manager.com
Other
26 stars 11 forks source link

API: log_event() throws an error on upper ASCII names #490

Closed toconnell closed 6 years ago

toconnell commented 6 years ago

User OID: 585287dc8740d97c8a639bd4 Method: POST URL: https://192.168.0.110:8013/new/survivor JSON: {u'father': u'5aad7cfa8740d9190b6d38f2', u'sex': u'F', u'public': False, u'primary_donor_parent': u'father', u'mother': u'5aad50638740d9190b6d3627', u'settlement': u'5a31a4aa8740d96dac139971', u'email': u'REDACTED'}

Traceback (most recent call last):
File "/home/toconnell/kdm-manager/v2/api/utils.py", line 539, in wrapper
 return log_event_call(self, *args, **kwargs)
File "/home/toconnell/kdm-manager/v2/api/Models.py", line 921, in log_event
 d['action']['repr'] = " ".join([action_word, "'%s'" % value, action_preposition, str(key)])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
toconnell commented 6 years ago

Just assuming that we've got ASCII incoming on everything from now on and doing some preemptive encode() calls to fix this:

@@ -831,10 +832,14 @@ class UserAsset(object):
         #   baseline attributes
         #

+        # for those who still raw-dog it; force to ASCII:
+        if msg is not None:
+            msg = msg.encode("ascii",'ignore')
+

         # 1.) event: determine event type if it's None
         if event_type is None:
@@ -848,10 +853,12 @@ class UserAsset(object):
         # 3.) key: default the key if we don't get one
         if key is None:
             key = " ".join(method.split("_")[1:])
+        key = key.encode('ascii','ignore')

         # 4.) value; default the value if we don't get one
         if value is None:
             value = "UNKNOWN"
+        value = value.encode('ascii','ignore')