This commit is contained in:
Виктор Гладких 2019-10-31 16:57:12 +03:00
parent 6c8deb5f80
commit 989e80ece6
5 changed files with 134 additions and 50 deletions

View File

View File

@ -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.'))

View File

@ -27,22 +27,7 @@ card = {
# "country": "Country", # "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", ) used_apps = ("location", )

View File

@ -6,11 +6,13 @@ from pprint import pprint
def transfer_countries(): def transfer_countries():
queryset = Cities.objects.raw("""SELECT cities.id, cities.country_code_2 queryset = Cities.objects.raw("""
FROM cities WHERE SELECT cities.id, cities.country_code_2
country_code_2 IS NOT NULL AND FROM cities
country_code_2 != "" WHERE country_code_2 IS NOT NULL AND
GROUP BY cities.country_code_2""") country_code_2 != ""
GROUP BY cities.id, cities.country_code_2
""")
queryset = [vars(query) for query in queryset] queryset = [vars(query) for query in queryset]
@ -22,16 +24,24 @@ def transfer_countries():
def transfer_regions(): def transfer_regions():
regions_without_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, regions_without_subregion_queryset = Cities.objects.raw("""
cities.country_code_2, cities.subregion_code SELECT cities.id,
FROM cities WHERE cities.region_code,
(subregion_code IS NULL OR cities.country_code_2,
subregion_code = "") AND cities.subregion_code
region_code IS NOT NULL AND FROM cities
region_code != "" AND WHERE (subregion_code IS NULL
country_code_2 IS NOT NULL AND OR subregion_code = ""
country_code_2 != "" )
GROUP BY region_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] regions_without_subregion_queryset = [vars(query) for query in regions_without_subregion_queryset]
@ -41,25 +51,34 @@ def transfer_regions():
else: else:
pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}") pprint(f"Parent regions serializer errors: {serialized_without_subregion.errors}")
regions_with_subregion_queryset = Cities.objects.raw("""SELECT cities.id, cities.region_code, regions_with_subregion_queryset = Cities.objects.raw("""
cities.country_code_2, cities.subregion_code SELECT
FROM cities WHERE cities.id,
subregion_code IS NOT NULL AND cities.region_code,
subregion_code != "" AND cities.country_code_2,
region_code IS NOT NULL AND cities.subregion_code
region_code != "" AND FROM cities
country_code_2 IS NOT NULL AND WHERE subregion_code IS NOT NULL AND
country_code_2 != "" subregion_code != "" AND
AND cities.subregion_code in ( region_code IS NOT NULL AND
SELECT region_code FROM cities WHERE region_code != "" AND
(subregion_code IS NULL OR country_code_2 IS NOT NULL AND
subregion_code = "") AND country_code_2 != ""
region_code IS NOT NULL AND AND cities.subregion_code in
region_code != "" AND (
country_code_2 IS NOT NULL AND SELECT region_code FROM cities WHERE
country_code_2 != "" (subregion_code IS NULL OR
) subregion_code = "") AND
GROUP BY region_code""") 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] regions_with_subregion_queryset = [vars(query) for query in regions_with_subregion_queryset]