2022-08-26 02:02:16 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.db import models
|
2022-08-27 06:34:59 +00:00
|
|
|
from frog_api.exceptions import AlreadyExistsException
|
2022-08-26 02:02:16 +00:00
|
|
|
from frog_api.models import Category, Project
|
2022-08-27 06:34:59 +00:00
|
|
|
from frog_api.serializers.model_serializers import ProjectSerializer
|
|
|
|
from frog_api.serializers.request_serializers import CreateCategoriesSerializer
|
2022-08-26 02:02:16 +00:00
|
|
|
from frog_api.views.common import get_project, get_version, validate_api_key
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectStructureView(APIView):
|
|
|
|
"""
|
|
|
|
API endpoint that allows projects to be viewed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request: Request) -> Response:
|
|
|
|
"""
|
|
|
|
Return a list of all projects.
|
|
|
|
"""
|
|
|
|
projects = Project.objects.all()
|
|
|
|
serializer = ProjectSerializer(projects, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryStructureView(APIView):
|
|
|
|
"""
|
|
|
|
API endpoint for adding new categories
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_categories(
|
|
|
|
req_data: dict[str, Any], project_slug: str, version_slug: str
|
|
|
|
) -> int:
|
2022-08-27 06:34:59 +00:00
|
|
|
request_ser = CreateCategoriesSerializer(data=req_data)
|
|
|
|
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-08-27 06:34:59 +00:00
|
|
|
categories: dict[str, str] = data["categories"]
|
2022-08-26 02:02:16 +00:00
|
|
|
|
|
|
|
to_save: list[models.Model] = []
|
2022-08-27 06:34:59 +00:00
|
|
|
for cat, name in categories.items():
|
2022-08-26 02:02:16 +00:00
|
|
|
if Category.objects.filter(slug=cat, version=version).exists():
|
|
|
|
raise AlreadyExistsException(
|
|
|
|
f"Category {cat} already exists for project '{project_slug}', version '{version_slug}'"
|
|
|
|
)
|
2022-08-27 06:34:59 +00:00
|
|
|
to_save.append(Category(version=version, slug=cat, name=name))
|
2022-08-26 02:02:16 +00:00
|
|
|
|
|
|
|
for s in to_save:
|
|
|
|
s.save()
|
|
|
|
|
|
|
|
return len(to_save)
|
|
|
|
|
|
|
|
def post(self, request: Request, project_slug: str, version_slug: str) -> Response:
|
|
|
|
result = CategoryStructureView.create_categories(
|
|
|
|
request.data, project_slug, version_slug
|
|
|
|
)
|
|
|
|
|
|
|
|
success_data = {
|
|
|
|
"result": "success",
|
|
|
|
"wrote": result,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response(success_data, status=status.HTTP_201_CREATED)
|