Skip to content

Commit

Permalink
Endpoint to check agreement signature
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Jul 16, 2024
1 parent 05d071f commit 31bc713
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 31 deletions.
13 changes: 6 additions & 7 deletions api/fixtures/repositories.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
"pk": 1,
"fields": {
"contributor": 1,
"points": 10,
"gh_id": "10274252"
"gh_id": 10274252,
"points": 10
}
},
{
"model": "api.repository",
"pk": 2,
"fields": {
"contributor": 2,
"points": 20,
"gh_id": "102097552"

"gh_id": 102097552,
"points": 20
}
},
{
"model": "api.repository",
"pk": 3,
"fields": {
"contributor": 3,
"points": 30,
"gh_id": "890732552"
"gh_id": 890732552,
"points": 30
}
}
]
2 changes: 1 addition & 1 deletion api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.25 on 2024-07-12 15:33
# Generated by Django 3.2.25 on 2024-07-16 12:27

from django.db import migrations, models
import django.db.models.deletion
Expand Down
14 changes: 10 additions & 4 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
from codeforlife.urls import get_urlpatterns
from rest_framework.routers import DefaultRouter

from .views import CheckAgreementViewSet
from .views import AgreementSignatureViewSet, ContributorViewSet

router = DefaultRouter()

router.register(
"check-agreement",
CheckAgreementViewSet,
basename="CheckAgreementViewSet",
"agreements",
AgreementSignatureViewSet,
basename="agreement",
)

router.register(
"contributors",
ContributorViewSet,
basename="contributor",
)


Expand Down
3 changes: 2 additions & 1 deletion api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
Created on 02/07/2024 at 11:59:45(+01:00).
"""

from .check_agreement import CheckAgreementViewSet
from .agreement_signature import AgreementSignatureViewSet
from .contributor import ContributorViewSet
53 changes: 35 additions & 18 deletions api/views/check_agreement.py → api/views/agreement_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,41 @@
from codeforlife.permissions import AllowAny
from codeforlife.response import Response
from codeforlife.user.models import User
from codeforlife.views import ModelViewSet

# from django.http import HttpResponse
from codeforlife.views import ModelViewSet, action
from rest_framework import status

from ..models import AgreementSignature, Contributor
import settings

# from rest_framework.views import APIView
from ..models import AgreementSignature, Contributor
from ..serializers import AgreementSignatureSerializer


class CheckAgreementViewSet(ModelViewSet[User, Contributor]):
class AgreementSignatureViewSet(ModelViewSet[User, AgreementSignature]):
"""
An endpoint to check if a contributor has signed latest agreement,
return OKAY if he has otherwise return the latest commit ID.
"""

# http_method_names = ["get"]
queryset = AgreementSignature.objects.all()
permission_classes = [AllowAny]

def get(self, request):
serializer_class = AgreementSignatureSerializer

@action(
detail=False,
methods=["get"],
url_path="check-signed/(?P<contributor_id>.+)",
)
def check_signed(self, request, **url_params: str):
"""
Get the latest commit id and compare with contributor's
agreement signature.
"""
# Repo information
github_id = 118008817 # TODO: Change later
owner = "ocadotechnology"
repo = "codeforlife-workshop"
file_name = "CONTRIBUTING.md"
github_id = url_params["contributor_id"] # Use id=1 for testing
owner = settings.OWNER
repo = settings.REPO_NAME
file_name = settings.FILE_NAME

params: Dict[str, str]
params = {"path": file_name, "per_page": 1}
Expand All @@ -55,19 +62,29 @@ def get(self, request):
)

# Retrieve contributor
contributor = Contributor.objects.get(id=github_id)
if not contributor:
return Response(status=status.HTTP_404_NOT_FOUND)
try:
contributor = Contributor.objects.get(id=github_id)
except Contributor.DoesNotExist:
return Response(
data={"outcome: ": "Contributor does not exist"},
status=status.HTTP_404_NOT_FOUND,
)

# Retrieve signature agreement IDs
signatures = AgreementSignature.objects.filter(contributor=contributor)
latest_signature = signatures.order_by("-signed_by").first()
latest_signature = signatures.order_by("-signed_at").first()
if not latest_signature:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(
data={"outcome: ": "No Agreement Signatures found."},
status=status.HTTP_404_NOT_FOUND,
)

# Compare agreement IDs
if latest_commit_id == latest_signature.agreement_id:
return Response(status=status.HTTP_200_OK)
return Response(
data={"Outcome:": "Successful"},
status=status.HTTP_200_OK,
)

return Response(
data={"latest_commit_id: ": latest_commit_id},
Expand Down
18 changes: 18 additions & 0 deletions api/views/contributor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
© Ocado Group
Created on 16/07/2024 at 11:03:09(+01:00).
"""

from codeforlife.permissions import AllowAny
from codeforlife.user.models import User
from codeforlife.views import ModelViewSet

from ..models import Contributor
from ..serializers import ContributorSerializer


# pylint: disable-next=missing-class-docstring,too-many-ancestors
class ContributorViewSet(ModelViewSet[User, Contributor]):
permission_classes = [AllowAny]
serializer_class = ContributorSerializer
queryset = Contributor.objects.all()
5 changes: 5 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
# pylint: disable-next=wildcard-import,unused-wildcard-import
from codeforlife.settings import *

# Repo information
OWNER = "ocadotechnology" # cspell:disable-line
REPO_NAME = "codeforlife-workspace"
FILE_NAME = "CONTRIBUTING.md"

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent

Expand Down

0 comments on commit 31bc713

Please sign in to comment.