Skip to content

Commit 7404286

Browse files
committed
Add EncryptedModelAdmin for encrypted fields
Paginator not working yet.
1 parent 000e0ef commit 7404286

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

django_mongodb_backend/admin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.contrib import admin
2+
from django.contrib.admin.views.main import ChangeList
3+
4+
5+
class EncryptedPaginator:
6+
# TODO: Implement pagination for encrypted data. This paginator
7+
# currently returns all results in a single page.
8+
def __init__(self, queryset, per_page):
9+
self.queryset = queryset
10+
self.per_page = per_page
11+
12+
def page(self, number):
13+
results = list(self.queryset)
14+
has_next = False
15+
return results, has_next
16+
17+
18+
class EncryptedChangeList(ChangeList):
19+
def get_results(self, request):
20+
paginator = EncryptedPaginator(self.queryset, self.list_per_page)
21+
self.result_list, _ = paginator.page(self.page_num + 1)
22+
23+
self.result_count = len(self.result_list)
24+
self.full_result_count = self.result_count
25+
26+
self.can_show_all = True
27+
self.multi_page = False
28+
29+
30+
class EncryptedModelAdmin(admin.ModelAdmin):
31+
def get_changelist(self, request, **kwargs):
32+
return EncryptedChangeList

docs/howto/queryable-encryption.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,26 @@ settings:
294294
},
295295
}
296296
297+
Configuring the ``EncryptedModelAdmin``
298+
=======================================
299+
300+
When using the :doc:`the Django admin site <django:ref/contrib/admin/index>`
301+
with models that have encrypted fields, use the :class:`EncryptedModelAdmin`
302+
class to ensure that encrypted fields are handled correctly. To do this, inherit
303+
from :class:`EncryptedModelAdmin` in your admin classes instead of the standard
304+
:class:`~django.contrib.admin.ModelAdmin`.
305+
306+
.. code-block:: python
307+
308+
# myapp/admin.py
309+
from django.contrib import admin
310+
from .models import Patient
311+
from django_mongodb_backend.admin import EncryptedModelAdmin
312+
313+
314+
@admin.register(Patient)
315+
class PatientAdmin(EncryptedModelAdmin):
316+
pass
317+
297318
You are now ready to :doc:`start developing applications
298319
</topics/queryable-encryption>` with Queryable Encryption!

docs/ref/contrib/admin.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=====
2+
Admin
3+
=====
4+
5+
Django MongoDB Backend supports the Django admin interface. To enable it, ensure
6+
that you have :ref:`specified the default pk field
7+
<specifying the-default-pk-field>` for the
8+
:class:`~django.contrib.admin.apps.AdminConfig` class as described in the
9+
:doc:`Getting Started </intro/configure>` guide.
10+
11+
``EncryptedModelAdmin``
12+
=======================
13+
14+
.. class:: EncryptedModelAdmin
15+
16+
.. versionadded:: 5.2.3
17+
18+
A :class:`~django.contrib.admin.ModelAdmin` subclass that supports models
19+
with encrypted fields. Use this class as a base class for your model's admin
20+
class to ensure that encrypted fields are handled correctly in the admin
21+
interface.
22+
23+
Define a model with encrypted fields:
24+
25+
.. code-block:: python
26+
27+
# myapp/models.py
28+
from django.db import models
29+
from django_mongodb_backend.fields import EmbeddedModelField
30+
31+
32+
class Patient(models.Model):
33+
patient_name = models.CharField(max_length=255)
34+
patient_id = models.BigIntegerField()
35+
patient_record = EmbeddedModelField("PatientRecord")
36+
37+
def __str__(self):
38+
return f"{self.patient_name} ({self.patient_id})"
39+
40+
Register it with the Django admin using the ``EncryptedModelAdmin`` as shown
41+
below:
42+
43+
.. code-block:: python
44+
45+
# myapp/admin.py
46+
from django.contrib import admin
47+
from django_mongodb_backend.admin import EncryptedModelAdmin
48+
from .models import Patient
49+
50+
51+
@admin.register(Patient)
52+
class PatientAdmin(EncryptedModelAdmin):
53+
pass

docs/ref/contrib/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Notes for Django's :doc:`django:ref/contrib/index` live here.
77
.. toctree::
88
:maxdepth: 1
99

10+
admin
1011
gis

docs/ref/utils.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ following parts can be considered stable.
4949
But for maximum flexibility, construct :setting:`DATABASES` manually as
5050
described in :ref:`configuring-databases-setting`.
5151

52-
.. versionadded:: 5.2.3
5352

5453
``model_has_encrypted_fields()``
5554
=================================
5655

5756
.. function:: model_has_encrypted_fields(model)
5857

58+
.. versionadded:: 5.2.3
59+
5960
Returns ``True`` if the given Django model has any fields that use
6061
encrypted models.
6162

0 commit comments

Comments
 (0)