add_establishment_description with elastic error
This commit is contained in:
parent
f3899f5cb8
commit
cc2c475b4e
|
|
@ -1,30 +1,65 @@
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from establishment.models import Establishment
|
from establishment.models import Establishment
|
||||||
from transfer.models import Descriptions
|
from transfer.models import Reviews, ReviewTexts
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = 'Add description values from old db to new db'
|
help = 'Add description values from old db reviews to new db'
|
||||||
|
|
||||||
def handle(self, *args, **kwargs):
|
def handle(self, *args, **kwargs):
|
||||||
count = 0
|
count = 0
|
||||||
|
update_locale = 0
|
||||||
|
valid_reviews = {}
|
||||||
|
|
||||||
queryset = Descriptions.objects.all()
|
queryset = Reviews.objects.exclude(
|
||||||
for obj in queryset:
|
establishment_id__isnull=True
|
||||||
|
).filter(
|
||||||
|
aasm_state='published'
|
||||||
|
).values_list(
|
||||||
|
'id',
|
||||||
|
'establishment_id',
|
||||||
|
'updated_at',
|
||||||
|
)
|
||||||
|
|
||||||
|
for r_id, establishment_id, new_date in queryset:
|
||||||
try:
|
try:
|
||||||
establishment = Establishment.objects.get(old_id=obj.establishment.id)
|
es_id, date = valid_reviews[r_id]
|
||||||
except Establishment.DoesNotExist:
|
except KeyError:
|
||||||
continue
|
valid_reviews[r_id] = (establishment_id, new_date)
|
||||||
except Establishment.MultipleObjectsReturned:
|
|
||||||
establishment = Establishment.objects.filter(old_id=obj.establishment.id).first()
|
|
||||||
else:
|
else:
|
||||||
|
if new_date > date:
|
||||||
|
valid_reviews[r_id] = (establishment_id, new_date)
|
||||||
|
|
||||||
|
text_qs = ReviewTexts.objects.filter(
|
||||||
|
review_id__in=(r_id for r_id in valid_reviews.keys())
|
||||||
|
).values_list(
|
||||||
|
'review__establishment_id',
|
||||||
|
'locale',
|
||||||
|
'text',
|
||||||
|
)
|
||||||
|
|
||||||
|
for es_id, locale, text in text_qs:
|
||||||
|
establishment = Establishment.objects.filter(old_id=es_id).first()
|
||||||
|
if establishment:
|
||||||
description = establishment.description
|
description = establishment.description
|
||||||
description.update({
|
description.update({
|
||||||
obj.locale: obj.text
|
locale: text
|
||||||
})
|
})
|
||||||
establishment.description = description
|
establishment.description = description
|
||||||
establishment.save()
|
establishment.save()
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
|
# Если нет en-GB в поле
|
||||||
|
for establishment in Establishment.objects.filter(old_id__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()
|
||||||
|
update_locale += 1
|
||||||
|
|
||||||
self.stdout.write(self.style.WARNING(f'Updated {count} objects.'))
|
self.stdout.write(self.style.WARNING(f'Updated {count} objects.'))
|
||||||
|
self.stdout.write(self.style.WARNING(f'Updated en-GB locale - {count}'))
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,6 @@ class EstablishmentSerializer(serializers.ModelSerializer):
|
||||||
if address:
|
if address:
|
||||||
return address.id
|
return address.id
|
||||||
return None
|
return None
|
||||||
# return None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_type(data):
|
def get_type(data):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user