Skip to content

Commit e6c43e2

Browse files
committed
[change] Refactor ReceiveUrlAdmin to inherit CopyableFieldAdmin #344
- Refactored ReceiveUrlAdmin to inherit from CopyableFieldsAdmin instead of ModelAdmin - Eliminated code duplication in add_view/change_view methods - Made receive_url work as a copyable field automatically - Updated ProjectAdmin to use single inheritance (no longer needs UUIDAdmin + ReceiveUrlAdmin) - Updated tests to reflect correct behavior (copyable fields excluded from add forms) - Reduced JavaScript and Python logic repetition This addresses the enhancement request to reduce repetition between ReceiveUrlAdmin and CopyableFieldAdmin by using proper inheritance. Fixes #344
1 parent f3fc7c2 commit e6c43e2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

openwisp_utils/admin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,22 @@ def uuid(self, obj):
150150
uuid.short_description = _("UUID")
151151

152152

153-
class ReceiveUrlAdmin(ModelAdmin):
153+
class ReceiveUrlAdmin(CopyableFieldsAdmin):
154154
"""Adds a receive_url field.
155155
156156
The receive_url method will build the URL using the parameters:
157157
158158
- receive_url_name
159159
- receive_url_object_arg
160-
- receive_url_object_arg
160+
- receive_url_querystring_arg
161161
"""
162162

163163
receive_url_querystring_arg = "key"
164164
receive_url_object_arg = "pk"
165165
receive_url_name = None
166166
receive_url_urlconf = None
167167
receive_url_baseurl = None
168+
copyable_fields = ("receive_url",)
168169

169170
def add_view(self, request, *args, **kwargs):
170171
self.request = request

tests/test_project/admin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
ReadOnlyAdmin,
99
ReceiveUrlAdmin,
1010
TimeReadonlyAdminMixin,
11-
UUIDAdmin,
1211
)
1312
from openwisp_utils.admin_theme.filters import (
1413
AutocompleteFilter,
@@ -80,12 +79,18 @@ class OperatorInline(HelpTextStackedInline):
8079

8180

8281
@admin.register(Project)
83-
class ProjectAdmin(UUIDAdmin, ReceiveUrlAdmin):
82+
class ProjectAdmin(ReceiveUrlAdmin):
8483
inlines = [OperatorInline]
8584
list_display = ("name",)
8685
fields = ("uuid", "name", "key", "receive_url")
8786
readonly_fields = ("uuid", "receive_url")
8887
receive_url_name = "receive_project"
88+
copyable_fields = ("uuid", "receive_url")
89+
90+
def uuid(self, obj):
91+
return obj.pk
92+
93+
uuid.short_description = _("UUID")
8994

9095

9196
class ShelfFilter(SimpleInputFilter):

tests/test_project/tests/test_admin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def test_uuid_field_in_add(self):
215215
response = self.client.get(path)
216216
self.assertEqual(response.status_code, 200)
217217
self.assertNotContains(response, "field-uuid")
218-
self.assertContains(response, "field-receive_url")
218+
self.assertNotContains(response, "field-receive_url")
219219

220220
def test_copyablefields_admin(self):
221221
class TestCopyableFieldAdmin(CopyableFieldsAdmin):
@@ -251,10 +251,12 @@ def test_copyablefields_admin_fields_order(self):
251251
path = reverse("admin:test_project_project_add")
252252
self.client.get(path)
253253
ma = ProjectAdmin(Project, self.site)
254-
# 'uuid' should be missing from ma.get_fields()
254+
# 'uuid' and 'receive_url' should be missing from ma.get_fields()
255255
# because we're testing the project admin add form,
256-
# and now we're adding it here again only to assert the admin field order
257-
self.assertEqual(ma.fields, ("uuid", *ma.get_fields(self.client.request)))
256+
# and now we're adding them here again only to assert the admin field order
257+
self.assertEqual(
258+
ma.fields, ("uuid", *ma.get_fields(self.client.request), "receive_url")
259+
)
258260
self.assertEqual(
259261
ma.readonly_fields, ma.get_readonly_fields(self.client.request)
260262
)

0 commit comments

Comments
 (0)