-
Notifications
You must be signed in to change notification settings - Fork 505
Expand file tree
/
Copy pathmodels.py
More file actions
44 lines (35 loc) · 1.39 KB
/
models.py
File metadata and controls
44 lines (35 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from django.conf import settings
from django.db import models
from django_lifecycle import ( # type: ignore[import-untyped]
BEFORE_UPDATE,
LifecycleModelMixin,
hook,
)
from rest_framework_api_key.models import AbstractAPIKey, APIKeyManager
from softdelete.models import ( # type: ignore[import-untyped]
SoftDeleteManager,
SoftDeleteObject,
)
from organisations.models import Organisation
class MasterAPIKeyManager(APIKeyManager, SoftDeleteManager): # type: ignore[misc]
pass
class MasterAPIKey(AbstractAPIKey, LifecycleModelMixin, SoftDeleteObject): # type: ignore[django-manager-missing,misc] # noqa: E501 # noqa: E501
organisation = models.ForeignKey(
Organisation,
on_delete=models.CASCADE,
related_name="master_api_keys",
)
objects = MasterAPIKeyManager() # type: ignore[misc]
is_admin = models.BooleanField(default=True)
created_by = models.ForeignKey(
"users.FFAdminUser", on_delete=models.SET_NULL, null=True, blank=True
)
@hook(BEFORE_UPDATE, when="is_admin", was=False, is_now=True)
def delete_role_api_keys( # type: ignore[no-untyped-def]
self,
):
if settings.IS_RBAC_INSTALLED:
from rbac.models import ( # type: ignore[import-not-found,unused-ignore]
MasterAPIKeyRole,
)
MasterAPIKeyRole.objects.filter(master_api_key=self.id).delete()