csvdatasource: Query slug unicity (#39647)
This commit is contained in:
parent
a174bc24eb
commit
11214a4ef4
|
@ -0,0 +1,36 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def generate_slug(instance):
|
||||
slug = instance.slug
|
||||
i = 1
|
||||
while True:
|
||||
queryset = instance._meta.model.objects.filter(slug=slug, resource=instance.resource).exclude(pk=instance.pk)
|
||||
if not queryset.exists():
|
||||
break
|
||||
slug = '%s-%s' % (instance.slug, i)
|
||||
i += 1
|
||||
return slug
|
||||
|
||||
|
||||
def set_slug(apps, schema_editor):
|
||||
Query = apps.get_model('csvdatasource', 'Query')
|
||||
for query in Query.objects.all().order_by('-pk'):
|
||||
if not Query.objects.filter(slug=query.slug, resource=query.resource).exclude(pk=query.pk).exists():
|
||||
continue
|
||||
query.slug = generate_slug(query)
|
||||
query.save(update_fields=['slug'])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('csvdatasource', '0013_auto_20181118_0807'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(set_slug, lambda x, y: None),
|
||||
]
|
|
@ -0,0 +1,18 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('csvdatasource', '0014_query_set_slug'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name='query',
|
||||
unique_together=set([('resource', 'slug')]),
|
||||
),
|
||||
]
|
|
@ -96,6 +96,7 @@ class Query(models.Model):
|
|||
|
||||
class Meta:
|
||||
ordering = ['slug']
|
||||
unique_together = ['resource', 'slug']
|
||||
|
||||
def get_list(self, attribute):
|
||||
if not getattr(self, attribute):
|
||||
|
|
Loading…
Reference in New Issue