Completed
Push — master ( 9de0cc...4056a6 )
by Kirill
11s
created

delete_duplicated_os()   B

Complexity

Conditions 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
dl 0
loc 21
rs 7.8867
c 1
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.9.6 on 2016-07-07 07:20
3
from __future__ import unicode_literals
4
5
from django.db import migrations
6
7
8
def delete_duplicated_os(apps, schema_editor):
9
    Os = apps.get_model("omaha", "Os")
10
    Request = apps.get_model("omaha", "Request")
11
    list_args = Os.objects.all().values('platform', 'version', 'sp', 'arch')
12
    not_dup_args = []
13
    dup_args = []
14
15
    for args in list_args:
16
        if args in not_dup_args:
17
            if args not in dup_args:
18
                dup_args.append(args)
19
        else:
20
            not_dup_args.append(args)
21
22
    print "\nDuplicated arguments: %s" % dup_args
23
    for args in dup_args:
24
        dups = list(Os.objects.filter(**args))
25
        original = dups[0]
26
        for os in dups[1:]:
27
            Request.objects.filter(os=os.id).update(os=original.id)
28
            os.delete()
29
30
31
class Migration(migrations.Migration):
32
33
    dependencies = [
34
        ('omaha', '0026_grant_permission_django_site'),
35
    ]
36
37
    operations = [
38
        migrations.RunPython(delete_duplicated_os, reverse_code=migrations.RunPython.noop),
39
    ]
40