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]