Skip to content

Commit

Permalink
feat: add import_projectbackup management command
Browse files Browse the repository at this point in the history
This is useful for importing huge backups not suitable for file upload.
  • Loading branch information
nijel committed Jan 30, 2025
1 parent ce30c86 commit 565464a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/admin/backup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The generated backups are kept on the server as configured by
:setting:`PROJECT_BACKUP_KEEP_DAYS` and :setting:`PROJECT_BACKUP_KEEP_COUNT`
(it defaults to keep at most 3 backups for 30 days).

Use the generated file to import project when :ref:`adding-projects`.
Use the generated file to import project when :ref:`adding-projects` or in :wladmin:`import_projectbackup`.

.. note::

Expand Down
13 changes: 13 additions & 0 deletions docs/admin/management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,19 @@ Importing Sphinx documentation split to multiple files and directories:
More detailed examples can be found in the :ref:`starting` chapter,
alternatively you might want to use :wladmin:`import_json`.

import_projectbackup
--------------------

.. weblate-admin:: import_projectbackup <project_name> <project_slug> <username> <filename>

.. versionadded:: 5.10

Imports :ref:`projectbackup`.

.. hint::

Usually it is more comfortable to import project when :ref:`adding-projects`.

importuserdata
--------------

Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Not yet released.
* :ref:`check-rst-references` check to validate reStructuredText references.
* :ref:`check-rst-syntax` check to validate reStructuredText syntax.
* API can now produce CSV output.
* New management command :wladmin:`import_projectbackup` to import :ref:`projectbackup`.

**Improvements**

Expand Down
36 changes: 36 additions & 0 deletions weblate/trans/management/commands/import_projectbackup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations

from weblate.auth.models import User
from weblate.trans.backups import ProjectBackup
from weblate.utils.management.base import BaseCommand


class Command(BaseCommand):
"""Command for importing project backup into Weblate."""

help = "imports project backup"

def add_arguments(self, parser) -> None:
super().add_arguments(parser)
parser.add_argument("project_name", help="Project name")
parser.add_argument("project_slug", help="Project slug")
parser.add_argument("username", help="Username doing import")
parser.add_argument("filename", help="Path to project backup")

def handle(
self,
project_name: str,
project_slug: str,
username: str,
filename: str,
**options,
) -> None:
user = User.objects.get(username=username)
restore = ProjectBackup(filename)

restore.validate()

restore.restore(project_name=project_name, project_slug=project_slug, user=user)
14 changes: 13 additions & 1 deletion weblate/trans/tests/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management import call_command
from django.db import connection
from django.test import skipIfDBFeature, skipUnlessDBFeature
from django.urls import reverse
Expand Down Expand Up @@ -128,9 +129,20 @@ def test_restore_not_supported(self) -> None:
def test_restore_4_14(self) -> None:
restore = ProjectBackup(TEST_BACKUP)
restore.validate()
restored = restore.restore(
restore.restore(
project_name="Restored", project_slug="restored", user=self.user
)
self.verify_restored()

@skipUnlessDBFeature("can_return_rows_from_bulk_insert")
def test_restore_cli(self) -> None:
call_command(
"import_projectbackup", "Restored", "restored", "testuser", TEST_BACKUP
)
self.verify_restored()

def verify_restored(self):
restored = Project.objects.get(slug="restored")
self.assertEqual(
16,
Unit.objects.filter(translation__component__project=restored).count(),
Expand Down

0 comments on commit 565464a

Please sign in to comment.