Created
November 25, 2019 10:49
-
-
Save mwesterhof/05dce295311300d8eaf023ec5b3ff33f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
from django.db import transaction | |
from .models import ProductCategory, Product, ProductMedia | |
class Importer: | |
# keep track of all objects that were created or updated ("seen") in the import | |
_item_tracker = defaultdict(set) | |
def _import_category(self, raw_data): | |
category = magic(raw_data) # do something to create or update a category object | |
for product_data in raw_data['products']: | |
self._import_product(category, product_data) | |
self._item_tracker[ProductCategory].add(category.pk) | |
def _import_products(self, for_category, raw_data): | |
product = magic(for_category, raw_data) # do something to create or update a product object | |
for media_data in raw_data['media']: | |
self._import_product_media(product, media_data) | |
self._item_tracker[Product].add(product.pk) | |
def _import_product_media(self, for_product, raw_data): | |
media = magic(for_product, raw_data) | |
self._item_tracker[ProductMedia].add(media.pk) | |
@transaction.atomic() | |
def do_import(self, raw_data): | |
for line in raw_data: | |
self._import_category(line) | |
# auto-cleanup all objects that were not "seen" for all registered models | |
for model, seen_pks in self._item_tracker.items(): | |
model.objects.exclude(pk__in=seen_pks).delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment