Update statistics migration to rename duplicates

Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-11-22 21:23:35 -04:00
parent 88ab66749e
commit 6d94227224
2 changed files with 28 additions and 2 deletions

View File

@@ -12,6 +12,8 @@
- Self-heal statistics results model when multiple
results are created using the same slug value.
Forum topic 1404.
- Update mayan_statistics migration 0002 to rename
duplicate slugs.
3.2.10 (2019-11-19)
===================

View File

@@ -1,10 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.25 on 2019-11-16 02:36
from __future__ import unicode_literals
from django.db import migrations, models
def operation_rename_duplicates(apps, schema_editor):
StatisticResult = apps.get_model(
app_label='mayan_statistics', model_name='StatisticResult'
)
slugs = []
for entry in StatisticResult.objects.using(schema_editor.connection.alias).all():
if entry.slug in slugs:
counter = 1
while(True):
attempt = '{}_{}'.format(entry.slug, counter)
if attempt not in slugs:
break
else:
counter = counter + 1
entry.slug = attempt
entry.save()
slugs.append(entry.slug)
class Migration(migrations.Migration):
dependencies = [
@@ -12,6 +32,10 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunPython(
code=operation_rename_duplicates,
reverse_code=migrations.RunPython.noop
),
migrations.AlterField(
model_name='statisticresult',
name='slug',