Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #81

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions nefertari_mongodb/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,19 +527,32 @@ def update_iterables(self, params, attr, unique=False,
is_dict = isinstance(type(self)._fields[attr], mongo.DictField)
is_list = isinstance(type(self)._fields[attr], mongo.ListField)

if is_list:
is_list_of_dicts=isinstance(type(self)._fields[attr].item_type, type(mongo.DictField))
def split_keys(keys):
neg_keys = []
pos_keys = []

self_keys = []
if is_list_of_dicts:
self_keys= getattr(self,attr)
for key in keys:
#edited to support dicts in a array
if isinstance(key,dict):
if key in getattr(self,attr):
self_keys.remove(key)
pos_keys.append(key)
continue
if key.startswith('__'):
continue
if key.startswith('-'):
neg_keys.append(key[1:])
else:
pos_keys.append(key.strip())
if self_keys:
neg_keys.extend(self_keys)
return pos_keys, neg_keys


def update_dict(update_params):
final_value = getattr(self, attr, {}) or {}
final_value = final_value.copy()
Expand Down Expand Up @@ -585,7 +598,10 @@ def update_list(update_params):
final_value += positive

if negative:
final_value = list(set(final_value) - set(negative))
if is_list_of_dicts:
[final_value.remove(i) for i in final_value if i in negative]
else:
final_value = list(set(final_value) - set(negative))

setattr(self, attr, final_value)
if save:
Expand All @@ -597,6 +613,12 @@ def update_list(update_params):
elif is_list:
update_list(params)

if is_dict:
update_dict(params)

elif is_list:
update_list(params)

@classmethod
def expand_with(cls, with_cls, join_on=None, attr_name=None, params={},
with_params={}):
Expand Down