# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2022-04-04 14:15
from __future__ import unicode_literals
from django.db import migrations, models
import hs_core.models
import re
from itertools import chain


def migrate_url_to_uid(apps, schema_editor):
    Contributor = apps.get_model('hs_core.Contributor')
    Creator = apps.get_model('hs_core.Creator')
    combined_list = list(chain(Contributor.objects.all(), Creator.objects.all()))
    for party in combined_list:
        if party.description:
            id = re.sub("[^0-9]", "", party.description)
            party.hydroshare_user_id = id
            party.save()


class Migration(migrations.Migration):

    dependencies = [
        ('hs_core', '0060_auto_20220202_1732'),
    ]

    operations = [
        migrations.AddField(
            model_name='contributor',
            name='hydroshare_user_id',
            field=models.IntegerField(blank=True, null=True, validators=[hs_core.models.validate_hydroshare_user_id]),
        ),
        migrations.AddField(
            model_name='creator',
            name='hydroshare_user_id',
            field=models.IntegerField(blank=True, null=True, validators=[hs_core.models.validate_hydroshare_user_id]),
        ),
        migrations.RunPython(migrate_url_to_uid),
        migrations.RemoveField(
            model_name='contributor',
            name='description',
        ),
        migrations.RemoveField(
            model_name='creator',
            name='description',
        ),
    ]
