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] 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}")