forked from Zipstack/unstract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
155 lines (143 loc) · 4.76 KB
/
models.py
File metadata and controls
155 lines (143 loc) · 4.76 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
import uuid
from typing import Any
from account.models import User
from api.constants import ApiExecution
from django.db import connection, models
from pipeline.models import Pipeline
from utils.models.base_model import BaseModel
from workflow_manager.workflow.models.workflow import Workflow
API_NAME_MAX_LENGTH = 30
DESCRIPTION_MAX_LENGTH = 255
API_ENDPOINT_MAX_LENGTH = 255
class APIDeployment(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
display_name = models.CharField(
max_length=API_NAME_MAX_LENGTH,
unique=True,
default="default api",
db_comment="User-given display name for the API.",
)
description = models.CharField(
max_length=DESCRIPTION_MAX_LENGTH,
blank=True,
default="",
db_comment="User-given description for the API.",
)
workflow = models.ForeignKey(
Workflow,
on_delete=models.CASCADE,
db_comment="Foreign key reference to the Workflow model.",
)
is_active = models.BooleanField(
default=True,
db_comment="Flag indicating whether the API is active or not.",
)
# TODO: Implement dynamic generation of API endpoints for API deployments
# instead of persisting them in the database.
api_endpoint = models.CharField(
max_length=API_ENDPOINT_MAX_LENGTH,
unique=True,
editable=False,
db_comment="URL endpoint for the API deployment.",
)
api_name = models.CharField(
max_length=API_NAME_MAX_LENGTH,
unique=True,
default="default",
db_comment="Short name for the API deployment.",
)
created_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
related_name="api_created_by",
null=True,
blank=True,
editable=False,
)
modified_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
related_name="api_modified_by",
null=True,
blank=True,
editable=False,
)
@property
def api_key_data(self):
return {"api": self.id, "description": f"API Key for {self.api_name}"}
def __str__(self) -> str:
return f"{self.id} - {self.display_name}"
def save(self, *args: Any, **kwargs: Any) -> None:
"""Save hook to update api_endpoint.
Custom save hook for updating the 'api_endpoint' based on
'api_name'. If the instance is being updated, it checks for
changes in 'api_name' and adjusts 'api_endpoint'
accordingly. If the instance is new, 'api_endpoint' is set
based on 'api_name' and the current database schema.
"""
if self.pk is not None:
try:
original = APIDeployment.objects.get(pk=self.pk)
if original.api_name != self.api_name:
org_schema = connection.get_tenant().schema_name
self.api_endpoint = (
f"{ApiExecution.PATH}/{org_schema}/{self.api_name}/"
)
except APIDeployment.DoesNotExist:
org_schema = connection.get_tenant().schema_name
self.api_endpoint = f"{ApiExecution.PATH}/{org_schema}/{self.api_name}/"
super().save(*args, **kwargs)
class APIKey(BaseModel):
id = models.UUIDField(
primary_key=True,
editable=False,
default=uuid.uuid4,
db_comment="Unique identifier for the API key.",
)
api_key = models.UUIDField(
default=uuid.uuid4,
editable=False,
unique=True,
db_comment="Actual key UUID.",
)
api = models.ForeignKey(
APIDeployment,
on_delete=models.CASCADE,
null=True,
blank=True,
db_comment="Foreign key reference to the APIDeployment model.",
)
pipeline = models.ForeignKey(
Pipeline,
on_delete=models.CASCADE,
null=True,
blank=True,
db_comment="Foreign key reference to the Pipeline model.",
)
description = models.CharField(
max_length=DESCRIPTION_MAX_LENGTH,
null=True,
db_comment="Description of the API key.",
)
is_active = models.BooleanField(
default=True,
db_comment="Flag indicating whether the API key is active or not.",
)
created_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
related_name="api_key_created_by",
null=True,
blank=True,
editable=False,
)
modified_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
related_name="api_key_modified_by",
null=True,
blank=True,
editable=False,
)
def __str__(self) -> str:
return f"{self.api.api_name} - {self.id} - {self.api_key}"