Skip to content

Commit

Permalink
ProductResource to ProductModel mapping with bulk update / create
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavage authored and viggo-devries committed Dec 28, 2023
1 parent 4105394 commit d9a25a4
Show file tree
Hide file tree
Showing 45 changed files with 4,291 additions and 247 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ jobs:

- name: Install dependencies
run: |
python -m pip install --upgrade pip poetry
python -m pip install --upgrade pip
pip install .[test]
poetry install --all-extras --no-root
- name: Check code quality
run: |
poetry run black --check oscar_odin/
poetry run pylint oscar_odin/
- name: Test with Django test runner
run: |
poetry run coverage run --branch -- ./runtests.py
poetry run coverage run --branch -- ./manage.py test tests/
poetry run coverage xml --include="oscar_odin/*" -o dist/coverage.xml
poetry run coverage report --include="oscar_odin/*"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ lib/
lib64/
parts/
sdist/
images/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
*.DS_Store


# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
48 changes: 18 additions & 30 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
default_language_version:
python: python3.8
python: python3.10


repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args:
- --py38-plus
- --keep-runtime-typing
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.270
hooks:
- id: ruff
args:
- --fix
- --exit-non-zero-on-fix
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- id: isort
name: isort (cython)
types: [cython]
- id: isort
name: isort (pyi)
types: [pyi]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: local
hooks:
- id: black
name: black
language: system
types: [python]
entry: black
args: [--check, oscar_odin/]
pass_filenames: false
- id: pylint
name: pylint
entry: pylint
args: [oscar_odin/]
language: system
types: [python]
pass_filenames: false
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.PHONY: fail-if-no-virtualenv all install dev lint test black

all: install migrate loaddata collectstatic

fail-if-no-virtualenv:
ifndef VIRTUAL_ENV # check for a virtualenv in development environment
ifndef PYENVPIPELINE_VIRTUALENV # check for jenkins pipeline virtualenv
$(error this makefile needs a virtualenv)
endif
endif

ifndef PIP_INDEX_URL
PIP_INDEX_URL=https://pypi.org/simple
endif


dev: install
pip install .[dev]

install: fail-if-no-virtualenv
pip install .[test]

lint: fail-if-no-virtualenv
black --check oscar_odin/
pylint oscar_odin/

test: fail-if-no-virtualenv
python3 manage.py makemigrations --check --dry-run
@python3 manage.py test tests/

black:
@black oscar_odin/
@black tests/

test:
python3 manage.py test tests/

ill:
rm db.sqlite3
cp klaas.sqlite3 db.sqlite3
python3 manage.py migrate
python3 manage.py test_illshit
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ Product = get_model("catalogue", "Product")
product = Product.objects.get(id=1)
product_resource = catalogue.product_to_resource(product)
```

# Developing odin

## Using pip:

make install
make test

## Using poetry:

poetry install --all-extras
poetry run ./manage.py test
14 changes: 14 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python
import os
import sys

import django
from django.conf import settings


if __name__ == "__main__":
# Configure path and django settings module
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)) + "/tests")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def tests(session: Session):
)
# fmt: on
session.install("-Ur", "requirements.txt")
session.run(HERE / "runtests.py")
session.run(HERE / "manage.py")
9 changes: 9 additions & 0 deletions oscar_odin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
Odin Resources and mappings to Oscar models.
"""
import os
import sys


def django_manage():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings")
from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
2 changes: 1 addition & 1 deletion oscar_odin/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.apps import AppConfig
from django.db.models import Model
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from odin import registration

from .django_resolver import ModelFieldResolver
Expand Down
12 changes: 11 additions & 1 deletion oscar_odin/django_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
class ModelFieldResolver(FieldResolverBase):
"""Field resolver for Django models."""

# pylint: disable=protected-access
def get_field_dict(self) -> Dict[str, Optional[Field]]:
"""Get a dictionary of fields from the source object."""
meta = getmeta(self.obj)
return {f.attname: f for f in meta.fields}

fields = meta._forward_fields_map

fields.update(
(r.related_name, r.field)
for r in meta.related_objects
if r.related_name != "+"
)

return fields
2 changes: 2 additions & 0 deletions oscar_odin/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class OscarOdinException(TypeError):
pass
2 changes: 1 addition & 1 deletion oscar_odin/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DecimalField(ScalarField):
}
scalar_type = Decimal

def __init__(self, places: int = 4, **kwargs):
def __init__(self, places: int = 2, **kwargs):
"""Initialise the field."""
super().__init__(**kwargs)
self.places = places
Expand Down
Loading

0 comments on commit d9a25a4

Please sign in to comment.