diff --git a/apps/main/migrations/0027_carousel_country.py b/apps/main/migrations/0027_carousel_country.py new file mode 100644 index 00000000..8686299d --- /dev/null +++ b/apps/main/migrations/0027_carousel_country.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.4 on 2019-11-01 14:23 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('location', '0020_merge_20191030_1714'), + ('main', '0026_auto_20191101_0500'), + ] + + operations = [ + migrations.AddField( + model_name='carousel', + name='country', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='location.Country', verbose_name='country'), + ), + ] diff --git a/apps/main/models.py b/apps/main/models.py index 0e070a95..14a548be 100644 --- a/apps/main/models.py +++ b/apps/main/models.py @@ -295,6 +295,13 @@ class Carousel(models.Model): attachment_suffix_url = models.TextField(_('old attachment_suffix_url'), blank=True, null=True, default=None) description = models.CharField(_('old description'), max_length=255, blank=True, null=True, default=None) link_title = models.CharField(_('old link_title'), max_length=255, blank=True, null=True, default=None) + country = models.ForeignKey( + Country, + blank=True, + null=True, + on_delete=models.SET_NULL, + verbose_name=_('country') + ) active = models.BooleanField(_('old active'), default=False) is_parse = models.BooleanField(_('is parse'), default=False) diff --git a/apps/main/transfer_data.py b/apps/main/transfer_data.py index efb7938d..1efae849 100644 --- a/apps/main/transfer_data.py +++ b/apps/main/transfer_data.py @@ -1,11 +1,15 @@ from pprint import pprint +from django.db.models import F + from transfer.models import CarouselElements from transfer.serializers.carousel import CarouselSerializer def transfer_carousel(): - queryset = CarouselElements.objects.all() + queryset = CarouselElements.objects.all().annotate( + country=F('home_page__site__country_code_2'), + ) serialized_data = CarouselSerializer(data=list(queryset.values()), many=True) if serialized_data.is_valid(): diff --git a/apps/transfer/models.py b/apps/transfer/models.py index 0ac0ee8a..f4a1ade5 100644 --- a/apps/transfer/models.py +++ b/apps/transfer/models.py @@ -940,12 +940,23 @@ class KeyValueMetadatumKeyValueMetadatumEstablishments(MigrateMixin): # managed = False # db_table = 'wine_types' +class HomePages(models.Model): + using = 'legacy' + + site = models.ForeignKey(Sites, models.DO_NOTHING, blank=True, null=True) + selection_of_week = models.IntegerField(blank=True, null=True) + + class Meta: + managed = False + db_table = 'home_pages' + + class CarouselElements(MigrateMixin): using = 'legacy' title = models.CharField(max_length=255, blank=True, null=True) link = models.CharField(max_length=255, blank=True, null=True) - home_page_id = models.IntegerField(blank=True, null=True) + home_page = models.ForeignKey(HomePages, models.DO_NOTHING, blank=True, null=True) created_at = models.DateTimeField() updated_at = models.DateTimeField() attachment_file_name = models.CharField(max_length=255, blank=True, null=True) diff --git a/apps/transfer/serializers/carousel.py b/apps/transfer/serializers/carousel.py index 3f1e8ecb..9340cf35 100644 --- a/apps/transfer/serializers/carousel.py +++ b/apps/transfer/serializers/carousel.py @@ -2,6 +2,7 @@ from django.contrib.contenttypes.models import ContentType from rest_framework import serializers from establishment.models import Establishment +from location.models import Country from main.models import Carousel from news.models import News @@ -24,18 +25,26 @@ class CarouselSerializer(serializers.Serializer): description = serializers.CharField(allow_null=True, allow_blank=True) attachment_suffix_url = serializers.CharField(allow_null=True) active = serializers.IntegerField() + country = serializers.CharField(allow_null=True) def create(self, validated_data): object_id, content_type_id = self.get_content_type(validated_data) validated_data.update({ + 'old_id': validated_data['id'], + 'country': self.get_country(validated_data), 'active': bool(int(validated_data['active'])), 'content_type_id': content_type_id, 'object_id': object_id, 'is_parse': bool(object_id), }) - obj = Carousel.objects.create(**validated_data) + validated_data.pop('id') + obj, _ = Carousel.objects.update_or_create(**validated_data) return obj + @staticmethod + def get_country(data): + return Country.objects.filter(code__iexact=data['country']).first() + @staticmethod def get_content_type(data): link = data['link']