CategoryDigestView
This commit is contained in:
parent
8bf138d8c9
commit
aaa92af032
|
@ -3,10 +3,10 @@ from frog_api import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("projects/", views.ProjectView.as_view()),
|
path("projects/", views.ProjectView.as_view()),
|
||||||
# re_path(
|
re_path(
|
||||||
# "data/(?P<project>.+)/(?P<version>.+)/(?P<category>.+)/$",
|
"data/(?P<project>.+)/(?P<version>.+)/(?P<category>.+)/$",
|
||||||
# views.EntryView.as_view(),
|
views.CategoryDigestView.as_view(),
|
||||||
# ),
|
),
|
||||||
re_path(
|
re_path(
|
||||||
"data/(?P<project>.+)/(?P<version>.+)/$",
|
"data/(?P<project>.+)/(?P<version>.+)/$",
|
||||||
views.VersionDigestView.as_view(),
|
views.VersionDigestView.as_view(),
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from typing import Optional
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from rest_framework.exceptions import APIException
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from frog_api.models import Entry, Project, Version
|
from frog_api.models import Category, Entry, Project, Version
|
||||||
from frog_api.serializers import (
|
from frog_api.serializers import (
|
||||||
ProjectSerializer,
|
ProjectSerializer,
|
||||||
TerseEntrySerializer,
|
TerseEntrySerializer,
|
||||||
|
@ -24,15 +24,36 @@ class ProjectView(APIView):
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
def get_latest_entry(project, version, category) -> Optional[Entry]:
|
def get_latest_entry(project, version, category) -> dict:
|
||||||
return Entry.objects.filter(
|
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(
|
||||||
category__slug=category,
|
category__slug=category,
|
||||||
category__version__slug=version,
|
category__version__slug=version,
|
||||||
category__version__project__slug=project,
|
category__version__project__slug=project,
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
|
if entry is None:
|
||||||
|
raise APIException("Entry is None", code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
def get_entry_json(entry) -> dict:
|
# Re-format the measures (TODO: DRF-ify this?)
|
||||||
entry_data = TerseEntrySerializer(entry).data
|
entry_data = TerseEntrySerializer(entry).data
|
||||||
entry_data["measures"] = {m["type"]: m["value"] for m in entry_data["measures"]}
|
entry_data["measures"] = {m["type"]: m["value"] for m in entry_data["measures"]}
|
||||||
return entry_data
|
return entry_data
|
||||||
|
@ -43,7 +64,7 @@ def get_versions_digest_for_project(project) -> dict:
|
||||||
for version in Version.objects.filter(project__slug=project):
|
for version in Version.objects.filter(project__slug=project):
|
||||||
entry = get_latest_entry(project, version.slug, "total")
|
entry = get_latest_entry(project, version.slug, "total")
|
||||||
if entry is not None:
|
if entry is not None:
|
||||||
versions[version.slug] = get_entry_json(entry)
|
versions[version.slug] = entry
|
||||||
return versions
|
return versions
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,7 +113,7 @@ class ProjectDigestView(APIView):
|
||||||
|
|
||||||
class VersionDigestView(APIView):
|
class VersionDigestView(APIView):
|
||||||
"""
|
"""
|
||||||
API endpoint that returns the most recent entry for a version of a project.
|
API endpoint that returns the most recent entry for overall progress for a version of a project.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get(self, request, project, version):
|
def get(self, request, project, version):
|
||||||
|
@ -100,17 +121,21 @@ class VersionDigestView(APIView):
|
||||||
Return the most recent entry for overall progress for a version of a project.
|
Return the most recent entry for overall progress for a version of a project.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not Project.objects.filter(slug=project).exists():
|
|
||||||
return Response(
|
|
||||||
f"Project {project} not found", status=status.HTTP_404_NOT_FOUND
|
|
||||||
)
|
|
||||||
|
|
||||||
if not Version.objects.filter(slug=version, project__slug=project).exists():
|
|
||||||
return Response(
|
|
||||||
f"Version {version} not found", status=status.HTTP_404_NOT_FOUND
|
|
||||||
)
|
|
||||||
|
|
||||||
entry = get_latest_entry(project, version, "total")
|
entry = get_latest_entry(project, version, "total")
|
||||||
entry_json = get_entry_json(entry)
|
|
||||||
|
|
||||||
return Response(entry_json)
|
return Response(entry)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in New Issue