-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkspace.py
More file actions
179 lines (149 loc) · 6.04 KB
/
workspace.py
File metadata and controls
179 lines (149 loc) · 6.04 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import uuid6
import typing
from typing import Literal, Optional
from django.db import models
from django.db.models import Q
from django.conf import settings
if typing.TYPE_CHECKING:
from django.contrib.auth import get_user_model
User = get_user_model()
class WorkspaceQueryset(models.QuerySet):
def get_queryset(self):
return self.select_related("transfer_confirmation", "delete_confirmation")
def visible(self, principal: Optional["User"]):
queryset = self.get_queryset()
if principal is None:
return queryset.filter(is_private=False)
elif hasattr(principal, "account_type"):
if principal.account_type == "admin":
return queryset
else:
return queryset.filter(
Q(is_private=False)
| Q(owner=principal)
| Q(collaborators__user=principal)
| Q(transfer_confirmation__new_owner=principal)
)
elif hasattr(principal, "workspace"):
return queryset.filter(Q(is_private=False) | Q(pk=principal.workspace.pk))
else:
return queryset.filter(is_private=False)
def associated(self, principal: Optional["User"]):
queryset = self.get_queryset()
if principal is None:
return queryset.none()
elif hasattr(principal, "account_type"):
if principal.account_type == "admin":
return queryset
else:
return queryset.filter(
Q(owner=principal)
| Q(collaborators__user=principal)
| Q(transfer_confirmation__new_owner=principal)
)
elif hasattr(principal, "workspace"):
return queryset.filter(id=principal.workspace.id)
else:
return queryset.none()
class Workspace(models.Model):
id = models.UUIDField(primary_key=True, default=uuid6.uuid7, editable=False)
name = models.CharField(max_length=255)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
is_private = models.BooleanField(default=False)
objects = WorkspaceQueryset.as_manager()
def __str__(self):
return f"{self.name} - {self.id}"
class Meta:
constraints = [
models.UniqueConstraint(
fields=["name", "owner"], name="unique_workspace_name_per_owner"
)
]
@property
def link(self):
return f"{settings.PROXY_BASE_URL}/api/auth/workspaces/{self.id}"
@property
def transfer_details(self):
return getattr(self, "transfer_confirmation", None)
@property
def delete_details(self):
return getattr(self, "delete_confirmation", None)
@classmethod
def can_principal_create(cls, principal: Optional["User"]):
return (
hasattr(principal, "account_type") and principal.account_type != "limited"
)
def get_principal_permissions(
self, principal: Optional["User"]
) -> list[Literal["edit", "delete", "view"]]:
if hasattr(principal, "account_type"):
if principal == self.owner or principal.account_type == "admin":
return ["view", "edit", "delete"]
elif (
self.is_private is False
or self.collaborators.filter(user=principal).exists()
):
return ["view"]
elif self.transfer_details and self.transfer_details.new_owner == principal:
return ["view"]
else:
return []
elif hasattr(principal, "workspace"):
if self.is_private is False or principal.workspace == self:
return ["view"]
else:
return []
else:
if self.is_private is False:
return ["view"]
else:
return []
def delete(self, *args, **kwargs):
self.delete_contents(filter_arg=self, filter_suffix="")
super().delete(*args, **kwargs)
@staticmethod
def delete_contents(filter_arg: models.Model, filter_suffix: Optional[str]):
from domains.iam.models import Role, Collaborator, APIKey
from domains.sta.models import (
Thing,
ObservedProperty,
ProcessingLevel,
ResultQualifier,
Sensor,
Unit,
)
workspace_relation_filter = (
f"workspace__{filter_suffix}" if filter_suffix else "workspace"
)
Collaborator.objects.filter(**{workspace_relation_filter: filter_arg}).delete()
Role.delete_contents(
filter_arg=filter_arg, filter_suffix=workspace_relation_filter
)
Role.objects.filter(**{workspace_relation_filter: filter_arg}).delete()
APIKey.objects.filter(**{workspace_relation_filter: filter_arg}).delete()
Thing.delete_contents(
filter_arg=filter_arg, filter_suffix=workspace_relation_filter
)
Thing.objects.filter(**{workspace_relation_filter: filter_arg}).delete()
ObservedProperty.objects.filter(
**{workspace_relation_filter: filter_arg}
).delete()
ProcessingLevel.objects.filter(
**{workspace_relation_filter: filter_arg}
).delete()
ResultQualifier.objects.filter(
**{workspace_relation_filter: filter_arg}
).delete()
Sensor.objects.filter(**{workspace_relation_filter: filter_arg}).delete()
Unit.objects.filter(**{workspace_relation_filter: filter_arg}).delete()
class WorkspaceTransferConfirmation(models.Model):
workspace = models.OneToOneField(
"Workspace", on_delete=models.CASCADE, related_name="transfer_confirmation"
)
new_owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
initiated = models.DateTimeField()
class WorkspaceDeleteConfirmation(models.Model):
workspace = models.OneToOneField(
"Workspace", on_delete=models.CASCADE, related_name="delete_confirmation"
)
initiated = models.DateTimeField()