-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathmodels.py
93 lines (74 loc) · 2.89 KB
/
models.py
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
#
# Copyright 2019 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""API models for import organization."""
from typing import Optional
from django.db import models
from django.db.models import Q
from management.cache import PublicTenantCache
from api.cross_access.model import CrossAccountRequest # noqa: F401
from api.status.model import Status # noqa: F401
class TenantModifiedQuerySet(models.QuerySet):
"""Queryset for modified tenants."""
def modified_only(self):
"""Return only modified tenants."""
return (
self.filter(Q(group__system=False) | Q(role__system=False))
.prefetch_related("group_set", "role_set")
.distinct()
)
def get_public_tenant(self):
"""Return the public tenant."""
cache = PublicTenantCache()
tenant = cache.get_tenant(Tenant.PUBLIC_TENANT_NAME)
if tenant is None:
tenant = self.get(tenant_name=Tenant.PUBLIC_TENANT_NAME)
cache.save_tenant(tenant)
return tenant
class Tenant(models.Model):
"""The model used to create a tenant schema."""
ready = models.BooleanField(default=False)
tenant_name = models.CharField(max_length=63)
account_id = models.CharField(max_length=36, default=None, null=True)
org_id = models.CharField(max_length=36, unique=True, default=None, db_index=True, null=True)
objects = TenantModifiedQuerySet.as_manager()
PUBLIC_TENANT_NAME = "public"
def __str__(self):
"""Get string representation of Tenant."""
return f"Tenant ({self.org_id})"
class Meta:
indexes = [
models.Index(fields=["ready"]),
]
class TenantAwareModel(models.Model):
"""Abstract model for inheriting `Tenant`."""
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
class Meta:
abstract = True
class User:
"""A request User. Might also represent a service account."""
username: Optional[str] = None
account: Optional[str] = None
admin: bool = False
access = {}
system: bool = False
is_active: bool = True
org_id: Optional[str] = None
user_id: Optional[str] = None
# Service account properties.
bearer_token: str = ""
client_id: str = ""
is_service_account: bool = False