gault-millau/apps/establishment/transfer_data.py
2019-10-31 08:49:17 +03:00

139 lines
3.9 KiB
Python

from pprint import pprint
from django.db.models import Q, F
from transfer.models import Establishments, Dishes
from transfer.serializers.establishment import EstablishmentSerializer
from establishment.models import Establishment
from location.models import Address
from transfer.serializers.plate import PlateSerializer
def transfer_establishment():
result = []
old_establishments = Establishments.objects.filter(
location__city__name__icontains='paris',
).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,
'state': item.state,
'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_menu():
dishes = Dishes.objects.exclude(
Q(establishment_id__isnull=True) |
Q(dish_type__isnull=True) |
Q(price__isnull=True) |
Q(currency__isnull=True) |
Q(name__isnull=True) |
Q(name__exact='')
).annotate(
country_code=F('establishment__location__country_code'),
)
serialized_data = PlateSerializer(data=list(dishes.values()), many=True)
if serialized_data.is_valid():
serialized_data.save()
else:
pprint(f"Menu 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 Address.MultipleObjectsReturned:
location = Address.objects.filter(old_id=old_establishment.location_id).first()
except Address.DoesNotExist:
continue
establishment.address = location
establishment.save()
data_types = {
"establishment": [
transfer_establishment,
],
"location_establishment": [
transfer_establishment_addresses
],
"menu": [transfer_menu],
}