Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Dec 20, 2023
1 parent bd365b7 commit 796af62
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 36 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ black:
@black oscar_odin/
@black tests/

test:
python3 manage.py test tests/

ill:
rm db.sqlite3
cp klaas.sqlite3 db.sqlite3
Expand Down
5 changes: 2 additions & 3 deletions oscar_odin/django_resolver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Resolver for resolving attributes on Django models."""
from typing import Dict, Optional

from django.db.models import ForeignKey

from odin import Field
from odin.mapping import FieldResolverBase
from odin.utils import getmeta
Expand All @@ -17,7 +15,8 @@ def get_field_dict(self) -> Dict[str, Optional[Field]]:
meta = getmeta(self.obj)

if meta.object_name == "Product":
fields = {f: meta._forward_fields_map[f] for f in meta._forward_fields_map}
# pylint: disable=protected-access
fields = dict(meta._forward_fields_map.items())
else:
fields = {f.name: f for f in meta.fields}

Expand Down
2 changes: 1 addition & 1 deletion oscar_odin/management/commands/test_illshit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=W0611
# pylint: skip-file
import io
import PIL

Expand Down
11 changes: 3 additions & 8 deletions oscar_odin/mappings/_model_mapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Extended model mapper for Django models."""
from typing import Sequence, cast
from collections import defaultdict

from django.db.models.fields.related import ForeignKey, ManyToManyField
from django.db.models.fields.reverse_related import (
OneToOneRel,
ManyToOneRel,
ManyToManyRel,
)
Expand Down Expand Up @@ -32,9 +30,8 @@ def __new__(cls, name, bases, attrs):
# Extract out foreign field types.
mapping_type.many_to_one_fields = many_to_one_fields = []
mapping_type.one_to_many_fields = one_to_many_fields = []
mapping_type.many_to_many_fields = many_to_many_fields = [
field for field in meta.many_to_many
]

mapping_type.many_to_many_fields = [field for field in meta.many_to_many]
mapping_type.foreign_key_fields = [
field for field in meta.fields if isinstance(field, ForeignKey)
]
Expand Down Expand Up @@ -65,7 +62,7 @@ class ModelMapping(MappingBase, metaclass=ModelMappingMeta):
def create_object(self, **field_values):
"""Create a new product model."""
attribute_values = field_values.pop("attributes", [])

print(self.many_to_many_fields)
m2m_related_values = {
field: field_values.pop(field.name)
for field in self.many_to_many_fields
Expand All @@ -90,8 +87,6 @@ def create_object(self, **field_values):
for key, value in attribute_values.items():
parent.attr.set(key, value)

self.context.add_attribute_data((parent, attribute_values))

for relation, instances in m2o_related_values.items():
if instances:
self.context.add_instances_to_m2o_relation(
Expand Down
15 changes: 6 additions & 9 deletions oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# pylint: disable=W0613
"""Mappings between odin and django-oscar models."""
from decimal import Decimal
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union, NamedTuple
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union

import odin
from django.contrib.auth.models import AbstractUser
from django.db import transaction
from django.db.models import QuerySet, Model, ManyToManyField, ForeignKey
from django.core.exceptions import ValidationError
from django.db.models.fields.files import ImageFieldFile
from django.http import HttpRequest
from oscar.apps.partner.strategy import Default as DefaultStrategy
Expand All @@ -15,7 +15,7 @@
from datetime import datetime

from .. import resources
from ..resources.catalogue import Structure, ProductAttributeValue
from ..resources.catalogue import Structure
from ._common import map_queryset
from ._model_mapper import ModelMapping
from .utils import (
Expand Down Expand Up @@ -101,7 +101,6 @@ class CategoryToModel(odin.Mapping):
@odin.map_field
def image(self, value: Optional[str]) -> Optional[str]:
"""Convert value into a pure URL."""
# TODO convert into a form that can be accepted by a model
return value

@odin.map_field
Expand Down Expand Up @@ -223,9 +222,7 @@ def map_stock_price(self) -> Tuple[Decimal, str, int]:
stock_strategy: DefaultStrategy = self.context["stock_strategy"]

# Switch here based on if this is a parent or child product
price, availability, stock_record = stock_strategy.fetch_for_product(
self.source
)
price, availability, _ = stock_strategy.fetch_for_product(self.source)

if availability.is_available_to_buy:
return price.excl_tax, price.currency, availability.num_available
Expand Down Expand Up @@ -400,7 +397,7 @@ def products_to_db(
context.fields_to_update = fields_to_update
context.identifier_mapping = identifier_mapping

errors = {}
errors = []

with transaction.atomic():
# Save all the foreign keys; parents and productclasses
Expand All @@ -410,7 +407,7 @@ def products_to_db(
save_products(instances, context, errors)

# Save all product attributes
save_attributes(context, errors)
save_attributes(instances)

# Save and set all one to many relations; images, stockrecords
save_one_to_many(context, errors)
Expand Down
8 changes: 3 additions & 5 deletions oscar_odin/mappings/context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from collections import defaultdict
from operator import attrgetter

from odin.utils import getmeta

from oscar_odin.utils import in_bulk
from oscar_odin.exceptions import OscarOdinException

Expand All @@ -18,6 +16,7 @@ def get_instances_to_create_or_update(Model, instances, identifier_mapping):
identifiers = identifier_mapping.get(Model, {})

if identifiers:
# pylint: disable=protected-access
id_mapping = in_bulk(
Model._default_manager, instances=instances, field_names=identifiers
)
Expand All @@ -31,6 +30,7 @@ def get_instances_to_create_or_update(Model, instances, identifier_mapping):

if key in id_mapping:
instance.pk = id_mapping[key]
# pylint: disable=protected-access
instance._state.db = "default"
instance._state.adding = False
instances_to_update.append(instance)
Expand Down Expand Up @@ -90,11 +90,10 @@ def get_fields_to_update(self, Model):
def get_create_and_update_relations(self, related_instance_items):
to_create = defaultdict(list)
to_update = defaultdict(list)
instance_update_pks = []

for relation in related_instance_items.keys():
all_instances = []
for product, instances in related_instance_items[relation]:
for _, instances in related_instance_items[relation]:
all_instances.extend(instances)

(
Expand All @@ -121,7 +120,6 @@ def get_o2m_relations(self):
def get_fk_relations(self):
to_create = defaultdict(list)
to_update = defaultdict(list)
instance_update_pks = []

for relation, instances in self.foreign_key_items.items():
(
Expand Down
20 changes: 12 additions & 8 deletions oscar_odin/mappings/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
from collections import defaultdict

from django.db.models import prefetch_related_objects
from django.core.exceptions import ValidationError

from oscar.core.loading import get_model

from oscar_odin.mappings.context import get_instances_to_create_or_update
from oscar_odin.utils import querycounter, in_bulk
from oscar_odin.utils import in_bulk

Product = get_model("catalogue", "Product")
ProductAttributeValue = get_model("catalogue", "ProductAttributeValue")


def validate_instances(instances, errors={}, validate_unique=True):
def validate_instances(instances, errors, validate_unique=True):
validated_instances = []

for instance in instances:
instance.full_clean(validate_unique=validate_unique)
try:
instance.full_clean(validate_unique=validate_unique)
except ValidationError as e:
errors.append(e)

validated_instances.append(instance)

return validated_instances, errors
Expand Down Expand Up @@ -58,7 +62,7 @@ def save_one_to_many(context, errors):

for relation, instances in instances_to_create.items():
validated_instances_to_create, errors = validate_instances(instances, errors)
relation.related_model.objects.bulk_create(instances)
relation.related_model.objects.bulk_create(validated_instances_to_create)

for relation, instances in instances_to_update.items():
fields = context.get_fields_to_update(relation.related_model)
Expand All @@ -71,7 +75,7 @@ def save_many_to_many(context, errors):

# Create many to many's
for relation, instances in m2m_to_create.items():
validated_m2m_instances, errors = validate_instances(instances)
validated_m2m_instances, errors = validate_instances(instances, errors)
relation.related_model.objects.bulk_create(validated_m2m_instances)

# Update many to many's
Expand Down Expand Up @@ -110,13 +114,13 @@ def save_many_to_many(context, errors):
Through.objects.bulk_create(throughs.values())


def save_attributes(context, errors):
def save_attributes(instances):
attributes_to_create = []
attributes_to_delete = []
attributes_to_update = []
fields_to_be_updated = set()

for product, attr in context.attribute_data:
for product in instances:
product.attr.invalidate()
(
to_be_deleted,
Expand Down
3 changes: 1 addition & 2 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ load-plugins = pylint_django
score = n

[MESSAGES CONTROL]
disable = R,C,W5103,W0707,E5110,W0511

disable = R,C,W5103,W0707,E5110,W0511,W0102

0 comments on commit 796af62

Please sign in to comment.