0001_initial.py 10.2 KB
Newer Older
luxcem's avatar
luxcem committed
1 2 3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

Nicolas Joyard's avatar
Nicolas Joyard committed
4 5 6
import os

from django.core import serializers
7
from django.db import migrations, models
luxcem's avatar
luxcem committed
8 9


Nicolas Joyard's avatar
Nicolas Joyard committed
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fixture_dir = os.path.abspath(
    os.path.join(
        os.path.dirname(__file__),
        '../fixtures'))
fixture_filename = 'country_initial_data.json'


def load_fixture(apps, schema_editor):
    fixture_file = os.path.join(fixture_dir, fixture_filename)

    fixture = open(fixture_file, 'rb')
    objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
    for obj in objects:
        obj.save()
    fixture.close()


def unload_fixture(apps, schema_editor):
    "Brutally deleting all entries for this model..."

    MyModel = apps.get_model("representatives", "Country")
    MyModel.objects.all().delete()


def create_ep_chamber(apps, schema_editor):
    name = 'European Parliament'
    abbr = 'EP'

    Chamber = apps.get_model("representatives", "Chamber")
    Constituency = apps.get_model("representatives", "Constituency")
    Group = apps.get_model("representatives", "Group")

    ep_chamber, _ = Chamber.objects.get_or_create(name=name, abbreviation=abbr)
    ep_constituency, _ = Constituency.objects.get_or_create(name=name)
    ep_group, _ = Group.objects.get_or_create(name=name, kind='chamber',
        abbreviation=abbr, chamber=ep_chamber)

    ep_chamber.save()
    ep_constituency.save()
    ep_group.save()


luxcem's avatar
luxcem committed
52 53 54 55 56 57
class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
Nicolas Joyard's avatar
Nicolas Joyard committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        migrations.CreateModel(
            name='Address',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('city', models.CharField(default=b'', max_length=255, blank=True)),
                ('street', models.CharField(default=b'', max_length=255, blank=True)),
                ('number', models.CharField(default=b'', max_length=255, blank=True)),
                ('postcode', models.CharField(default=b'', max_length=255, blank=True)),
                ('floor', models.CharField(default=b'', max_length=255, blank=True)),
                ('office_number', models.CharField(default=b'', max_length=255, blank=True)),
                ('kind', models.CharField(default=b'', max_length=255, blank=True)),
                ('name', models.CharField(default=b'', max_length=255, blank=True)),
                ('location', models.CharField(default=b'', max_length=255, blank=True)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='Chamber',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('abbreviation', models.CharField(default=b'', max_length=10, db_index=True, blank=True)),
            ],
        ),
        migrations.CreateModel(
            name='Constituency',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('name', models.CharField(max_length=255)),
            ],
            options={
                'abstract': False,
            },
        ),
luxcem's avatar
luxcem committed
98 99 100
        migrations.CreateModel(
            name='Country',
            fields=[
Nicolas Joyard's avatar
Nicolas Joyard committed
101 102 103 104 105 106 107 108 109 110 111 112 113
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('code', models.CharField(unique=True, max_length=2)),
            ],
        ),
        migrations.CreateModel(
            name='Email',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('email', models.EmailField(max_length=254)),
                ('kind', models.CharField(default=b'', max_length=255, blank=True)),
luxcem's avatar
luxcem committed
114
            ],
Nicolas Joyard's avatar
Nicolas Joyard committed
115 116 117
            options={
                'abstract': False,
            },
luxcem's avatar
luxcem committed
118
        ),
Nicolas Joyard's avatar
Nicolas Joyard committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        migrations.CreateModel(
            name='Group',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('name', models.CharField(max_length=511, db_index=True)),
                ('abbreviation', models.CharField(default=b'', max_length=10, db_index=True, blank=True)),
                ('kind', models.CharField(max_length=255, db_index=True)),
                ('chamber', models.ForeignKey(related_name='groups', to='representatives.Chamber', null=True)),
            ],
            options={
                'ordering': ('name',),
            },
        ),
        migrations.CreateModel(
            name='Mandate',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('role', models.CharField(default=b'', help_text=b'Eg.: president of a political group', max_length=255, blank=True)),
                ('begin_date', models.DateField(null=True, blank=True)),
                ('end_date', models.DateField(null=True, blank=True)),
                ('link', models.URLField()),
                ('constituency', models.ForeignKey(related_name='mandates', to='representatives.Constituency', null=True)),
                ('group', models.ForeignKey(related_name='mandates', to='representatives.Group', null=True)),
            ],
            options={
                'ordering': ('-end_date',),
            },
        ),
        migrations.CreateModel(
            name='Phone',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('number', models.CharField(default=b'', max_length=255, blank=True)),
                ('kind', models.CharField(default=b'', max_length=255, blank=True)),
                ('address', models.ForeignKey(related_name='phones', to='representatives.Address', null=True)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='Representative',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('slug', models.SlugField(unique=True, max_length=100)),
                ('first_name', models.CharField(default=b'', max_length=255, blank=True)),
                ('last_name', models.CharField(default=b'', max_length=255, blank=True)),
                ('full_name', models.CharField(max_length=255)),
                ('gender', models.SmallIntegerField(default=0, choices=[(0, b'N/A'), (1, b'F'), (2, b'M')])),
                ('birth_place', models.CharField(default=b'', max_length=255, blank=True)),
                ('birth_date', models.DateField(null=True, blank=True)),
                ('cv', models.TextField(default=b'', blank=True)),
                ('photo', models.CharField(max_length=512, null=True)),
                ('active', models.BooleanField(default=False)),
            ],
            options={
                'ordering': ['last_name', 'first_name'],
            },
        ),
        migrations.CreateModel(
            name='WebSite',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('url', models.CharField(default=b'', max_length=2048, blank=True)),
                ('kind', models.CharField(default=b'', max_length=255, blank=True)),
                ('representative', models.ForeignKey(to='representatives.Representative')),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='phone',
            name='representative',
            field=models.ForeignKey(to='representatives.Representative'),
        ),
        migrations.AddField(
            model_name='mandate',
            name='representative',
            field=models.ForeignKey(related_name='mandates', to='representatives.Representative'),
        ),
        migrations.AddField(
            model_name='email',
            name='representative',
            field=models.ForeignKey(to='representatives.Representative'),
        ),
        migrations.AddField(
            model_name='constituency',
            name='country',
            field=models.ForeignKey(related_name='constituencies', blank=True, to='representatives.Country', null=True),
        ),
        migrations.AddField(
            model_name='chamber',
            name='country',
            field=models.ForeignKey(related_name='chambers', to='representatives.Country', null=True),
        ),
        migrations.AddField(
            model_name='address',
            name='country',
            field=models.ForeignKey(to='representatives.Country'),
        ),
        migrations.AddField(
            model_name='address',
            name='representative',
            field=models.ForeignKey(to='representatives.Representative'),
        ),

        migrations.RunPython(load_fixture, reverse_code=unload_fixture),

        migrations.RunPython(create_ep_chamber),
luxcem's avatar
luxcem committed
239
    ]