Skip to content

Commit 01c4d8e

Browse files
committed
Do not include the Dataspace related FKs on addition
as the Dataspace does not exist yet Signed-off-by: tdruez <[email protected]>
1 parent 093403e commit 01c4d8e

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

dje/admin.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,11 +1081,7 @@ class DataspaceConfigurationInline(DataspacedFKMixin, admin.StackedInline):
10811081
form = DataspaceConfigurationForm
10821082
verbose_name_plural = _("Configuration")
10831083
verbose_name = _("Dataspace configuration")
1084-
fieldsets = [
1085-
(
1086-
"",
1087-
{"fields": ("homepage_layout",)},
1088-
),
1084+
add_fieldsets = [
10891085
(
10901086
"AboutCode Integrations",
10911087
{
@@ -1142,8 +1138,15 @@ class DataspaceConfigurationInline(DataspacedFKMixin, admin.StackedInline):
11421138
},
11431139
),
11441140
]
1141+
# Do not include the Dataspace related FKs on addition as the Dataspace does not exist yet
1142+
fieldsets = [("", {"fields": ("homepage_layout",)})] + add_fieldsets
11451143
can_delete = False
11461144

1145+
def get_fieldsets(self, request, obj=None):
1146+
if not obj:
1147+
return self.add_fieldsets
1148+
return super().get_fieldsets(request, obj)
1149+
11471150

11481151
@admin.register(Dataspace, site=dejacode_site)
11491152
class DataspaceAdmin(
@@ -1433,7 +1436,6 @@ class DejacodeUserAdmin(
14331436
add_fieldsets = (
14341437
(None, {"fields": ("username", "dataspace")}),
14351438
(_("Personal information"), {"fields": ("email", "first_name", "last_name", "company")}),
1436-
(_("Profile"), {"fields": ("homepage_layout",)}),
14371439
(
14381440
_("Notifications"),
14391441
{
@@ -1448,6 +1450,7 @@ class DejacodeUserAdmin(
14481450
(_("Permissions"), {"fields": ("is_staff", "is_superuser", "groups")}),
14491451
)
14501452
fieldsets = add_fieldsets[:-1] + (
1453+
(_("Profile"), {"fields": ("homepage_layout",)}),
14511454
(_("Permissions"), {"fields": ("is_active", "is_staff", "is_superuser", "groups")}),
14521455
(_("Important dates"), {"fields": ("last_login", "last_api_access", "date_joined")}),
14531456
)

dje/tests/test_admin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ def test_dataspace_admin_changeform_update_packages_from_scan_field_validation(s
188188
response = self.client.post(url, data, follow=True)
189189
self.assertContains(response, "was changed successfully.")
190190

191+
def test_dataspace_admin_changeform_hide_dataspace_fk_on_addition(self):
192+
self.client.login(username=self.super_user.username, password="secret")
193+
194+
expected1 = "homepage_layout"
195+
expected2 = "Homepage layout"
196+
197+
url = reverse("admin:dje_dataspace_add")
198+
response = self.client.get(url)
199+
self.assertNotContains(response, expected1)
200+
self.assertNotContains(response, expected2)
201+
202+
url = reverse("admin:dje_dataspace_change", args=[self.other_dataspace.pk])
203+
response = self.client.get(url)
204+
self.assertContains(response, expected1)
205+
self.assertContains(response, expected2)
206+
191207
def test_dataspace_admin_changelist_missing_in_filter_availability(self):
192208
# MissingInFilter is only available to superusers
193209
url = reverse("admin:organization_owner_changelist")

dje/tests/test_user.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,21 @@ def test_user_admin_changeform_submit_row_delete_button_label(self):
413413
)
414414
self.assertContains(response, expected, html=True)
415415

416+
def test_user_admin_hide_dataspace_fk_on_addition(self):
417+
self.client.login(username="nexb_user", password="secret")
418+
url = reverse("admin:dje_dejacodeuser_add")
419+
response = self.client.get(url)
420+
421+
expected1 = "homepage_layout"
422+
expected2 = "Homepage layout"
423+
self.assertNotContains(response, expected1)
424+
self.assertNotContains(response, expected2)
425+
426+
url = reverse("admin:dje_dejacodeuser_change", args=[self.other_user.pk])
427+
response = self.client.get(url)
428+
self.assertContains(response, expected1)
429+
self.assertContains(response, expected2)
430+
416431
def test_user_admin_form_scope_homepage_layout_choices(self):
417432
self.client.login(username=self.nexb_user.username, password="secret")
418433
url = reverse("admin:dje_dejacodeuser_change", args=[self.nexb_user.pk])

0 commit comments

Comments
 (0)