gault-millau/apps/collection/management/commands/import_collection.py
Виктор Гладких 989e80ece6 First
2019-10-31 16:57:12 +03:00

81 lines
4.9 KiB
Python

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