Skip to content

Commit 852e204

Browse files
committed
[fix] Fixes by @coderabbitai
1 parent d9c6199 commit 852e204

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

openwisp_users/management/commands/export_users.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.contrib.auth import get_user_model
44
from django.core.exceptions import ObjectDoesNotExist
5-
from django.core.management.base import BaseCommand
5+
from django.core.management.base import BaseCommand, CommandError
66

77
from ... import settings as app_settings
88

@@ -53,7 +53,9 @@ def add_arguments(self, parser):
5353
def handle(self, *args, **options):
5454
fields = app_settings.EXPORT_USERS_COMMAND_CONFIG.get("fields", []).copy()
5555
# Get the fields to be excluded from the command-line argument
56-
exclude_fields = options.get("exclude_fields").split(",")
56+
exclude_fields = [
57+
t.strip() for t in options.get("exclude_fields").split(",") if t.strip()
58+
]
5759
# Remove excluded fields from the export fields (match on the field name)
5860
fields = [
5961
field
@@ -97,7 +99,7 @@ def _get_field_value(self, user, field):
9799
try:
98100
return callable_fn(user)
99101
except Exception as e:
100-
raise Exception(f"Error calling function for field '{name}': {e}")
102+
raise CommandError(f"Error calling function for field '{name}': {e}")
101103
if subfields is not None:
102104
try:
103105
attr = getattr(user, name)

openwisp_users/tests/test_commands.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616

1717
class TestManagementCommands(TestOrganizationMixin, TestCase):
18-
app_label = "openwisp_users"
19-
2018
def setUp(self):
2119
super().setUp()
2220
self.temp_file = NamedTemporaryFile(mode="wt", delete=False)
@@ -126,6 +124,7 @@ def test_related_fields(self):
126124
"prefetch_related": [],
127125
},
128126
)
127+
@capture_stdout()
129128
def test_related_fields_no_n_plus_1(self):
130129
"""Query count must not grow with additional users."""
131130
user1 = self._create_user()
@@ -173,12 +172,12 @@ def _broken_callable(user):
173172
{"name": "birth_date", "fields": ["year"]},
174173
# manager with single subfield
175174
{
176-
"name": f"{app_label}_organizationuser",
175+
"name": "openwisp_users_organizationuser",
177176
"fields": ["organization_id"],
178177
},
179178
# manager with multiple subfields
180179
{
181-
"name": f"{app_label}_organizationuser",
180+
"name": "openwisp_users_organizationuser",
182181
"fields": ["organization_id", "is_admin"],
183182
},
184183
# dot-notation on a manager
@@ -188,6 +187,7 @@ def _broken_callable(user):
188187
"prefetch_related": ["openwisp_users_organizationuser"],
189188
},
190189
)
190+
@capture_stdout()
191191
def test_subfields_dict_field(self):
192192
org = self._create_org(name="org1")
193193
user1 = self._create_user(birth_date=None)

0 commit comments

Comments
 (0)