Skip to content

Commit

Permalink
[Story]: OAuth - updated primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Nagendra committed Feb 12, 2024
1 parent a4186fa commit 81262d6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 81 deletions.
41 changes: 41 additions & 0 deletions core/migrations/0012_auto_20240212_0639.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 3.1.5 on 2024-02-12 06:39

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('core', '0011_connector_oauth'),
]

operations = [
migrations.AddField(
model_name='connector',
name='oauth_keys',
field=models.CharField(choices=[('public', 'PUBLIC'), ('private', 'PRIVATE')],
default='private', max_length=64),
),
migrations.CreateModel(
name='OAuthApiKeys',
fields=[
('id', models.UUIDField(default=uuid.UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'),
editable=False, primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('type', models.CharField(blank=True, default='DUMMY_CONNECTOR', max_length=64, null=True)),
('oauth_config', models.JSONField(null=True)),
('workspace', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
related_name='oauth_api_keys', to='core.workspace')),
],
options={
'db_table': 'core_oauth_api_keys',
},
),
migrations.AddConstraint(
model_name='oauthapikeys',
constraint=models.UniqueConstraint(fields=('workspace', 'type'), name='unique_oauth_keys'),
),
]
27 changes: 0 additions & 27 deletions core/migrations/0012_oauthapikeys.py

This file was deleted.

17 changes: 0 additions & 17 deletions core/migrations/0013_auto_20240123_1020.py

This file was deleted.

18 changes: 0 additions & 18 deletions core/migrations/0014_connector_oauth_keys.py

This file was deleted.

6 changes: 5 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ class ValmiUserIDJitsuApiToken(models.Model):


class OAuthApiKeys(models.Model):
id = models.UUIDField(primary_key=True, editable=False, default=uuid.UUID("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"))
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
workspace = models.ForeignKey(to=Workspace, on_delete=models.CASCADE, related_name="oauth_api_keys")
type = models.CharField(primary_key=True, max_length=64, null=False, blank=False, default="DUMMY_CONNECTOR")
type = models.CharField(max_length=64, null=True, blank=True, default="DUMMY_CONNECTOR")
oauth_config = models.JSONField(blank=False, null=True)

class Meta:
db_table = 'core_oauth_api_keys'
constraints = [
models.UniqueConstraint(fields=['workspace', 'type'], name='unique_oauth_keys')
]
51 changes: 33 additions & 18 deletions core/oauth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import json
import logging
import uuid


from typing import List

Expand Down Expand Up @@ -60,49 +62,62 @@ def get_config(request, connector_type: str):
@router.post("/workspaces/{workspace_id}/config/create", response={200: OAuthSchema, 400: DetailSchema})
def create_obj(request, workspace_id, payload: OAuthSchemaIn):
data = payload.dict()
logger.debug("create oauth keys_", data)
logger.debug("payload", payload)
try:
data["workspace"] = Workspace.objects.get(id=workspace_id)

workspace = Workspace.objects.get(id=workspace_id)

logger.debug("workspace", workspace.id)

data["id"] = uuid.uuid4()
data["workspace"] = workspace

result = OAuthApiKeys.objects.create(**data)

return result
except Exception:
return {"detail": "The specific oauth key cannot be created."}


@router.put("/workspaces/{workspace_id}/config/update", response={200: OAuthSchema, 400: DetailSchema})
@router.put("/workspaces/{workspace_id}/config/update", response={200: OAuthSchema, 404: Json, 400: DetailSchema})
def update_obj(request, workspace_id, payload: OAuthSchemaUpdateIn):
data = payload.dict()

try:
oauth_key = OAuthApiKeys.objects.filter(type=data.pop("type"))
data["workspace"] = Workspace.objects.get(id=workspace_id)
workspace = Workspace.objects.get(id=workspace_id)

oauth_key.update(**data)
return oauth_key.first()
oauth_key = OAuthApiKeys.objects.filter(workspace=workspace, type=data.pop("type"))

except Exception:
return {"detail": "The specific oauth key cannot be updated."}
if oauth_key.exists():

data["workspace"] = workspace

oauth_key.update(**data)
return oauth_key.first()
else:
response_data = {"detail": "The specific oauth key cannot be updated."}
response_json = json.dumps(response_data)

return (404, response_json)

@router.get("/workspaces/{workspace_id}/keys", response=List[OAuthSchema])
def list_objs(request, workspace_id):
workspace = Workspace.objects.get(id=workspace_id)
queryset = OAuthApiKeys.objects.filter(workspace=workspace)
return queryset
except Exception:
return {"detail": "The specific oauth key cannot be updated."}


@router.get("/workspaces/{workspace_id}/keys/{type}", response={200: List[OAuthSchema], 400: DetailSchema})
@router.get("/workspaces/{workspace_id}/keys/{type}", response={200: List[OAuthSchema], 404: Json, 400: DetailSchema})
def get_obj(request, workspace_id, type):
try:
workspace = Workspace.objects.get(id=workspace_id)

queryset: OAuthSchema = OAuthApiKeys.objects.filter(workspace=workspace, type=type)

if queryset:
if queryset.exists():
return [queryset.first()]
else:
logger.debug("No found")
return []
response_data = {"detail": "The specific oauth key does not exist."}
response_json = json.dumps(response_data)

return (404, response_json)

except Exception:
return {"detail": "The specific oauth key cannot be fetched."}

0 comments on commit 81262d6

Please sign in to comment.