2022-08-26 02:02:16 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2022-08-27 06:34:59 +00:00
|
|
|
from frog_api.exceptions import AlreadyExistsException
|
2022-08-28 13:59:52 +00:00
|
|
|
from frog_api.models import Category, Project, Version
|
2022-08-27 06:34:59 +00:00
|
|
|
from frog_api.serializers.model_serializers import ProjectSerializer
|
2022-08-28 13:59:52 +00:00
|
|
|
from frog_api.serializers.request_serializers import (
|
2022-09-22 20:40:44 +00:00
|
|
|
CreateCategorySerializer,
|
2022-08-28 13:59:52 +00:00
|
|
|
CreateProjectSerializer,
|
|
|
|
CreateVersionSerializer,
|
|
|
|
)
|
|
|
|
from frog_api.views.common import (
|
|
|
|
get_project,
|
|
|
|
get_version,
|
|
|
|
validate_api_key,
|
|
|
|
validate_ultimate_api_key,
|
|
|
|
)
|
2022-08-26 02:02:16 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
2022-08-28 13:59:52 +00:00
|
|
|
from frog_api.views.data import DEFAULT_CATEGORY_NAME, DEFAULT_CATEGORY_SLUG
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-08-28 13:59:52 +00:00
|
|
|
|
|
|
|
class RootStructureView(APIView):
|
2022-09-22 20:40:44 +00:00
|
|
|
def get(self, request: Request, format: Any = None) -> Response:
|
2022-08-26 02:02:16 +00:00
|
|
|
"""
|
2022-09-22 20:40:44 +00:00
|
|
|
Get a list of all projects
|
2022-08-26 02:02:16 +00:00
|
|
|
"""
|
|
|
|
projects = Project.objects.all()
|
|
|
|
serializer = ProjectSerializer(projects, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
class ProjectStructureView(APIView):
|
|
|
|
"""
|
|
|
|
API endpoint for modifying projects
|
|
|
|
"""
|
|
|
|
|
|
|
|
def post(self, request: Request, project_slug: str) -> Response:
|
2022-08-28 13:59:52 +00:00
|
|
|
"""
|
|
|
|
Create a new project.
|
|
|
|
"""
|
|
|
|
request_ser = CreateProjectSerializer(data=request.data)
|
|
|
|
|
|
|
|
validate_ultimate_api_key(request_ser.data["api_key"])
|
|
|
|
|
|
|
|
if request_ser.is_valid():
|
|
|
|
request_ser.project.save()
|
|
|
|
return Response(request_ser.project.data, status=status.HTTP_201_CREATED)
|
|
|
|
return Response(request_ser.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
class VersionStructureView(APIView):
|
2022-08-28 13:59:52 +00:00
|
|
|
"""
|
2022-09-22 20:40:44 +00:00
|
|
|
API endpoint for modifying versions
|
2022-08-28 13:59:52 +00:00
|
|
|
"""
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
def post(self, request: Request, project_slug: str, version_slug: str) -> Response:
|
2022-08-28 13:59:52 +00:00
|
|
|
request_ser = CreateVersionSerializer(data=request.data)
|
|
|
|
|
|
|
|
project = get_project(project_slug)
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
if not request_ser.is_valid():
|
|
|
|
return Response(request_ser.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2022-08-28 13:59:52 +00:00
|
|
|
validate_api_key(request_ser.data["api_key"], project)
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
if Version.objects.filter(slug=version_slug, project=project).exists():
|
|
|
|
raise AlreadyExistsException(
|
|
|
|
f"Version {version_slug} already exists in project {project_slug}"
|
2022-08-28 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
version = Version(
|
|
|
|
project=project,
|
|
|
|
slug=version_slug,
|
|
|
|
name=request_ser.data["name"],
|
|
|
|
)
|
|
|
|
version.save()
|
2022-08-28 13:59:52 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
# Create the default category
|
|
|
|
default_cat = Category(
|
|
|
|
version=version,
|
|
|
|
slug=DEFAULT_CATEGORY_SLUG,
|
|
|
|
name=DEFAULT_CATEGORY_NAME,
|
|
|
|
)
|
|
|
|
default_cat.save()
|
|
|
|
return Response(status=status.HTTP_201_CREATED)
|
2022-08-28 13:59:52 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
def delete(
|
|
|
|
self, request: Request, project_slug: str, version_slug: str
|
|
|
|
) -> Response:
|
|
|
|
project = get_project(project_slug)
|
|
|
|
|
|
|
|
validate_api_key(request.data["api_key"], project)
|
|
|
|
|
|
|
|
version = get_version(version_slug, project)
|
|
|
|
|
|
|
|
version.delete()
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryStructureView(APIView):
|
2022-08-26 02:02:16 +00:00
|
|
|
"""
|
2022-09-22 20:40:44 +00:00
|
|
|
API endpoint for modifying categories
|
2022-08-26 02:02:16 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
2022-09-22 20:40:44 +00:00
|
|
|
def create_category(
|
|
|
|
req_data: dict[str, Any],
|
|
|
|
project_slug: str,
|
|
|
|
version_slug: str,
|
|
|
|
category_slug: str,
|
2022-08-26 02:02:16 +00:00
|
|
|
) -> int:
|
2022-09-22 20:40:44 +00:00
|
|
|
request_ser = CreateCategorySerializer(data=req_data)
|
2022-08-27 06:34:59 +00:00
|
|
|
request_ser.is_valid(raise_exception=True)
|
|
|
|
data = request_ser.data
|
|
|
|
|
2022-08-26 02:02:16 +00:00
|
|
|
project = get_project(project_slug)
|
|
|
|
|
2022-08-27 06:34:59 +00:00
|
|
|
validate_api_key(request_ser.data["api_key"], project)
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-08-27 06:34:59 +00:00
|
|
|
version = get_version(version_slug, project)
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
if Category.objects.filter(slug=category_slug, version=version).exists():
|
|
|
|
raise AlreadyExistsException(
|
|
|
|
f"Category '{category_slug}' already exists for project '{project_slug}', version '{version_slug}'"
|
|
|
|
)
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
category = Category(version=version, slug=category_slug, name=data["name"])
|
|
|
|
category.save()
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
return 1
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
def post(
|
|
|
|
self, request: Request, project_slug: str, version_slug: str, category_slug: str
|
|
|
|
) -> Response:
|
|
|
|
result = CategoryStructureView.create_category(
|
|
|
|
request.data, project_slug, version_slug, category_slug
|
2022-08-26 02:02:16 +00:00
|
|
|
)
|
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
return Response(status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
def delete(
|
|
|
|
self, request: Request, project_slug: str, version_slug: str, category_slug: str
|
|
|
|
) -> Response:
|
|
|
|
project = get_project(project_slug)
|
|
|
|
|
|
|
|
validate_api_key(request.data["api_key"], project)
|
|
|
|
|
|
|
|
version = get_version(version_slug, project)
|
|
|
|
|
|
|
|
category = Category.objects.get(slug=category_slug, version=version)
|
2022-08-26 02:02:16 +00:00
|
|
|
|
2022-09-22 20:40:44 +00:00
|
|
|
category.delete()
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|