kopia lustrzana https://gitlab.com/marnanel/chapeau
Don't json-encode values twice.
_normalise_type_for_thing moved from create to Thing. create doesn't worry about what kind of fields it's asking Thing to set. Thing's setitem adds audiences correctly, though this requires it to save() as a side-effect if this is the first creation.2019-08-17
rodzic
87f015eea5
commit
44aeab91d1
|
@ -39,8 +39,6 @@ def create(
|
|||
str(k))
|
||||
continue
|
||||
|
||||
value[k] = _normalise_type_for_thing(v)
|
||||
|
||||
if 'type' not in value:
|
||||
logger.warn("Things must have a type; dropping message")
|
||||
return
|
||||
|
@ -80,6 +78,8 @@ def create(
|
|||
|
||||
# XXX get the record from "types", and see what fields we need
|
||||
|
||||
########################
|
||||
|
||||
try:
|
||||
need_actor, need_object, need_target = TYPES[value['type']]
|
||||
except KeyError:
|
||||
|
@ -127,6 +127,8 @@ def create(
|
|||
logger.warn(message)
|
||||
raise ValueError(message)
|
||||
|
||||
########################
|
||||
|
||||
# Right, we need to create an object.
|
||||
|
||||
result = cls()
|
||||
|
@ -135,16 +137,9 @@ def create(
|
|||
result.remote_url = value['id']
|
||||
del value['id']
|
||||
|
||||
for f,v in value.copy().items():
|
||||
if hasattr(result, 'f_'+f):
|
||||
# result has a specialised field for this
|
||||
logger.debug(' %s is a specialised field', f)
|
||||
setattr(result, 'f_'+f, json.dumps(v))
|
||||
del value[f]
|
||||
for f,v in value.items():
|
||||
result[f] = v
|
||||
|
||||
# ...and everything else goes in other_fields.
|
||||
logger.debug(' remaining fields: %s', str(value))
|
||||
result.other_fields = json.dumps(value)
|
||||
result.save()
|
||||
|
||||
if run_side_effects:
|
||||
|
@ -152,26 +147,3 @@ def create(
|
|||
|
||||
return result
|
||||
|
||||
def _normalise_type_for_thing(v):
|
||||
logger.debug('Normalising %s', v)
|
||||
|
||||
if v is None:
|
||||
return v # we're good with nulls
|
||||
if isinstance(v, str):
|
||||
return v # strings are fine
|
||||
elif isinstance(v, dict):
|
||||
return v # so are dicts
|
||||
elif isinstance(v, bool):
|
||||
return v # also booleans
|
||||
elif isinstance(v, list):
|
||||
return v # and lists as well
|
||||
elif isinstance(v, Thing):
|
||||
return v.url # Things can deal with themselves
|
||||
|
||||
# okay, it's something weird
|
||||
|
||||
try:
|
||||
return v.activity_form
|
||||
except AttributeError:
|
||||
return str(v)
|
||||
|
||||
|
|
|
@ -215,10 +215,8 @@ class Thing(PolymorphicModel):
|
|||
|
||||
value = _normalise_type_for_thing(value)
|
||||
|
||||
value = json.dumps(value)
|
||||
|
||||
if hasattr(self, 'f_'+name):
|
||||
setattr(self, 'f_'+name, value)
|
||||
setattr(self, 'f_'+name, json.dumps(value))
|
||||
else:
|
||||
others = json.loads(self.other_fields)
|
||||
others[name] = value
|
||||
|
|
Ładowanie…
Reference in New Issue