Skip to content

Instantly share code, notes, and snippets.

@dwcaraway
Created September 18, 2013 13:02
Show Gist options
  • Save dwcaraway/6608849 to your computer and use it in GitHub Desktop.
Save dwcaraway/6608849 to your computer and use it in GitHub Desktop.
Possible bug in model_save.py
def package_extras_save(extra_dicts, obj, context):
allow_partial_update = context.get("allow_partial_update", False)
if extra_dicts is None and allow_partial_update:
return
model = context["model"]
session = context["session"]
extras_list = obj.extras_list
old_extras = dict((extra.key, extra) for extra in extras_list)
new_extras = {}
for extra_dict in extra_dicts or []:
if extra_dict.get("deleted"):
continue
if extra_dict['value'] is None:
pass
else:
new_extras[extra_dict["key"]] = extra_dict["value"]
#new
for key in set(new_extras.keys()) - set(old_extras.keys()):
state = 'pending' if context.get('pending') else 'active'
extra = model.PackageExtra(state=state, key=key, value=new_extras[key])
session.add(extra)
extras_list.append(extra)
#changed
for key in set(new_extras.keys()) & set(old_extras.keys()):
extra = old_extras[key]
#dont change state to pending if nothing has changed
if new_extras[key] == extra.value and extra.state != 'deleted':
continue
state = 'pending' if context.get('pending') else 'active'
extra.value = new_extras[key]
extra.state = state
session.add(extra)
#deleted
for key in set(old_extras.keys()) - set(new_extras.keys()):
extra = old_extras[key]
if extra.state == 'deleted':
continue
state = 'pending-deleted' if context.get('pending') else 'deleted'
extra.state = state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment