From 989e80ece6bcaab9bdab36cdac41bc84e1890807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Thu, 31 Oct 2019 16:57:12 +0300 Subject: [PATCH 1/5] First --- apps/collection/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/import_collection.py | 80 +++++++++++++++++ apps/collection/transfer.py | 17 +--- apps/location/transfer_data.py | 87 +++++++++++-------- 5 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 apps/collection/management/__init__.py create mode 100644 apps/collection/management/commands/__init__.py create mode 100644 apps/collection/management/commands/import_collection.py diff --git a/apps/collection/management/__init__.py b/apps/collection/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/collection/management/commands/__init__.py b/apps/collection/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/collection/management/commands/import_collection.py b/apps/collection/management/commands/import_collection.py new file mode 100644 index 00000000..7c6d92f8 --- /dev/null +++ b/apps/collection/management/commands/import_collection.py @@ -0,0 +1,80 @@ +from django.core.management.base import BaseCommand +from establishment.models import Establishment +from location.models import Country +from transfer.models import Collections +from collection.models import Collection +from news.models import News + + +class Command(BaseCommand): + help = 'Import collection' + + def handle(self, *args, **kwargs): + raw_qs = Collections.objects.raw(''' + select + distinct + a.collection_id, + a.establishment_id, + a.title, + a.tag_name, + a.slug, + a.attachment_file_name, + a.attachment_content_type, + a.attachment_file_size, + a.attachment_suffix_url, + active as is_publish,, + a.country_code, + a.geometries, + a.description + from + ( + select distinct + c.id as collection_id, + m.establishment_id, + c.title, c.tag_name, + c.slug, c.attachment_file_name, + c.attachment_content_type, c.attachment_file_size, + c.attachment_suffix_url, + active, + s.country_code_2 as country_code, + c.geometries, + c.title as description + from collections as c + join metadata m on m.value = c.tag_name + join establishments e on e.id = m.establishment_id + join sites s on s.id = c.site_id + where m.`key` = 'collection' + + union + + select distinct + c.id as collection_id, + m.establishment_id, + c.title, c.tag_name, + c.slug, c.attachment_file_name, + c.attachment_content_type, c.attachment_file_size, + c.attachment_suffix_url, + active, + s.country_code_2 as country_code, + c.geometries, + c.title as description + from collections as c + join metadata m on m.value = c.slug + join establishments e on e.id = m.establishment_id + join sites s on s.id = c.site_id + where m.`key` = 'collection' + ) a + ''') + objects = [] + queryset = [vars(query) for query in raw_qs] + for obj in queryset: + establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() + country = Country.objects.filter(code=obj['country_code']).first() + objects.append( + Collection(name={"en-GB":obj['title']}, collection_type = Collection.ORDINARY, + is_publish=obj['is_publish'], country=country, + description=obj['description'], + slug=obj['slug'] + ) + ) + # self.stdout.write(self.style.WARNING(f'Deleted {count} objects.')) diff --git a/apps/collection/transfer.py b/apps/collection/transfer.py index 6c18c9ae..1f7feb46 100644 --- a/apps/collection/transfer.py +++ b/apps/collection/transfer.py @@ -27,22 +27,7 @@ card = { # "country": "Country", } }, - "Guide": { - # как работать с ForeignKey на самого себя(self), поле "parent" - "data_type": "objects", - "dependencies": ("Collection", "self"), - "fields": { - "Guides": { - # нету аналогов для полей start и end - "name": "title" - } - }, - "relations": { - # аналалог для поля "collection" не найдено - # "parent": "Guide", - # "collection": "Collection" - } - } + } used_apps = ("location", ) diff --git a/apps/location/transfer_data.py b/apps/location/transfer_data.py index b19eb42b..afd964ee 100644 --- a/apps/location/transfer_data.py +++ b/apps/location/transfer_data.py @@ -6,11 +6,13 @@ from pprint import pprint def transfer_countries(): - queryset = Cities.objects.raw("""SELECT cities.id, cities.country_code_2 - FROM cities WHERE - country_code_2 IS NOT NULL AND - country_code_2 != "" - GROUP BY cities.country_code_2""") + queryset = Cities.objects.raw(""" + SELECT cities.id, cities.country_code_2 + FROM cities + WHERE country_code_2 IS NOT NULL AND + country_code_2 != "" + GROUP BY cities.id, cities.country_code_2 + """) queryset = [vars(query) for query in queryset] @@ -22,16 +24,24 @@ def transfer_countries(): def transfer_regions(): - regions_without_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, - cities.country_code_2, cities.subregion_code - FROM cities WHERE - (subregion_code IS NULL OR - subregion_code = "") AND - region_code IS NOT NULL AND - region_code != "" AND - country_code_2 IS NOT NULL AND - country_code_2 != "" - GROUP BY region_code""") + regions_without_subregion_queryset = Cities.objects.raw(""" + SELECT cities.id, + cities.region_code, + cities.country_code_2, + cities.subregion_code + FROM cities + WHERE (subregion_code IS NULL + OR subregion_code = "" + ) + AND region_code IS NOT NULL + AND region_code != "" + AND country_code_2 IS NOT NULL + AND country_code_2 != "" + GROUP BY cities.id, + cities.region_code, + cities.country_code_2, + cities.subregion_code + """) regions_without_subregion_queryset = [vars(query) for query in regions_without_subregion_queryset] @@ -41,25 +51,34 @@ def transfer_regions(): else: pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}") - regions_with_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, - cities.country_code_2, cities.subregion_code - FROM cities WHERE - subregion_code IS NOT NULL AND - subregion_code != "" AND - region_code IS NOT NULL AND - region_code != "" AND - country_code_2 IS NOT NULL AND - country_code_2 != "" - AND cities.subregion_code in ( - SELECT region_code FROM cities WHERE - (subregion_code IS NULL OR - subregion_code = "") AND - region_code IS NOT NULL AND - region_code != "" AND - country_code_2 IS NOT NULL AND - country_code_2 != "" - ) - GROUP BY region_code""") + regions_with_subregion_queryset = Cities.objects.raw(""" + SELECT + cities.id, + cities.region_code, + cities.country_code_2, + cities.subregion_code + FROM cities + WHERE subregion_code IS NOT NULL AND + subregion_code != "" AND + region_code IS NOT NULL AND + region_code != "" AND + country_code_2 IS NOT NULL AND + country_code_2 != "" + AND cities.subregion_code in + ( + SELECT region_code FROM cities WHERE + (subregion_code IS NULL OR + subregion_code = "") AND + region_code IS NOT NULL AND + region_code != "" AND + country_code_2 IS NOT NULL AND + country_code_2 != "" + ) + GROUP BY cities.id, + cities.region_code, + cities.country_code_2, + cities.subregion_code + """) regions_with_subregion_queryset = [vars(query) for query in regions_with_subregion_queryset] From 7b4837f4f3a1f551eafb47c103660a48048c2bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Thu, 31 Oct 2019 17:04:25 +0300 Subject: [PATCH 2/5] Add --- .../management/commands/import_collection.py | 8 ++--- .../migrations/0017_collection_old_id.py | 18 ++++++++++ apps/collection/models.py | 1 + .../migrations/0003_auto_20191030_1315.py | 29 ++++++++++++++++ .../migrations/0004_auto_20191031_0923.py | 33 +++++++++++++++++++ .../migrations/0005_auto_20191031_0929.py | 19 +++++++++++ .../migrations/0006_auto_20191031_0930.py | 19 +++++++++++ 7 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 apps/collection/migrations/0017_collection_old_id.py create mode 100644 apps/product/migrations/0003_auto_20191030_1315.py create mode 100644 apps/product/migrations/0004_auto_20191031_0923.py create mode 100644 apps/product/migrations/0005_auto_20191031_0929.py create mode 100644 apps/product/migrations/0006_auto_20191031_0930.py diff --git a/apps/collection/management/commands/import_collection.py b/apps/collection/management/commands/import_collection.py index 7c6d92f8..380e856b 100644 --- a/apps/collection/management/commands/import_collection.py +++ b/apps/collection/management/commands/import_collection.py @@ -68,13 +68,13 @@ class Command(BaseCommand): objects = [] queryset = [vars(query) for query in raw_qs] for obj in queryset: - establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() + # establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() country = Country.objects.filter(code=obj['country_code']).first() objects.append( - Collection(name={"en-GB":obj['title']}, collection_type = Collection.ORDINARY, + Collection(name={"en-GB":obj['title']}, collection_type=Collection.ORDINARY, is_publish=obj['is_publish'], country=country, description=obj['description'], - slug=obj['slug'] + slug=obj['slug'], old_id=obj['old_id'] ) ) - # self.stdout.write(self.style.WARNING(f'Deleted {count} objects.')) + Collection.objects.bulk_create(objects) diff --git a/apps/collection/migrations/0017_collection_old_id.py b/apps/collection/migrations/0017_collection_old_id.py new file mode 100644 index 00000000..11b84c8a --- /dev/null +++ b/apps/collection/migrations/0017_collection_old_id.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.4 on 2019-10-31 13:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('collection', '0016_auto_20191024_1334'), + ] + + operations = [ + migrations.AddField( + model_name='collection', + name='old_id', + field=models.IntegerField(blank=True, null=True), + ), + ] diff --git a/apps/collection/models.py b/apps/collection/models.py index cbcc3842..a051a5c1 100644 --- a/apps/collection/models.py +++ b/apps/collection/models.py @@ -76,6 +76,7 @@ class Collection(ProjectBaseMixin, CollectionDateMixin, slug = models.SlugField(max_length=50, unique=True, verbose_name=_('Collection slug'), editable=True, null=True) + old_id=models.IntegerField(null=True, blank=True) objects = CollectionQuerySet.as_manager() class Meta: diff --git a/apps/product/migrations/0003_auto_20191030_1315.py b/apps/product/migrations/0003_auto_20191030_1315.py new file mode 100644 index 00000000..9aea341e --- /dev/null +++ b/apps/product/migrations/0003_auto_20191030_1315.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2.4 on 2019-10-30 13:15 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0002_product_slug'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='old_id', + field=models.IntegerField(blank=True, null=True, verbose_name='Product old id'), + ), + migrations.AddField( + model_name='product', + name='old_id_establishment', + field=models.IntegerField(blank=True, null=True, verbose_name='Establishment old id'), + ), + migrations.AlterField( + model_name='product', + name='establishment', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='products', to='establishment.Establishment', verbose_name='establishment'), + ), + ] diff --git a/apps/product/migrations/0004_auto_20191031_0923.py b/apps/product/migrations/0004_auto_20191031_0923.py new file mode 100644 index 00000000..65d63782 --- /dev/null +++ b/apps/product/migrations/0004_auto_20191031_0923.py @@ -0,0 +1,33 @@ +# Generated by Django 2.2.4 on 2019-10-31 09:23 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0003_auto_20191030_1315'), + ] + + operations = [ + migrations.RemoveField( + model_name='product', + name='old_id_establishment', + ), + migrations.AlterField( + model_name='product', + name='establishment', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='products', to='establishment.Establishment', verbose_name='establishment'), + ), + migrations.AlterField( + model_name='product', + name='old_id', + field=models.IntegerField(blank=True, null=True), + ), + migrations.AlterField( + model_name='productsubtype', + name='index_name', + field=models.CharField(choices=[('rum', 'Rum'), ('other', 'Other'), ('extra brut', 'extra brut'), ('brut', 'brut'), ('brut nature', 'brut nature'), ('demi-sec', 'demi-sec'), ('Extra Dry', 'Extra Dry'), ('dosage zero', 'dosage zero'), ('sec', 'sec'), ('doux', 'doux'), ('moelleux', 'moelleux')], db_index=True, max_length=50, unique=True, verbose_name='Index name'), + ), + ] diff --git a/apps/product/migrations/0005_auto_20191031_0929.py b/apps/product/migrations/0005_auto_20191031_0929.py new file mode 100644 index 00000000..6dfd4a93 --- /dev/null +++ b/apps/product/migrations/0005_auto_20191031_0929.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.4 on 2019-10-31 09:29 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0004_auto_20191031_0923'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='characteristics', + field=django.contrib.postgres.fields.jsonb.JSONField(null=True, verbose_name='Characteristics'), + ), + ] diff --git a/apps/product/migrations/0006_auto_20191031_0930.py b/apps/product/migrations/0006_auto_20191031_0930.py new file mode 100644 index 00000000..687fa793 --- /dev/null +++ b/apps/product/migrations/0006_auto_20191031_0930.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.4 on 2019-10-31 09:30 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0005_auto_20191031_0929'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='establishment', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='products', to='establishment.Establishment', verbose_name='establishment'), + ), + ] From 1abe280ef7dec737476bd748fe0bd44e0a689c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Thu, 31 Oct 2019 17:51:23 +0300 Subject: [PATCH 3/5] Add unique collection --- .../management/commands/import_collection.py | 54 +++++++++++++------ .../migrations/0007_auto_20191031_1408.py | 29 ++++++++++ .../migrations/0008_auto_20191031_1410.py | 19 +++++++ apps/product/models.py | 3 +- 4 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 apps/product/migrations/0007_auto_20191031_1408.py create mode 100644 apps/product/migrations/0008_auto_20191031_1410.py diff --git a/apps/collection/management/commands/import_collection.py b/apps/collection/management/commands/import_collection.py index 380e856b..f0b7ab8e 100644 --- a/apps/collection/management/commands/import_collection.py +++ b/apps/collection/management/commands/import_collection.py @@ -1,6 +1,6 @@ from django.core.management.base import BaseCommand from establishment.models import Establishment -from location.models import Country +from location.models import Country, Language from transfer.models import Collections from collection.models import Collection from news.models import News @@ -13,22 +13,25 @@ class Command(BaseCommand): raw_qs = Collections.objects.raw(''' select distinct + a.id, a.collection_id, - a.establishment_id, + -- a.establishment_id, a.title, a.tag_name, a.slug, - a.attachment_file_name, - a.attachment_content_type, - a.attachment_file_size, - a.attachment_suffix_url, - active as is_publish,, - a.country_code, - a.geometries, - a.description + -- a.attachment_file_name, + -- a.attachment_content_type, + -- a.attachment_file_size, + -- a.attachment_suffix_url, + -- active as is_publish, + a.country_code, + -- a.geometries, + a.description, + min(a.start) AS start from ( select distinct + c.id, c.id as collection_id, m.establishment_id, c.title, c.tag_name, @@ -38,7 +41,8 @@ class Command(BaseCommand): active, s.country_code_2 as country_code, c.geometries, - c.title as description + c.title as description, + m.created_at as start from collections as c join metadata m on m.value = c.tag_name join establishments e on e.id = m.establishment_id @@ -48,6 +52,7 @@ class Command(BaseCommand): union select distinct + c.id, c.id as collection_id, m.establishment_id, c.title, c.tag_name, @@ -57,24 +62,41 @@ class Command(BaseCommand): active, s.country_code_2 as country_code, c.geometries, - c.title as description + c.title as description, + m.created_at as start from collections as c join metadata m on m.value = c.slug join establishments e on e.id = m.establishment_id join sites s on s.id = c.site_id where m.`key` = 'collection' - ) a + ) a + group by + a.id, + a.collection_id, + -- a.establishment_id, + a.title, + a.tag_name, + a.slug, + -- a.attachment_file_name, + -- a.attachment_content_type, + -- a.attachment_file_size, + -- a.attachment_suffix_url, + -- active as is_publish, + a.country_code, + a.description ''') objects = [] queryset = [vars(query) for query in raw_qs] for obj in queryset: # establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() - country = Country.objects.filter(code=obj['country_code']).first() + lang = Language.objects.filter(locale=obj['country_code']) + country = Country.objects.filter(languages__in=lang).first() objects.append( Collection(name={"en-GB":obj['title']}, collection_type=Collection.ORDINARY, - is_publish=obj['is_publish'], country=country, + country=country, description=obj['description'], - slug=obj['slug'], old_id=obj['old_id'] + slug=obj['slug'], old_id=obj['collection_id'], + start=obj['start'] ) ) Collection.objects.bulk_create(objects) diff --git a/apps/product/migrations/0007_auto_20191031_1408.py b/apps/product/migrations/0007_auto_20191031_1408.py new file mode 100644 index 00000000..2845f395 --- /dev/null +++ b/apps/product/migrations/0007_auto_20191031_1408.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2.4 on 2019-10-31 14:08 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0006_auto_20191031_0930'), + ] + + operations = [ + migrations.RemoveField( + model_name='product', + name='old_id', + ), + migrations.AlterField( + model_name='product', + name='characteristics', + field=django.contrib.postgres.fields.jsonb.JSONField(verbose_name='Characteristics', null=True), + ), + migrations.AlterField( + model_name='product', + name='establishment', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='products', to='establishment.Establishment', verbose_name='establishment'), + ), + ] diff --git a/apps/product/migrations/0008_auto_20191031_1410.py b/apps/product/migrations/0008_auto_20191031_1410.py new file mode 100644 index 00000000..af3d17d6 --- /dev/null +++ b/apps/product/migrations/0008_auto_20191031_1410.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.4 on 2019-10-31 14:10 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0007_auto_20191031_1408'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='characteristics', + field=django.contrib.postgres.fields.jsonb.JSONField(null=True, verbose_name='Characteristics'), + ), + ] diff --git a/apps/product/models.py b/apps/product/models.py index 332a92f6..19d8443c 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -138,7 +138,8 @@ class Product(TranslatedFieldsMixin, BaseAttributes): help_text='{"en-GB":"some text"}') description = TJSONField(_('Description'), null=True, blank=True, default=None, help_text='{"en-GB":"some text"}') - characteristics = JSONField(_('Characteristics')) + #TODO set null=False + characteristics = JSONField(_('Characteristics'), null=True) country = models.ManyToManyField('location.Country', verbose_name=_('Country')) available = models.BooleanField(_('Available'), default=True) From 54f2290bbda60c6f934429f3fd2bf128a98cebfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Fri, 1 Nov 2019 10:16:05 +0300 Subject: [PATCH 4/5] exit --- .../management/commands/import_collection.py | 194 ++++++++++++------ apps/transfer/serializers/location.py | 2 +- 2 files changed, 129 insertions(+), 67 deletions(-) diff --git a/apps/collection/management/commands/import_collection.py b/apps/collection/management/commands/import_collection.py index f0b7ab8e..0b85e7da 100644 --- a/apps/collection/management/commands/import_collection.py +++ b/apps/collection/management/commands/import_collection.py @@ -10,93 +10,155 @@ class Command(BaseCommand): help = 'Import collection' def handle(self, *args, **kwargs): + # raw_qs = Collections.objects.raw(''' + # select + # distinct + # a.id, + # a.collection_id, + # -- a.establishment_id, + # a.title, + # a.tag_name, + # a.slug, + # -- a.attachment_file_name, + # -- a.attachment_content_type, + # -- a.attachment_file_size, + # -- a.attachment_suffix_url, + # -- active as is_publish, + # a.country_code, + # -- a.geometries, + # a.description, + # min(a.start) AS start + # from + # ( + # select distinct + # c.id, + # c.id as collection_id, + # m.establishment_id, + # c.title, c.tag_name, + # c.slug, c.attachment_file_name, + # c.attachment_content_type, c.attachment_file_size, + # c.attachment_suffix_url, + # active, + # s.country_code_2 as country_code, + # c.geometries, + # c.title as description, + # m.created_at as start + # from collections as c + # join metadata m on m.value = c.tag_name + # join establishments e on e.id = m.establishment_id + # join sites s on s.id = c.site_id + # where m.`key` = 'collection' + # + # union + # + # select distinct + # c.id, + # c.id as collection_id, + # m.establishment_id, + # c.title, c.tag_name, + # c.slug, c.attachment_file_name, + # c.attachment_content_type, c.attachment_file_size, + # c.attachment_suffix_url, + # active, + # s.country_code_2 as country_code, + # c.geometries, + # c.title as description, + # m.created_at as start + # from collections as c + # join metadata m on m.value = c.slug + # join establishments e on e.id = m.establishment_id + # join sites s on s.id = c.site_id + # where m.`key` = 'collection' + # ) a + # group by + # a.id, + # a.collection_id, + # -- a.establishment_id, + # a.title, + # a.tag_name, + # a.slug, + # -- a.attachment_file_name, + # -- a.attachment_content_type, + # -- a.attachment_file_size, + # -- a.attachment_suffix_url, + # -- active as is_publish, + # a.country_code, + # a.description + # ''') + # objects = [] + # queryset = [vars(query) for query in raw_qs] + # for obj in queryset: + # # establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() + # lang = Language.objects.filter(locale=obj['country_code']) + # country = Country.objects.filter(languages__in=lang).first() + # if country: + # objects.append( + # Collection(name={"en-GB": obj['title']}, collection_type=Collection.ORDINARY, + # country=country, + # description=obj['description'], + # slug=obj['slug'], old_id=obj['collection_id'], + # start=obj['start'] + # ) + # ) + # Collection.objects.bulk_create(objects) + raw_qs = Collections.objects.raw(''' select distinct a.id, - a.collection_id, - -- a.establishment_id, - a.title, - a.tag_name, - a.slug, - -- a.attachment_file_name, - -- a.attachment_content_type, - -- a.attachment_file_size, - -- a.attachment_suffix_url, - -- active as is_publish, - a.country_code, - -- a.geometries, - a.description, - min(a.start) AS start - from + a.collection_id, + a.establishment_id + from ( select distinct - c.id, - c.id as collection_id, + c.id, + c.id as collection_id, m.establishment_id, - c.title, c.tag_name, - c.slug, c.attachment_file_name, - c.attachment_content_type, c.attachment_file_size, + c.title, c.tag_name, + c.slug, c.attachment_file_name, + c.attachment_content_type, c.attachment_file_size, c.attachment_suffix_url, - active, - s.country_code_2 as country_code, - c.geometries, - c.title as description, - m.created_at as start + active, + s.country_code_2 as country_code, + c.geometries, + c.title as description, + m.created_at as start from collections as c join metadata m on m.value = c.tag_name join establishments e on e.id = m.establishment_id join sites s on s.id = c.site_id where m.`key` = 'collection' - - union - + + union + select distinct - c.id, - c.id as collection_id, + c.id, + c.id as collection_id, m.establishment_id, - c.title, c.tag_name, - c.slug, c.attachment_file_name, - c.attachment_content_type, c.attachment_file_size, + c.title, c.tag_name, + c.slug, c.attachment_file_name, + c.attachment_content_type, c.attachment_file_size, c.attachment_suffix_url, - active, - s.country_code_2 as country_code, - c.geometries, - c.title as description, - m.created_at as start + active, + s.country_code_2 as country_code, + c.geometries, + c.title as description, + m.created_at as start from collections as c join metadata m on m.value = c.slug join establishments e on e.id = m.establishment_id join sites s on s.id = c.site_id where m.`key` = 'collection' ) a - group by - a.id, - a.collection_id, - -- a.establishment_id, - a.title, - a.tag_name, - a.slug, - -- a.attachment_file_name, - -- a.attachment_content_type, - -- a.attachment_file_size, - -- a.attachment_suffix_url, - -- active as is_publish, - a.country_code, - a.description - ''') - objects = [] + ''') + queryset = [vars(query) for query in raw_qs] for obj in queryset: - # establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() - lang = Language.objects.filter(locale=obj['country_code']) - country = Country.objects.filter(languages__in=lang).first() - objects.append( - Collection(name={"en-GB":obj['title']}, collection_type=Collection.ORDINARY, - country=country, - description=obj['description'], - slug=obj['slug'], old_id=obj['collection_id'], - start=obj['start'] - ) - ) - Collection.objects.bulk_create(objects) + print('COLLECT_ID: {}'.format(obj['collection_id'])) + est = Establishment.objects.filter(old_id=obj['establishment_id']) + if est.exists(): + inst = est.first() + collect = Collection.objects.filter(old_id=obj['collection_id']) + for c in collect: + inst.collections.add(c) + inst.save() diff --git a/apps/transfer/serializers/location.py b/apps/transfer/serializers/location.py index d4ea51cc..4e05897d 100644 --- a/apps/transfer/serializers/location.py +++ b/apps/transfer/serializers/location.py @@ -71,7 +71,7 @@ class RegionSerializer(serializers.ModelSerializer): print(data) if "subregion_code" in data and data["subregion_code"] is not None and data["subregion_code"].strip() != "": try: - parent_region = Region.objects.get(code=str(data['region_code'])) + parent_region = Region.objects.filter(code=str(data['region_code'])).first() except Exception as e: raise ValueError(f"Parent region error with {data}: {e}") From dbee524a31058159239a2d249955181868526da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Fri, 1 Nov 2019 10:17:12 +0300 Subject: [PATCH 5/5] Add --- .../management/commands/import_collection.py | 182 +++++++++--------- 1 file changed, 91 insertions(+), 91 deletions(-) diff --git a/apps/collection/management/commands/import_collection.py b/apps/collection/management/commands/import_collection.py index 0b85e7da..c3b5d978 100644 --- a/apps/collection/management/commands/import_collection.py +++ b/apps/collection/management/commands/import_collection.py @@ -10,97 +10,97 @@ class Command(BaseCommand): help = 'Import collection' def handle(self, *args, **kwargs): - # raw_qs = Collections.objects.raw(''' - # select - # distinct - # a.id, - # a.collection_id, - # -- a.establishment_id, - # a.title, - # a.tag_name, - # a.slug, - # -- a.attachment_file_name, - # -- a.attachment_content_type, - # -- a.attachment_file_size, - # -- a.attachment_suffix_url, - # -- active as is_publish, - # a.country_code, - # -- a.geometries, - # a.description, - # min(a.start) AS start - # from - # ( - # select distinct - # c.id, - # c.id as collection_id, - # m.establishment_id, - # c.title, c.tag_name, - # c.slug, c.attachment_file_name, - # c.attachment_content_type, c.attachment_file_size, - # c.attachment_suffix_url, - # active, - # s.country_code_2 as country_code, - # c.geometries, - # c.title as description, - # m.created_at as start - # from collections as c - # join metadata m on m.value = c.tag_name - # join establishments e on e.id = m.establishment_id - # join sites s on s.id = c.site_id - # where m.`key` = 'collection' - # - # union - # - # select distinct - # c.id, - # c.id as collection_id, - # m.establishment_id, - # c.title, c.tag_name, - # c.slug, c.attachment_file_name, - # c.attachment_content_type, c.attachment_file_size, - # c.attachment_suffix_url, - # active, - # s.country_code_2 as country_code, - # c.geometries, - # c.title as description, - # m.created_at as start - # from collections as c - # join metadata m on m.value = c.slug - # join establishments e on e.id = m.establishment_id - # join sites s on s.id = c.site_id - # where m.`key` = 'collection' - # ) a - # group by - # a.id, - # a.collection_id, - # -- a.establishment_id, - # a.title, - # a.tag_name, - # a.slug, - # -- a.attachment_file_name, - # -- a.attachment_content_type, - # -- a.attachment_file_size, - # -- a.attachment_suffix_url, - # -- active as is_publish, - # a.country_code, - # a.description - # ''') - # objects = [] - # queryset = [vars(query) for query in raw_qs] - # for obj in queryset: - # # establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() - # lang = Language.objects.filter(locale=obj['country_code']) - # country = Country.objects.filter(languages__in=lang).first() - # if country: - # objects.append( - # Collection(name={"en-GB": obj['title']}, collection_type=Collection.ORDINARY, - # country=country, - # description=obj['description'], - # slug=obj['slug'], old_id=obj['collection_id'], - # start=obj['start'] - # ) - # ) - # Collection.objects.bulk_create(objects) + raw_qs = Collections.objects.raw(''' + select + distinct + a.id, + a.collection_id, + -- a.establishment_id, + a.title, + a.tag_name, + a.slug, + -- a.attachment_file_name, + -- a.attachment_content_type, + -- a.attachment_file_size, + -- a.attachment_suffix_url, + -- active as is_publish, + a.country_code, + -- a.geometries, + a.description, + min(a.start) AS start + from + ( + select distinct + c.id, + c.id as collection_id, + m.establishment_id, + c.title, c.tag_name, + c.slug, c.attachment_file_name, + c.attachment_content_type, c.attachment_file_size, + c.attachment_suffix_url, + active, + s.country_code_2 as country_code, + c.geometries, + c.title as description, + m.created_at as start + from collections as c + join metadata m on m.value = c.tag_name + join establishments e on e.id = m.establishment_id + join sites s on s.id = c.site_id + where m.`key` = 'collection' + + union + + select distinct + c.id, + c.id as collection_id, + m.establishment_id, + c.title, c.tag_name, + c.slug, c.attachment_file_name, + c.attachment_content_type, c.attachment_file_size, + c.attachment_suffix_url, + active, + s.country_code_2 as country_code, + c.geometries, + c.title as description, + m.created_at as start + from collections as c + join metadata m on m.value = c.slug + join establishments e on e.id = m.establishment_id + join sites s on s.id = c.site_id + where m.`key` = 'collection' + ) a + group by + a.id, + a.collection_id, + -- a.establishment_id, + a.title, + a.tag_name, + a.slug, + -- a.attachment_file_name, + -- a.attachment_content_type, + -- a.attachment_file_size, + -- a.attachment_suffix_url, + -- active as is_publish, + a.country_code, + a.description + ''') + objects = [] + queryset = [vars(query) for query in raw_qs] + for obj in queryset: + # establishment = Establishment.objects.filter(old_id=obj['establishment_id']).first() + lang = Language.objects.filter(locale=obj['country_code']) + country = Country.objects.filter(languages__in=lang).first() + if country: + objects.append( + Collection(name={"en-GB": obj['title']}, collection_type=Collection.ORDINARY, + country=country, + description=obj['description'], + slug=obj['slug'], old_id=obj['collection_id'], + start=obj['start'] + ) + ) + Collection.objects.bulk_create(objects) raw_qs = Collections.objects.raw(''' select