|
| 1 | +"""Handles grabbing programs and program details from GPP.""" |
| 2 | + |
| 3 | +__all__ = ["GPPProgramViewSet"] |
| 4 | + |
| 5 | +from asgiref.sync import async_to_sync |
| 6 | +from django.conf import settings |
| 7 | +from gpp_client import GPPClient |
| 8 | +from rest_framework import permissions |
| 9 | +from rest_framework.exceptions import PermissionDenied |
| 10 | +from rest_framework.request import Request |
| 11 | +from rest_framework.response import Response |
| 12 | +from rest_framework.viewsets import GenericViewSet, mixins |
| 13 | + |
| 14 | + |
| 15 | +class GPPProgramViewSet( |
| 16 | + GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin |
| 17 | +): |
| 18 | + serializer_class = None |
| 19 | + permission_classes = [permissions.IsAuthenticated] |
| 20 | + queryset = None |
| 21 | + |
| 22 | + def list(self, request: Request, *args, **kwargs) -> Response: |
| 23 | + """Return a list of GPP programs associated with the authenticated user. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + request : Request |
| 28 | + The HTTP request object, including user context. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + Response |
| 33 | + A DRF Response object containing a list of GPP programs. |
| 34 | +
|
| 35 | + Raises |
| 36 | + ------ |
| 37 | + PermissionDenied |
| 38 | + If the authenticated user has not configured GPP login credentials. |
| 39 | + """ |
| 40 | + if not hasattr(request.user, "gpplogin"): |
| 41 | + raise PermissionDenied( |
| 42 | + "GPP login credentials are not configured for this user." |
| 43 | + ) |
| 44 | + credentials = request.user.gpplogin |
| 45 | + |
| 46 | + # Setup client to communicate with GPP. |
| 47 | + client = GPPClient(url=settings.GPP_URL, token=credentials.token) |
| 48 | + programs = async_to_sync(client.program.get_all)() |
| 49 | + |
| 50 | + return Response(programs) |
| 51 | + |
| 52 | + def retrieve(self, request: Request, *args, **kwargs) -> Response: |
| 53 | + """Return details for a specific GPP program by program ID. |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + request : Request |
| 58 | + The HTTP request object, including user context. |
| 59 | +
|
| 60 | + Returns |
| 61 | + ------- |
| 62 | + Response |
| 63 | + A DRF Response object containing the details of the requested program. |
| 64 | +
|
| 65 | + Raises |
| 66 | + ------ |
| 67 | + PermissionDenied |
| 68 | + If the authenticated user has not configured GPP login credentials. |
| 69 | + KeyError |
| 70 | + If 'pk' (the program ID) is not present in kwargs. |
| 71 | + """ |
| 72 | + program_id = kwargs["pk"] |
| 73 | + |
| 74 | + # Check if GPP credentials have been added. |
| 75 | + if not hasattr(request.user, "gpplogin"): |
| 76 | + # Return with the proper error |
| 77 | + raise PermissionDenied( |
| 78 | + "GPP login credentials are not configured for this user." |
| 79 | + ) |
| 80 | + credentials = request.user.gpplogin |
| 81 | + |
| 82 | + # Setup client to communicate with GPP. |
| 83 | + client = GPPClient(url=settings.GPP_URL, token=credentials.token) |
| 84 | + program = async_to_sync(client.program.get_by_id)(program_id=program_id) |
| 85 | + |
| 86 | + return Response(program) |
0 commit comments