Skip to content

Commit e5b2cc4

Browse files
sudip-khanalsusilnem
authored andcommitted
EAP: Add api to download template files (#2619)
1 parent 0a7e2fe commit e5b2cc4

File tree

8 files changed

+98
-3
lines changed

8 files changed

+98
-3
lines changed

assets

eap/serializers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from main.writable_nested_serializers import NestedCreateMixin, NestedUpdateMixin
4141
from utils.file_check import validate_file_type
4242

43-
ALLOWED_FILE_EXTENTIONS: list[str] = ["pdf", "docx", "pptx", "xlsx"]
43+
ALLOWED_FILE_EXTENTIONS: list[str] = ["pdf", "docx", "pptx", "xlsx", "xlsm"]
4444

4545

4646
class BaseEAPSerializer(serializers.ModelSerializer):
@@ -218,6 +218,10 @@ class EAPFileInputSerializer(serializers.Serializer):
218218
file = serializers.ListField(child=serializers.FileField(required=True))
219219

220220

221+
class EAPGlobalFilesSerializer(serializers.Serializer):
222+
url = serializers.URLField(read_only=True)
223+
224+
221225
class EAPFileSerializer(BaseEAPSerializer):
222226
id = serializers.IntegerField(required=False)
223227
file = serializers.FileField(required=True)

eap/test_views.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,3 +2308,40 @@ def test_snapshot_full_eap(self):
23082308
orig_actors[0].description,
23092309
snapshot_actors[0].description,
23102310
)
2311+
2312+
2313+
class EAPGlobalFileTestCase(APITestCase):
2314+
def setUp(self):
2315+
super().setUp()
2316+
self.url = "/api/v2/eap/global-files/"
2317+
2318+
def test_get_template_files_invalid_param(self):
2319+
self.authenticate()
2320+
response = self.client.get(f"{self.url}invalid_type/")
2321+
self.assert_400(response)
2322+
self.assertIn("detail", response.data)
2323+
2324+
def test_get_budget_template(self):
2325+
self.authenticate()
2326+
response = self.client.get(f"{self.url}budget_template/")
2327+
self.assert_200(response)
2328+
self.assertIn("url", response.data)
2329+
self.assertTrue(response.data["url"].endswith("files/eap/budget_template.xlsm"))
2330+
2331+
def test_get_forecast_table_template(self):
2332+
self.authenticate()
2333+
response = self.client.get(f"{self.url}forecast_table/")
2334+
self.assert_200(response)
2335+
self.assertIn("url", response.data)
2336+
self.assertTrue(response.data["url"].endswith("files/eap/forecasts_table.docx"))
2337+
2338+
def test_get_theory_of_change_template(self):
2339+
self.authenticate()
2340+
response = self.client.get(f"{self.url}theory_of_change_table/")
2341+
self.assert_200(response)
2342+
self.assertIn("url", response.data)
2343+
self.assertTrue(response.data["url"].endswith("files/eap/theory_of_change_table.docx"))
2344+
2345+
def test_get_template_files_unauthenticated(self):
2346+
response = self.client.get(f"{self.url}budget_template/")
2347+
self.assert_401(response)

eap/views.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Create your views here.
22
from django.db.models import Case, F, IntegerField, When
33
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
56
from rest_framework import mixins, permissions, response, status, viewsets
67
from rest_framework.decorators import action
78

@@ -29,6 +30,7 @@
2930
from eap.serializers import (
3031
EAPFileInputSerializer,
3132
EAPFileSerializer,
33+
EAPGlobalFilesSerializer,
3234
EAPRegistrationSerializer,
3335
EAPStatusSerializer,
3436
EAPValidatedBudgetFileSerializer,
@@ -324,3 +326,54 @@ def multiple_file(self, request):
324326
file_serializer.save()
325327
return response.Response(file_serializer.data, status=status.HTTP_201_CREATED)
326328
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)
279 KB
Binary file not shown.
35 KB
Binary file not shown.
38.3 KB
Binary file not shown.

main/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
router.register(r"simplified-eap", eap_views.SimplifiedEAPViewSet, basename="simplified_eap")
200200
router.register(r"full-eap", eap_views.FullEAPViewSet, basename="full_eap")
201201
router.register(r"eap-file", eap_views.EAPFileViewSet, basename="eap_file")
202+
router.register(r"eap/global-files", eap_views.EAPGlobalFilesViewSet, basename="eap_global_files")
202203

203204
admin.site.site_header = "IFRC Go administration"
204205
admin.site.site_title = "IFRC Go admin"

0 commit comments

Comments
 (0)