0029_source.py 2.29 KB
Newer Older
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
1
2
3
# Generated by Django 3.1.7 on 2021-04-12 10:07

from django.db import migrations, models
4
import django.db.models.deletion
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
5
6


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def create_sources(apps, schema_editor):
    Source = apps.get_model('catalog', 'Source')
    items_to_create = [
        {
            'name': 'Virgo',
            'url': 'https://virgo.igs.umaryland.edu/',
            'doi': '10.1038/s41467-020-14677-3'
        },
        {
            'name': 'IGC-9.9M',
            'url': 'https://db.cngb.org/microbiome/genecatalog/genecatalog_human/',
            'doi': '10.1038/s41467-020-14677-3'
        },
    ]
    for item in items_to_create:
        new_entry = Source(**item)
        new_entry.save()


def link_to_source(apps, schema_editor):
    Gene = apps.get_model('catalog', 'Gene')
    Source = apps.get_model('catalog', 'Source')
    mapping_source = {
        'igc': Source.objects.get(name='IGC-9.9M'),
        'virgo': Source.objects.get(name='Virgo')
    }
    for row in Gene.objects.all():
        if row.source != 'undef':
            row.new_source = mapping_source.get(row.source)
            row.save()

Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Migration(migrations.Migration):

    dependencies = [
        ('catalog', '0028_index_source_and_functions'),
    ]

    operations = [
        migrations.CreateModel(
            name='Source',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(db_index=True, max_length=100, unique=True)),
                ('url', models.CharField(blank=True, max_length=100)),
                ('doi', models.CharField(blank=True, max_length=100)),
                ('tools', models.JSONField(blank=True, null=True)),
            ],
        ),
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        migrations.RunPython(create_sources),
        migrations.AddField(
            model_name='gene',
            name='new_source',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='genes', to='catalog.source'),
        ),
        migrations.RunPython(link_to_source),
        migrations.RemoveField(
            model_name='gene',
            name='source',
        ),
        migrations.RenameField(
            model_name='gene',
            old_name='new_source',
            new_name='source',
        ),
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
71
    ]