-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from DalgoT4D/create-orgtasks
Create org task
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""All the master data api cane found here""" | ||
|
||
from ninja import NinjaAPI | ||
from ninja.errors import HttpError | ||
from django.forms.models import model_to_dict | ||
|
||
from ninja.errors import ValidationError | ||
from ninja.responses import Response | ||
from pydantic.error_wrappers import ValidationError as PydanticValidationError | ||
|
||
|
||
from ddpui import auth | ||
from ddpui.models.tasks import Task | ||
from ddpui.utils.custom_logger import CustomLogger | ||
from ddpui.utils.constants import ( | ||
TASK_DBTRUN, | ||
TASK_GITPULL, | ||
) | ||
from ddpui.ddpdbt import dbt_service | ||
|
||
dataapi = NinjaAPI(urls_namespace="master_data") | ||
# http://127.0.0.1:8000/api/docs | ||
|
||
|
||
logger = CustomLogger("ddpui") | ||
|
||
|
||
@dataapi.exception_handler(ValidationError) | ||
def ninja_validation_error_handler(request, exc): # pylint: disable=unused-argument | ||
""" | ||
Handle any ninja validation errors raised in the apis | ||
These are raised during request payload validation | ||
exc.errors is correct | ||
""" | ||
return Response({"detail": exc.errors}, status=422) | ||
|
||
|
||
@dataapi.exception_handler(PydanticValidationError) | ||
def pydantic_validation_error_handler( | ||
request, exc: PydanticValidationError | ||
): # pylint: disable=unused-argument | ||
""" | ||
Handle any pydantic errors raised in the apis | ||
These are raised during response payload validation | ||
exc.errors() is correct | ||
""" | ||
print(exc) | ||
return Response({"detail": exc.errors()}, status=500) | ||
|
||
|
||
@dataapi.exception_handler(Exception) | ||
def ninja_default_error_handler( | ||
request, exc: Exception # skipcq PYL-W0613 | ||
): # pylint: disable=unused-argument | ||
"""Handle any other exception raised in the apis""" | ||
logger.info(exc) | ||
return Response({"detail": "something went wrong"}, status=500) | ||
|
||
|
||
@dataapi.get("/tasks/", auth=auth.CanManagePipelines()) | ||
def get_tasks(request): | ||
"""Fetch master list of tasks related to transformation""" | ||
tasks = [ | ||
model_to_dict(task, exclude=["id"]) | ||
for task in Task.objects.filter(type__in=["dbt", "git"]).all() | ||
] | ||
return tasks | ||
|
||
|
||
@dataapi.get("/tasks/{slug}/config/", auth=auth.CanManagePipelines()) | ||
def get_task_config(request, slug): | ||
"""Get task config which details about the parameters that can be added/used while running it""" | ||
task = Task.objects.filter(slug=slug).first() | ||
|
||
if not task: | ||
raise HttpError(404, "Task not found") | ||
|
||
return dbt_service.task_config_params(task) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters