diff --git a/apps/establishment/management/commands/add_establishment_description.py b/apps/establishment/management/commands/add_establishment_description.py index 533a8bd7..f0d7da59 100644 --- a/apps/establishment/management/commands/add_establishment_description.py +++ b/apps/establishment/management/commands/add_establishment_description.py @@ -1,5 +1,5 @@ from django.core.management.base import BaseCommand - +from tqdm import tqdm from establishment.models import Establishment from transfer.models import Reviews, ReviewTexts @@ -22,7 +22,7 @@ class Command(BaseCommand): 'updated_at', ) - for r_id, establishment_id, new_date in queryset: + for r_id, establishment_id, new_date in tqdm(queryset): try: review_id, date = valid_reviews[establishment_id] except KeyError: @@ -41,7 +41,7 @@ class Command(BaseCommand): 'text', ) - for es_id, locale, text in text_qs: + for es_id, locale, text in tqdm(text_qs): establishment = Establishment.objects.filter(old_id=es_id).first() if establishment: description = establishment.description @@ -53,7 +53,7 @@ class Command(BaseCommand): count += 1 # Если нет en-GB в поле - for establishment in Establishment.objects.filter(old_id__isnull=False): + for establishment in tqdm(Establishment.objects.filter(old_id__isnull=False)): description = establishment.description if len(description) and 'en-GB' not in description: description.update({ diff --git a/apps/establishment/management/commands/add_true_description.py b/apps/establishment/management/commands/add_true_description.py new file mode 100644 index 00000000..ab810507 --- /dev/null +++ b/apps/establishment/management/commands/add_true_description.py @@ -0,0 +1,45 @@ +from django.core.management.base import BaseCommand +from tqdm import tqdm + +from establishment.models import Establishment +from transfer.models import Descriptions + + +class Command(BaseCommand): + help = """Add description to establishment from old db.""" + + def handle(self, *args, **kwarg): + establishments = Establishment.objects.exclude(old_id__isnull=True) + + self.stdout.write(self.style.WARNING(f'Clear old descriptions')) + for item in tqdm(establishments): + item.description = None + item.save() + + queryset = Descriptions.objects.filter( + establishment_id__in=list(establishments.values_list('old_id', flat=True)), + ).values_list('establishment_id', 'locale', 'text') + + self.stdout.write(self.style.WARNING(f'Update new description')) + for establishment_id, locale, text in tqdm(queryset): + establishment = Establishment.objects.filter(old_id=establishment_id).first() + if establishment: + if establishment.description: + establishment.description.update({ + locale: text + }) + else: + establishment.description = {locale: text} + establishment.save() + + self.stdout.write(self.style.WARNING(f'Update en-GB description')) + for establishment in tqdm(establishments.filter(description__isnull=False)): + description = establishment.description + if len(description) and 'en-GB' not in description: + description.update({ + 'en-GB': next(iter(description.values())) + }) + establishment.description = description + establishment.save() + + self.stdout.write(self.style.WARNING(f'Done')) diff --git a/apps/transfer/serializers/establishment.py b/apps/transfer/serializers/establishment.py index a287d61b..1db16711 100644 --- a/apps/transfer/serializers/establishment.py +++ b/apps/transfer/serializers/establishment.py @@ -77,7 +77,11 @@ class EstablishmentSerializer(serializers.ModelSerializer): schedules = validated_data.pop('schedules') subtypes = [validated_data.pop('subtype', None)] - establishment = Establishment.objects.create(**validated_data) + # establishment = Establishment.objects.create(**validated_data) + establishment, _ = Establishment.objects.update_or_create( + old_id=validated_data['old_id'], + defaults=validated_data, + ) if email: ContactEmail.objects.get_or_create( email=email,