Add unique collection

This commit is contained in:
Виктор Гладких 2019-10-31 17:51:23 +03:00
parent 7b4837f4f3
commit 1abe280ef7
4 changed files with 88 additions and 17 deletions

View File

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

View File

@ -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'),
),
]

View File

@ -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'),
),
]

View File

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