+ updated = False
+ try:
+ if update_dict:
+ for dot_k, v in update_dict.items():
+ dict_to_update, key_to_update, _ = _iterate_keys(dot_k, db_item)
+ dict_to_update[key_to_update] = v
+ updated = True
+ if unset:
+ for dot_k in unset:
+ try:
+ dict_to_update, key_to_update, _ = _iterate_keys(
+ dot_k, db_item, populate=False
+ )
+ del dict_to_update[key_to_update]
+ updated = True
+ except Exception:
+ pass
+ if pull:
+ for dot_k, v in pull.items():
+ try:
+ dict_to_update, key_to_update, _ = _iterate_keys(
+ dot_k, db_item, populate=False
+ )
+ except Exception:
+ continue
+ if key_to_update not in dict_to_update:
+ continue
+ if not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot pull '{}'. Target is not a list".format(dot_k)
+ )
+ while v in dict_to_update[key_to_update]:
+ dict_to_update[key_to_update].remove(v)
+ updated = True
+ if pull_list:
+ for dot_k, v in pull_list.items():
+ if not isinstance(v, list):
+ raise DbException(
+ "Invalid content at pull_list, '{}' must be an array".format(
+ dot_k
+ ),
+ http_code=HTTPStatus.BAD_REQUEST,
+ )
+ try:
+ dict_to_update, key_to_update, _ = _iterate_keys(
+ dot_k, db_item, populate=False
+ )
+ except Exception:
+ continue
+ if key_to_update not in dict_to_update:
+ continue
+ if not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot pull_list '{}'. Target is not a list".format(dot_k)
+ )
+ for single_v in v:
+ while single_v in dict_to_update[key_to_update]:
+ dict_to_update[key_to_update].remove(single_v)
+ updated = True
+ if push:
+ for dot_k, v in push.items():
+ dict_to_update, key_to_update, populated = _iterate_keys(
+ dot_k, db_item
+ )
+ if (
+ isinstance(dict_to_update, dict)
+ and key_to_update not in dict_to_update
+ ):
+ dict_to_update[key_to_update] = [v]
+ updated = True
+ elif populated and dict_to_update[key_to_update] is None:
+ dict_to_update[key_to_update] = [v]
+ updated = True
+ elif not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot push '{}'. Target is not a list".format(dot_k)
+ )
+ else:
+ dict_to_update[key_to_update].append(v)
+ updated = True
+ if push_list:
+ for dot_k, v in push_list.items():
+ if not isinstance(v, list):
+ raise DbException(
+ "Invalid content at push_list, '{}' must be an array".format(
+ dot_k
+ ),
+ http_code=HTTPStatus.BAD_REQUEST,
+ )
+ dict_to_update, key_to_update, populated = _iterate_keys(
+ dot_k, db_item
+ )
+ if (
+ isinstance(dict_to_update, dict)
+ and key_to_update not in dict_to_update
+ ):
+ dict_to_update[key_to_update] = v.copy()
+ updated = True
+ elif populated and dict_to_update[key_to_update] is None:
+ dict_to_update[key_to_update] = v.copy()
+ updated = True
+ elif not isinstance(dict_to_update[key_to_update], list):
+ raise DbException(
+ "Cannot push '{}'. Target is not a list".format(dot_k),
+ http_code=HTTPStatus.CONFLICT,
+ )
+ else:
+ dict_to_update[key_to_update] += v
+ updated = True
+
+ return updated