Skip to content

Commit

Permalink
Clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Dec 8, 2023
1 parent 687a7e8 commit 8fbed89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
54 changes: 11 additions & 43 deletions oscar_odin/mappings/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,40 +100,17 @@ def add_instances_to_o2m_relation(self, relation, instances):
def add_instance_to_fk_items(self, field, instance):
if not instance.pk:
self.foreign_key_items[field] += [instance]

def get_filled_out_fields(self, source_objects):
fields = set()
for source in source_objects:
meta = getmeta(source)
fields.update([k for k, v in source.__dict__.items() if v is not None])

return fields

def add_source_fields(self, model, source_objects):
self.source_fields[model] = self.get_filled_out_fields(source_objects)


def add_fields_to_update(self, fields_to_update):
self.fields_to_update = fields_to_update

def get_fields_to_update(self, Model):
model_field_names = [field.name for field in Model._meta.get_fields()]
return [f for f in self.fields_to_update if f in model_field_names] or None

@property
def get_all_m2o_instances(self):
for relation in self.many_to_one_items:
for product, instances in self.many_to_one_items[relation]:
yield (relation, product, instances)

@property
def get_all_m2m_instances(self):
for relation in self.many_to_many_items:
for product, instances in self.many_to_many_items[relation]:
yield (relation, product, instances)

def get_create_and_update_relations(self, related_instance_items):
m2m_to_create = defaultdict(list)
m2m_to_update = defaultdict(list)
to_create = defaultdict(list)
to_update = defaultdict(list)
instance_update_pks = []

for relation in related_instance_items.keys():
Expand All @@ -146,14 +123,10 @@ def get_create_and_update_relations(self, related_instance_items):
instances_to_update,
) = get_instances_to_create_or_update(relation.related_model, all_instances)

m2m_to_create[relation].extend(instances_to_create)
m2m_to_update[relation].extend(instances_to_update)
# for instance in instances_to_update:
# if instance.pk not in instance_update_pks:
# m2m_to_update[relation].append(instance)
# instance_update_pks.append(instance.pk)
to_create[relation].extend(instances_to_create)
to_update[relation].extend(instances_to_update)

return (m2m_to_create, m2m_to_update)
return (to_create, to_update)

@property
def get_all_m2m_relations(self):
Expand All @@ -165,8 +138,8 @@ def get_o2m_relations(self):

@property
def get_fk_relations(self):
m2m_to_create = defaultdict(list)
m2m_to_update = defaultdict(list)
to_create = defaultdict(list)
to_update = defaultdict(list)
instance_update_pks = []

for relation, instances in self.foreign_key_items.items():
Expand All @@ -175,15 +148,10 @@ def get_fk_relations(self):
instances_to_update,
) = get_instances_to_create_or_update(relation.related_model, instances)

m2m_to_create[relation].extend(instances_to_create)
m2m_to_update[relation].extend(instances_to_update)

# for instance in instances_to_update:
# if instance.pk not in instance_update_pks:
# m2m_to_update[relation].append(instance)
# instance_update_pks.append(instance.pk)
to_create[relation].extend(instances_to_create)
to_update[relation].extend(instances_to_update)

return (m2m_to_create, m2m_to_update)
return (to_create, to_update)

@property
def get_all_o2m_instances(self):
Expand Down
1 change: 1 addition & 0 deletions oscar_odin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.db.models import Model, ManyToManyField, ForeignKey, Q
from django.db import connections


def get_filters(instances, field_names):
for ui in instances:
klaas = {}
Expand Down

0 comments on commit 8fbed89

Please sign in to comment.