<filename>cpdb/data/migrations/0064_set_allegation_areas_lineareas.py
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2018-07-12 03:29
from __future__ import unicode_literals
from datetime import datetime

from django.db import migrations
from django.contrib.gis.measure import D

import pytz


def alter_data(apps, schema_editor):
    Allegation = apps.get_model('data', 'Allegation')
    Area = apps.get_model('data', 'Area')
    LineArea = apps.get_model('data', 'LineArea')
    distance = D(m=10)

    for allegation in Allegation.objects.filter(incident_date__lt=datetime(1988, 1, 1, tzinfo=pytz.utc)):
        if allegation.point is None:
            allegation.areas.set([])
            allegation.line_areas.set([])
        else:
            allegation.areas.set(Area.objects.filter(polygon__covers=allegation.point))
            allegation.line_areas.set(LineArea.objects.filter(geom__distance_lte=(allegation.point, distance)))


class Migration(migrations.Migration):

    dependencies = [
        ('data', '0063_import_old_address_for_allegation_before_1988'),
    ]

    operations = [
        migrations.RunPython(
            alter_data,
            reverse_code=migrations.RunPython.noop,
            elidable=True),
    ]

<filename>cpdb/data/migrations/0064_set_allegation_areas_lineareas.py
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2018-07-12 03:29
from __future__ import unicode_literals
from datetime import datetime

from django.db import migrations
from django.contrib.gis.measure import D

import pytz


def alter_data(apps, schema_editor):
    Allegation = apps.get_model('data', 'Allegation')
    Area = apps.get_model('data', 'Area')
    LineArea = apps.get_model('data', 'LineArea')
    distance = D(m=10)

    for allegation in Allegation.objects.filter(incident_date__lt=datetime(1988, 1, 1, tzinfo=pytz.utc)):
        if allegation.point is None:
            allegation.areas.set([])
            allegation.line_areas.set([])
        else:
            allegation.areas.set(Area.objects.filter(polygon__covers=allegation.point))
            allegation.line_areas.set(LineArea.objects.filter(geom__distance_lte=(allegation.point, distance)))


class Migration(migrations.Migration):

    dependencies = [
        ('data', '0063_import_old_address_for_allegation_before_1988'),
    ]

    operations = [
        migrations.RunPython(
            alter_data,
            reverse_code=migrations.RunPython.noop,
            elidable=True),
    ]

# -*- coding: utf-8 -*- # Generated by Django 1.11.4 on 2018-07-12 03:29