From 5f14868b6ad258636700267c18bcefc4855d2201 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 25 Jul 2023 13:41:23 +0200 Subject: [PATCH] MT#57906 repoapi: add loaddata_release command * loads data exported via dumpdata_release command Change-Id: I027f409e4121e6c3d4679ac400668a7efd993b78 --- .../management/commands/loaddata_release.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 repoapi/management/commands/loaddata_release.py diff --git a/repoapi/management/commands/loaddata_release.py b/repoapi/management/commands/loaddata_release.py new file mode 100644 index 0000000..e6be0ff --- /dev/null +++ b/repoapi/management/commands/loaddata_release.py @@ -0,0 +1,48 @@ +# Copyright (C) 2023 The Sipwise Team - http://sipwise.com +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU 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 General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +import argparse + +from django.core.management.base import BaseCommand +from tablib import Dataset + +from repoapi.admin import JenkinsBuildInfoResource + + +class Command(BaseCommand): + help = "import BuildRelease info" + + def add_arguments(self, parser): + parser.add_argument( + "--format", choices=["json", "yaml"], default="yaml" + ) + parser.add_argument("file", type=argparse.FileType("r")) + + def handle(self, *args, **options): + jbi = JenkinsBuildInfoResource() + data = Dataset() + with options["file"] as f: + data.load(f) + loaded = len(data) + self.stdout.write( + self.style.SUCCESS("Successfully loaded %s jbi objects" % loaded) + ) + result = jbi.import_data( + data, raise_errors=True, use_transactions=True + ) + self.stdout.write( + self.style.SUCCESS( + "Successfully imported %s jbi objects" % result.total_rows + ) + )