# Generated by Django 2.2 on 2019-10-15 20:09

from django.db import migrations, models
import uuid
from django.core.files import File
from urllib.request import urlretrieve
from urllib.error import HTTPError
import os


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0003_auto_20191015_1305'),
    ]

    def migrate_data(apps, schema_editor):
        User = apps.get_model('users', 'User')
        users = User.objects.all()
        for user in users:
            if user.profile_photo.name is None or user.profile_photo.name is '':
                try:
                    url = 'https://backend.jobhax.com/media/981fa136-adc0-4b26-89bf-c1c4bb56af6f.png'
                    urlretrieve(url, filename=url.split('/')[-1])
                    file = open(url.split('/')[-1], 'rb')
                    filename = "%s.%s" % (uuid.uuid4(), 'jpg')
                    user.profile_photo.save(filename, File(file), save=True)
                    user.save()
                    os.remove(url.split('/')[-1])
                except FileNotFoundError as err:
                    pass  # something wrong with local path
                except HTTPError as err:
                    pass  # something wrong with url

    operations = [
        migrations.RunPython(migrate_data, reverse_code=migrations.RunPython.noop),
    ]
