diff --git a/apps/location/transfer_data.py b/apps/location/transfer_data.py index 0d90461a..658d2d20 100644 --- a/apps/location/transfer_data.py +++ b/apps/location/transfer_data.py @@ -1,8 +1,8 @@ from transfer.serializers import location as location_serializers from transfer import models as transfer_models -from location.models import Country +from location.models import Country, CityGallery, City +from gallery.models import Image from pprint import pprint - from requests import get @@ -179,6 +179,42 @@ def update_flags(): query.save() +def transfer_city_gallery(): + created_counter = 0 + cities_not_exists = {} + gallery_obj_exists_counter = 0 + + city_gallery = transfer_models.CityPhotos.objects.exclude(city__isnull=True) \ + .exclude(city__country_code_2__isnull=True) \ + .exclude(city__country_code_2__iexact='') \ + .exclude(city__region_code__isnull=True) \ + .exclude(city__region_code__iexact='') \ + .values_list('city_id', 'attachment_suffix_url') + for old_city_id, image_suffix_url in city_gallery: + city = City.objects.filter(old_id=old_city_id) + if city.exists(): + city = city.first() + image, _ = Image.objects.get_or_create(image=image_suffix_url, + defaults={ + 'image': image_suffix_url, + 'orientation': Image.HORIZONTAL, + 'title': f'{city.name} - {image_suffix_url}', + }) + city_gallery, created = CityGallery.objects.get_or_create(image=image, + city=city, + is_main=True) + if created: + created_counter += 1 + else: + gallery_obj_exists_counter += 1 + else: + cities_not_exists.update({'city_old_id': old_city_id}) + + print(f'Created: {created_counter}\n' + f'City not exists: {cities_not_exists}\n' + f'Already added: {gallery_obj_exists_counter}') + + data_types = { "dictionaries": [ transfer_countries, @@ -192,4 +228,5 @@ data_types = { "update_country_flag": [ update_flags ], + "fill_city_gallery": [transfer_city_gallery] } diff --git a/apps/transfer/management/commands/transfer.py b/apps/transfer/management/commands/transfer.py index 9126856f..2d0ce399 100644 --- a/apps/transfer/management/commands/transfer.py +++ b/apps/transfer/management/commands/transfer.py @@ -40,6 +40,7 @@ class Command(BaseCommand): 'product_review', 'newsletter_subscriber', # подписчики на рассылку - переносить после переноса пользователей №1 'purchased_plaques', # №6 - перенос купленных тарелок + 'fill_city_gallery', # №3 - перенос галереи городов ] def handle(self, *args, **options): diff --git a/apps/transfer/models.py b/apps/transfer/models.py index f4a1a800..a8190879 100644 --- a/apps/transfer/models.py +++ b/apps/transfer/models.py @@ -217,10 +217,10 @@ class CityNames(MigrateMixin): class CityPhotos(MigrateMixin): using = 'legacy' - # city_id = models.IntegerField(blank=True, null=True) city = models.ForeignKey(Cities, models.DO_NOTHING, blank=True, null=True) attachment_file_name = models.CharField(max_length=255, blank=True, null=True) attachment_content_type = models.CharField(max_length=255, blank=True, null=True) + attachment_suffix_url = models.CharField(max_length=255) geometries = models.CharField(max_length=1024, blank=True, null=True) attachment_file_size = models.IntegerField(blank=True, null=True) attachment_updated_at = models.DateTimeField(blank=True, null=True)