Coverage for app/venv/lib/python3.14/site-packages/weblate/accounts/management/commands/dumpuserdata.py: 0%

27 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4from __future__ import annotations 

5 

6import json 

7from pathlib import Path 

8from typing import TYPE_CHECKING 

9 

10from django.core.management.base import CommandError 

11from django.core.serializers.json import DjangoJSONEncoder 

12 

13from weblate.accounts.models import Profile 

14from weblate.utils.management.base import BaseCommand 

15 

16if TYPE_CHECKING: 

17 from django.core.management.base import CommandParser 

18 

19 

20class Command(BaseCommand): 

21 help = "dumps user data to JSON file" 

22 

23 def add_arguments(self, parser: CommandParser) -> None: 

24 parser.add_argument("json-file", type=Path, help="File where to export") 

25 

26 def handle(self, *args, **options) -> None: 

27 data = [] 

28 

29 profiles = Profile.objects.select_related("user").prefetch_related( 

30 "watched", "languages", "secondary_languages" 

31 ) 

32 

33 for profile in profiles: 

34 if not profile.user.is_active or profile.user.is_bot: 

35 continue 

36 data.append(profile.dump_data()) 

37 

38 try: 

39 with options["json-file"].open("w") as handle: 

40 json.dump(data, handle, indent=2, cls=DjangoJSONEncoder) 

41 except OSError as error: 

42 msg = f"Could not open file: {error}" 

43 raise CommandError(msg) from error