gault-millau/apps/establishment/transfer_data.py
2019-10-31 07:00:44 +03:00

109 lines
3.0 KiB
Python

from pprint import pprint
from django.db.models import Q
from transfer.models import Establishments
from transfer.serializers.establishment import EstablishmentSerializer
from establishment.models import Establishment
from location.models import Address
def transfer_establishment():
result = []
old_establishments = Establishments.objects.exclude(
Q(type='Wineyard') |
Q(location__timezone__isnull=True)
).prefetch_related(
'establishmentinfos_set',
'schedules_set',
'descriptions_set',
)
for item in old_establishments:
data = {
'old_id': item.id,
'name': item.name,
'name_translated': item.index_name,
'slug': item.slug,
'type': item.type,
'phone': item.phone,
'created': item.created_at,
'description': {},
'website': None,
'facebook': None,
'twitter': None,
'lafourchette': None,
'booking': None,
'schedules': None,
'email': None,
}
if item.location:
data.update({
'location': item.location.id,
'tz': item.location.timezone,
})
# Инфо
info = item.establishmentinfos_set.first()
if info:
data.update({
'website': info.website,
'facebook': info.facebook,
'twitter': info.twitter,
'lafourchette': info.lafourchette,
'booking': info.booking_url,
'email': info.email,
})
# Время работы
schedule = item.schedules_set.first()
if schedule:
data.update({
'schedules': schedule.timetable,
})
# Описание
descriptions = item.descriptions_set.all()
for description in descriptions:
data['description'].update({
description.locale: description.text,
})
result.append(data)
serialized_data = EstablishmentSerializer(data=result, many=True)
if serialized_data.is_valid():
serialized_data.save()
else:
pprint(f"Establishment serializer errors: {serialized_data.errors}")
def transfer_establishment_addresses():
old_establishments = Establishments.objects.only("id", "location_id").exclude(
Q(type='Wineyard') |
Q(location__timezone__isnull=True)
)
for old_establishment in old_establishments:
try:
establishment = Establishment.objects.get(old_id=old_establishment.id)
except Establishment.DoesNotExist:
continue
try:
location = Address.objects.get(old_id=old_establishment.location_id)
except Establishment.DoesNotExist:
continue
establishment.address = location
establishment.save()
data_types = {
"establishment": [
transfer_establishment,
transfer_establishment_addresses
]
}