82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
from pprint import pprint
|
|
|
|
from transfer.models import Establishments
|
|
from transfer.serializers.establishment import EstablishmentSerializer
|
|
|
|
|
|
def transfer_establishment():
|
|
result = []
|
|
|
|
old_establishments = Establishments.objects.exclude(
|
|
type='Wineyard',
|
|
).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': [],
|
|
'tz': None,
|
|
'website': None,
|
|
'facebook': None,
|
|
'twitter': None,
|
|
'lafourchette': None,
|
|
'booking': None,
|
|
'schedules': None,
|
|
'location': 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'].append({
|
|
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}")
|
|
|
|
|
|
data_types = {
|
|
"establishment": [transfer_establishment]
|
|
}
|