# Path: cpm_generic/migration_utils.py
# def get_image_model(apps):
#     """ Return Image model that works in migrations.
# 
#     Models created by Django migration subsystem contain fields,
#     but dont' contain methods of the original models.
# 
#     The hack with adding method get_upload_to lets us save images in
#     migrations, e.g.:
#     >>> from django.core.files import File
#     >>> Image = get_image_model()
#     >>> photo = Image(title='example title')
#     >>> with open(path) as f:
#     ...     photo.file.save(name='name.png', content=File(f))
# 
#     Args:
#         apps (django.db.migrations.state.StateApps): Apps registry.
# 
#     Returns:
#         type: Image model.
#     """
#     from wagtail.wagtailimages.models import Image as LatestImage
# 
#     Image = apps.get_model('wagtailimages.Image')
#     Image.get_upload_to = LatestImage.get_upload_to.im_func
# 
#     return Image


from functools import partial
from django.core.files import File
from django.db import migrations
from cpm_generic.migration_utils import get_image_model
import json
import os

def _get_season(apps, season_name):
    Season = apps.get_model('cpm_data.Season')
    return Season.objects.get(name_en=season_name)


def _get_data(season_year):
    with open(os.path.join(
            DIR_0011,
            'jury_' + season_year + '.json'
    )) as data_file:
        return json.load(data_file)


def add_jurymembers(apps, schema_editor, season_name):
    season = _get_season(apps, season_name=season_name)
    JuryMember = apps.get_model('cpm_data.JuryMember')
    Image = get_image_model(apps)
    Collection = apps.get_model('wagtailcore.Collection')

    collection_id = Collection.objects.filter(depth=1)[0]

    for item in _get_data(season_name):
        photo = Image(title=item['name_en'], collection=collection_id)

        with open(_get_photo_path(item['photo']), 'rb') as photo_file:
            photo.file.save(name=item['photo'], content=File(photo_file))
            photo.save()

        jurymember, _created = JuryMember.objects.get_or_create(

# Path: cpm_generic/migration_utils.py
# def get_image_model(apps):
#     """ Return Image model that works in migrations.
# 
#     Models created by Django migration subsystem contain fields,
#     but dont' contain methods of the original models.
# 
#     The hack with adding method get_upload_to lets us save images in
#     migrations, e.g.:
#     >>> from django.core.files import File
#     >>> Image = get_image_model()
#     >>> photo = Image(title='example title')
#     >>> with open(path) as f:
#     ...     photo.file.save(name='name.png', content=File(f))
# 
#     Args:
#         apps (django.db.migrations.state.StateApps): Apps registry.
# 
#     Returns:
#         type: Image model.
#     """
#     from wagtail.wagtailimages.models import Image as LatestImage
# 
#     Image = apps.get_model('wagtailimages.Image')
#     Image.get_upload_to = LatestImage.get_upload_to.im_func
# 
#     return Image


# Path: cpm_data/migrations/0011_add_jury_data_to_season.py
from functools import partial
from django.core.files import File
from django.db import migrations
from cpm_generic.migration_utils import get_image_model
import json
import os


def _get_season(apps, season_name):
    Season = apps.get_model('cpm_data.Season')
    return Season.objects.get(name_en=season_name)


def _get_data(season_year):
    with open(os.path.join(
            DIR_0011,
            'jury_' + season_year + '.json'
    )) as data_file:
        return json.load(data_file)


def add_jurymembers(apps, schema_editor, season_name):
    season = _get_season(apps, season_name=season_name)
    JuryMember = apps.get_model('cpm_data.JuryMember')
    Image = get_image_model(apps)
    Collection = apps.get_model('wagtailcore.Collection')

    collection_id = Collection.objects.filter(depth=1)[0]

    for item in _get_data(season_name):
        photo = Image(title=item['name_en'], collection=collection_id)

        with open(_get_photo_path(item['photo']), 'rb') as photo_file:
            photo.file.save(name=item['photo'], content=File(photo_file))
            photo.save()

        jurymember, _created = JuryMember.objects.get_or_create(

            name_en=item['name_en'],