from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from django.db.models import signals
from optparse import make_option

import json
import pprint

from taiga.projects.models import Project
from taiga.export_import.renderers import ExportRenderer
from taiga.export_import.dump_service import dict_to_project, TaigaImportError
from taiga.export_import.service import get_errors

class Command(BaseCommand):
    args = '<dump_file> <owner-email>'
    help = 'Export a project to json'
    renderer_context = {"indent": 4}
    renderer = ExportRenderer()
    option_list = BaseCommand.option_list + (
        make_option('--overwrite',
            action='store_true',
            dest='overwrite',
            default=False,
            help='Delete project if exists'),
        )

    def handle(self, *args, **options):
        data = json.loads(open(args[0], 'r').read())
        try:
            with transaction.atomic():
                if options["overwrite"]:
                    receivers_back = signals.post_delete.receivers
                    signals.post_delete.receivers = []
                    try:
                        proj = Project.objects.get(slug=data.get("slug", "not a slug"))
                        proj.tasks.all().delete()
                        proj.user_stories.all().delete()
                        proj.issues.all().delete()
                        proj.memberships.all().delete()
                        proj.roles.all().delete()
                        proj.delete()
                    except Project.DoesNotExist:
                        pass
                    signals.post_delete.receivers = receivers_back
                dict_to_project(data, args[1])
        except TaigaImportError as e:
            print("ERROR:", end=" ")
            print(e.message)
            print(get_errors())

@@ -1,20 +1,840 @@
+# Copyright (C) 2014 Andrey Antukh %3Cniwi@niwi.be%3E%0A# Copyright (C) 2014 Jes%C3%BAs Espino %3Cjespinog@gmail.com%3E%0A# Copyright (C) 2014 David Barrag%C3%A1n %3Cbameda@dbarragan.com%3E%0A# This program is free software: you can redistribute it and/or modify%0A# it under the terms of the GNU Affero General Public License as%0A# published by the Free Software Foundation, either version 3 of the%0A# License, or (at your option) any later version.%0A#%0A# This program is distributed in the hope that it will be useful,%0A# but WITHOUT ANY WARRANTY; without even the implied warranty of%0A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A# GNU Affero General Public License for more details.%0A#%0A# You should have received a copy of the GNU Affero General Public License%0A# along with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A%0A
 from django.core.man

Add license