# Generated by Django 3.1.5 on 2021-01-12 19:56
import os
import random
import socket
import struct
from datetime import date

from django.db import migrations, transaction

from vulnerability_app.models import Url, UrlType


def read_from_files():
    directory = "vulnerability_app/data"
    url_list = []
    url_types = []
    for filename in os.listdir(directory):
        url_type = filename.split("_")[0]
        url_types.append(url_type)
        with open(os.path.join(directory, filename), "r", encoding="utf-8") as f:
            urls = [line.rstrip() for line in f.readlines()]
            for url in urls:
                url_list.append((url, url_type))

    return url_types, url_list


def populate_urls(apps, schema_editor):
    # add the URL types
    urls_types, urls = read_from_files()
    url_types_mapping = dict()

    with transaction.atomic():
        for url_type in urls_types:
            new_url_type = UrlType(name=url_type)
            new_url_type.save()
            url_types_mapping[new_url_type.name] = new_url_type

    # add the full URLs
        result = []
        for url_path, url_type in urls:
            new_object = Url(path=url_path,
                             accessed_on=date(day=random.randrange(1, 30), month=random.randrange(1, 13), year=2020),
                             ip=socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))),
                             url_type=url_types_mapping[url_type]
                             )
            result.append(new_object)
    Url.objects.bulk_create(result)


class Migration(migrations.Migration):
    atomic = False

    dependencies = [
        ('vulnerability_app', '0002_safedomain_url_urltype'),
    ]

    operations = [
        migrations.RunPython(populate_urls),
    ]
