From 4e3a990ac60a8035557e761e910eb9421a7f3cf6 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Fri, 29 Nov 2019 16:44:24 +0300 Subject: [PATCH] added command to fix existed collections --- .../management/commands/fix_collection.py | 32 +++++++++++++++++++ .../management/commands/import_collection.py | 7 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 apps/collection/management/commands/fix_collection.py diff --git a/apps/collection/management/commands/fix_collection.py b/apps/collection/management/commands/fix_collection.py new file mode 100644 index 00000000..9162f8f7 --- /dev/null +++ b/apps/collection/management/commands/fix_collection.py @@ -0,0 +1,32 @@ +from django.conf import settings +from django.core.management.base import BaseCommand +from tqdm import tqdm + +from collection.models import Collection + + +class Command(BaseCommand): + help = """Fix existed collections.""" + + def handle(self, *args, **kwarg): + update_collections = [] + collections = Collection.objects.values_list('id', 'collection_type', 'description') + for id, collection_type, description in tqdm(collections): + collection = Collection.objects.get(id=id) + description = collection.description + collection_updated = False + + if isinstance(description, str): + if description.lower().find('pop') != -1: + collection.collection_type = Collection.POP + collection_updated = True + + if not isinstance(description, dict): + collection.description = {settings.FALLBACK_LOCALE: collection.description} + collection_updated = True + + if collection_updated: + update_collections.append(collection) + + Collection.objects.bulk_update(update_collections, ['collection_type', 'description', ]) + self.stdout.write(self.style.WARNING(f'Updated products: {len(update_collections)}')) diff --git a/apps/collection/management/commands/import_collection.py b/apps/collection/management/commands/import_collection.py index 67d1cacf..f8f1702e 100644 --- a/apps/collection/management/commands/import_collection.py +++ b/apps/collection/management/commands/import_collection.py @@ -3,6 +3,7 @@ from establishment.models import Establishment from location.models import Country, Language from transfer.models import Collections from collection.models import Collection +from django.conf import settings from news.models import News @@ -93,9 +94,11 @@ class Command(BaseCommand): country = Country.objects.filter(code=obj['country_code']).first() if country: objects.append( - Collection(name={"en-GB": obj['title']}, collection_type=Collection.ORDINARY, + Collection(name={settings.FALLBACK_LOCALE: obj['title']}, + collection_type=Collection.POP if obj['description'].lower().find('pop') != -1 + else Collection.ORDINARY, country=country, - description=obj['description'], + description={settings.FALLBACK_LOCALE: obj['description']}, slug=obj['slug'], old_id=obj['collection_id'], start=obj['start'], image_url='https://s3.eu-central-1.amazonaws.com/gm-test.com/media/'+obj['attachment_suffix_url']