Skip to content

Commit 04c3513

Browse files
committed
[change] Added default uuid method to CopyableFieldsAdmin #328
``CopyableFieldsAdmin`` now ships with a default ``uuid()`` method that returns ``obj.pk`` and has ``short_description`` set to ``'UUID'``. This allows subclasses to use ``copyable_fields = ('uuid',)`` without defining their own ``uuid`` method when their model uses a UUID primary key. The ``uuid`` field is not automatically added to ``copyable_fields``, that's still the developer's explicit decision. Related to #328
1 parent 1e2fc38 commit 04c3513

4 files changed

Lines changed: 33 additions & 14 deletions

File tree

docs/developer/admin-utilities.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,12 @@ it easy to copy the fields contents.
4949
Useful for auto-generated fields such as UUIDs, secret keys, tokens, etc.
5050

5151
To replicate the "copy UUID" behavior previously provided by the removed
52-
``UUIDAdmin`` class, set ``copyable_fields = ("uuid",)`` on a
53-
``CopyableFieldsAdmin`` subclass and expose a ``uuid()`` display method,
54-
for example:
52+
``UUIDAdmin`` class, just set ``copyable_fields = ("uuid",)`` on a
53+
``CopyableFieldsAdmin`` subclass:
5554

5655
.. code-block:: python
5756
5857
from django.contrib import admin
59-
from django.utils.translation import gettext_lazy as _
6058
from openwisp_utils.admin import CopyableFieldsAdmin
6159
6260
from .models import MyModel
@@ -66,11 +64,6 @@ for example:
6664
class MyModelAdmin(CopyableFieldsAdmin):
6765
copyable_fields = ("uuid",)
6866
69-
def uuid(self, obj):
70-
return obj.pk
71-
72-
uuid.short_description = _("UUID")
73-
7467
``openwisp_utils.admin.ReceiveUrlAdmin``
7568
----------------------------------------
7669

openwisp_utils/admin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
130130
extra_context=extra_context,
131131
)
132132

133+
def uuid(self, obj):
134+
"""Return the object's primary key (UUID).
135+
136+
This default implementation allows subclasses to use
137+
copyable_fields = ("uuid",) without defining their own uuid()
138+
method when their model uses a UUID primary key.
139+
"""
140+
return obj.pk
141+
142+
uuid.short_description = _("UUID")
143+
133144
class Media:
134145
js = ("admin/js/jquery.init.js", "openwisp-utils/js/copyable.js")
135146

tests/test_project/admin.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ class ProjectAdmin(CopyableFieldsAdmin, ReceiveUrlAdmin):
8888
receive_url_name = "receive_project"
8989
copyable_fields = ("uuid",)
9090

91-
def uuid(self, obj):
92-
return obj.pk
93-
94-
uuid.short_description = _("UUID")
95-
9691

9792
class ShelfFilter(SimpleInputFilter):
9893
parameter_name = "shelf"

tests/test_project/tests/test_admin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,26 @@ class TestCopyableFieldAdmin(CopyableFieldsAdmin):
247247
ma.get_fields(self.client.request)
248248
self.assertIn(copyable_field_err, err.exception.args[0])
249249

250+
def test_copyablefields_admin_default_uuid_method(self):
251+
"""CopyableFieldsAdmin should provide a default uuid method.
252+
253+
This allows subclasses to use copyable_fields = ("uuid",) without
254+
having to define their own uuid() method and short_description.
255+
The uuid method should return obj.pk and have short_description
256+
"UUID".
257+
"""
258+
259+
class TestAdminWithUuid(CopyableFieldsAdmin):
260+
copyable_fields = ("uuid",)
261+
262+
p = Project.objects.create(name="test-default-uuid")
263+
ma = TestAdminWithUuid(Project, AdminSite())
264+
# Verify the uuid method exists and returns obj.pk
265+
self.assertTrue(hasattr(ma, "uuid"))
266+
self.assertEqual(ma.uuid(p), p.pk)
267+
# Verify short_description is set
268+
self.assertEqual(ma.uuid.short_description, "UUID")
269+
250270
def test_copyablefields_admin_fields_order(self):
251271
path = reverse("admin:test_project_project_add")
252272
self.client.get(path)

0 commit comments

Comments
 (0)