import json

from django.core.management.base import BaseCommand
from django.db import transaction

from epdb.logic import PackageManager
from epdb.models import User


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument(
            "--data",
            type=str,
            help="Path of the Package to import.",
            required=True,
        )
        parser.add_argument(
            "--owner",
            type=str,
            help="Username of the desired Owner.",
            required=True,
        )

    @transaction.atomic
    def handle(self, *args, **options):
        owner = User.objects.get(username=options["owner"])
        package_data = json.load(open(options["data"]))
        PackageManager.import_legacy_package(package_data, owner)
