frogress/frog_api/views.py

142 lines
4.2 KiB
Python
Raw Normal View History

2022-08-21 11:25:44 +00:00
from rest_framework import status
2022-08-24 01:58:55 +00:00
from rest_framework.exceptions import APIException
2022-08-19 10:35:05 +00:00
from rest_framework.response import Response
from rest_framework.views import APIView
2022-08-24 01:58:55 +00:00
from frog_api.models import Category, Entry, Project, Version
2022-08-21 11:25:44 +00:00
from frog_api.serializers import (
ProjectSerializer,
TerseEntrySerializer,
)
2022-08-19 10:35:05 +00:00
class ProjectView(APIView):
"""
2022-08-21 11:25:44 +00:00
API endpoint that allows projects to be viewed.
2022-08-19 10:35:05 +00:00
"""
def get(self, request, format=None):
"""
Return a list of all projects.
"""
projects = Project.objects.all()
serializer = ProjectSerializer(projects, many=True)
return Response(serializer.data)
2022-08-21 11:25:44 +00:00
2022-08-24 01:58:55 +00:00
def get_latest_entry(project, version, category) -> dict:
if not Project.objects.filter(slug=project).exists():
raise APIException(
f"Project {project} not found", code=status.HTTP_404_NOT_FOUND
)
if not Version.objects.filter(slug=version, project__slug=project).exists():
raise APIException(
f"Version '{version}' not found for project '{project}'",
code=status.HTTP_404_NOT_FOUND,
)
if not Category.objects.filter(
slug=category, version__slug=version, version__project__slug=project
).exists():
raise APIException(
f"Category '{category}' not found for project '{project}' and version '{version}'",
code=status.HTTP_404_NOT_FOUND,
)
entry = Entry.objects.filter(
2022-08-21 11:25:44 +00:00
category__slug=category,
category__version__slug=version,
category__version__project__slug=project,
).first()
2022-08-24 01:58:55 +00:00
if entry is None:
raise APIException("Entry is None", code=status.HTTP_404_NOT_FOUND)
2022-08-21 11:25:44 +00:00
2022-08-24 01:58:55 +00:00
# Re-format the measures (TODO: DRF-ify this?)
2022-08-22 08:54:38 +00:00
entry_data = TerseEntrySerializer(entry).data
entry_data["measures"] = {m["type"]: m["value"] for m in entry_data["measures"]}
return entry_data
2022-08-21 11:25:44 +00:00
def get_versions_digest_for_project(project) -> dict:
versions = {}
for version in Version.objects.filter(project__slug=project):
entry = get_latest_entry(project, version.slug, "total")
if entry is not None:
2022-08-24 01:58:55 +00:00
versions[version.slug] = entry
2022-08-21 11:25:44 +00:00
return versions
class RootDigestView(APIView):
"""
API endpoint that returns the most recent entry for each version of each project.
"""
def get(self, request):
"""
Return the most recent entry for ovreall progress for each version of each project.
"""
projects = {}
for project in Project.objects.all():
versions = get_versions_digest_for_project(project.slug)
if len(versions) > 0:
projects[project.slug] = versions
return Response({"progress": projects})
class ProjectDigestView(APIView):
"""
API endpoint that returns the most recent entry for each version of a project.
"""
def get(self, request, project):
"""
2022-08-22 02:01:56 +00:00
Return the most recent entry for overall progress for each version of a project.
2022-08-21 11:25:44 +00:00
"""
2022-08-22 08:54:38 +00:00
if not Project.objects.filter(slug=project).exists():
return Response(
f"Project {project} not found", status=status.HTTP_404_NOT_FOUND
)
2022-08-21 11:25:44 +00:00
projects = {}
versions = get_versions_digest_for_project(project)
if len(versions) > 0:
projects[project] = versions
return Response({"progress": projects})
2022-08-22 08:54:38 +00:00
class VersionDigestView(APIView):
"""
2022-08-24 01:58:55 +00:00
API endpoint that returns the most recent entry for overall progress for a version of a project.
2022-08-22 08:54:38 +00:00
"""
def get(self, request, project, version):
"""
Return the most recent entry for overall progress for a version of a project.
"""
2022-08-24 01:58:55 +00:00
entry = get_latest_entry(project, version, "total")
2022-08-22 08:54:38 +00:00
2022-08-24 01:58:55 +00:00
return Response(entry)
2022-08-22 08:54:38 +00:00
2022-08-24 01:58:55 +00:00
class CategoryDigestView(APIView):
"""
API endpoint that returns the most recent entry for a specific cagory and a version of a project.
"""
def get(self, request, project, version, category):
"""
Return the most recent entry for a specific cagory and a version of a project.
"""
entry = get_latest_entry(project, version, category)
return Response(entry)