|
1 | 1 | # Create your views here. |
2 | 2 | from django.db.models import Case, F, IntegerField, When |
3 | 3 | from django.db.models.query import Prefetch, QuerySet |
4 | | -from drf_spectacular.utils import extend_schema |
| 4 | +from django.templatetags.static import static |
| 5 | +from drf_spectacular.utils import OpenApiParameter, extend_schema |
5 | 6 | from rest_framework import mixins, permissions, response, status, viewsets |
6 | 7 | from rest_framework.decorators import action |
7 | 8 |
|
|
29 | 30 | from eap.serializers import ( |
30 | 31 | EAPFileInputSerializer, |
31 | 32 | EAPFileSerializer, |
| 33 | + EAPGlobalFilesSerializer, |
32 | 34 | EAPRegistrationSerializer, |
33 | 35 | EAPStatusSerializer, |
34 | 36 | EAPValidatedBudgetFileSerializer, |
@@ -324,3 +326,54 @@ def multiple_file(self, request): |
324 | 326 | file_serializer.save() |
325 | 327 | return response.Response(file_serializer.data, status=status.HTTP_201_CREATED) |
326 | 328 | return response.Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 329 | + |
| 330 | + |
| 331 | +class EAPGlobalFilesViewSet( |
| 332 | + mixins.RetrieveModelMixin, |
| 333 | + viewsets.GenericViewSet, |
| 334 | +): |
| 335 | + |
| 336 | + serializer_class = EAPGlobalFilesSerializer |
| 337 | + permission_classes = permissions.IsAuthenticated, DenyGuestUserPermission |
| 338 | + |
| 339 | + lookup_field = "template_type" |
| 340 | + lookup_url_kwarg = "template_type" |
| 341 | + |
| 342 | + template_map = { |
| 343 | + "budget_template": "files/eap/budget_template.xlsm", |
| 344 | + "forecast_table": "files/eap/forecasts_table.docx", |
| 345 | + "theory_of_change_table": "files/eap/theory_of_change_table.docx", |
| 346 | + } |
| 347 | + |
| 348 | + @extend_schema( |
| 349 | + request=None, |
| 350 | + responses=EAPGlobalFilesSerializer, |
| 351 | + parameters=[ |
| 352 | + OpenApiParameter( |
| 353 | + name="template_type", |
| 354 | + location=OpenApiParameter.PATH, |
| 355 | + description="Type of EAP template to download", |
| 356 | + required=True, |
| 357 | + type=str, |
| 358 | + enum=list(template_map.keys()), |
| 359 | + ) |
| 360 | + ], |
| 361 | + ) |
| 362 | + def retrieve(self, request, *args, **kwargs): |
| 363 | + template_type = kwargs.get("template_type") |
| 364 | + if not template_type: |
| 365 | + return response.Response( |
| 366 | + { |
| 367 | + "detail": "Template file type not found.", |
| 368 | + }, |
| 369 | + status=400, |
| 370 | + ) |
| 371 | + if template_type not in self.template_map: |
| 372 | + return response.Response( |
| 373 | + { |
| 374 | + "detail": f"Invalid template file type '{template_type}'.Please use one of the following values:{(self.template_map.keys())}." # noqa |
| 375 | + }, |
| 376 | + status=400, |
| 377 | + ) |
| 378 | + serializer = EAPGlobalFilesSerializer({"url": request.build_absolute_uri(static(self.template_map[template_type]))}) |
| 379 | + return response.Response(serializer.data) |
0 commit comments