From 773ab631064a155983fe173a5d45af336472aa3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 20 Jun 2016 12:24:34 +0200 Subject: [PATCH] Adding dump_project_async command --- .../management/commands/dump_project_async.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 taiga/export_import/management/commands/dump_project_async.py diff --git a/taiga/export_import/management/commands/dump_project_async.py b/taiga/export_import/management/commands/dump_project_async.py new file mode 100644 index 00000000..d48a0c19 --- /dev/null +++ b/taiga/export_import/management/commands/dump_project_async.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2014-2016 Andrey Antukh +# Copyright (C) 2014-2016 Jesús Espino +# Copyright (C) 2014-2016 David Barragán +# Copyright (C) 2014-2016 Alejandro Alonso +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.core.management.base import BaseCommand, CommandError +from django.db.models import Q +from django.conf import settings + +from taiga.projects.models import Project +from taiga.users.models import User +from taiga.permissions.services import is_project_admin +from taiga.export_import import tasks + + +class Command(BaseCommand): + help = "Export projects to a json file" + + def add_arguments(self, parser): + parser.add_argument("project_slugs", + nargs="+", + help="") + + parser.add_argument("-u", "--user", + action="store", + dest="user", + default="./", + metavar="DIR", + required=True, + help="Dump as user by email or username.") + + parser.add_argument("-f", "--format", + action="store", + dest="format", + default="plain", + metavar="[plain|gzip]", + help="Format to the output file plain json or gzipped json. ('plain' by default)") + + def handle(self, *args, **options): + username_or_email = options["user"] + dump_format = options["format"] + project_slugs = options["project_slugs"] + + try: + user = User.objects.get(Q(username=username_or_email) | Q(email=username_or_email)) + except Exception: + raise CommandError("Error loading user".format(username_or_email)) + + for project_slug in project_slugs: + try: + project = Project.objects.get(slug=project_slug) + except Project.DoesNotExist: + raise CommandError("Project '{}' does not exist".format(project_slug)) + + if not is_project_admin(user, project): + self.stderr.write(self.style.ERROR( + "ERROR: Not sending task because user '{}' doesn't have permissions to export '{}' project".format( + username_or_email, + project_slug + ) + )) + continue + + task = tasks.dump_project.delay(user, project, dump_format) + tasks.delete_project_dump.apply_async( + (project.pk, project.slug, task.id, dump_format), + countdown=settings.EXPORTS_TTL + ) + print("-> Sent task for dump of project '{}' as user {}".format(project.name, username_or_email))