added command to fix existed collections

This commit is contained in:
Anatoly 2019-11-29 16:44:24 +03:00
parent a102b68173
commit 4e3a990ac6
2 changed files with 37 additions and 2 deletions

View File

@ -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)}'))

View File

@ -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']