Skip to content

Commit abb546a

Browse files
committed
Add a json output with open calls. Same list as on front page in json format.
1 parent dbc62f0 commit abb546a

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

hypha/apply/funds/models/applications.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import date
22
from typing import Optional
33

4+
import nh3
45
from django import forms
56
from django.conf import settings
67
from django.contrib.postgres.fields import ArrayField
@@ -73,10 +74,40 @@ def order_by_end_date(self):
7374
return qs.order_by("end_date")
7475

7576

77+
class AsJsonMixin:
78+
@cached_property
79+
def as_json(self):
80+
# Clean the strings in title and description.
81+
title = nh3.clean(self.title, tags=set())
82+
description = nh3.clean(self.description, tags=set())
83+
# If image exist scale it down and convert to webp.
84+
image = (
85+
self.image.get_rendition("max-1200x1200|format-webp|webpquality-60").url
86+
if self.image
87+
else ""
88+
)
89+
# Make sure weight is an int.
90+
weight = int(self.weight)
91+
# If next deadline exist and is set to show, format it as standard iso date.
92+
try:
93+
next_deadline = (
94+
self.next_deadline().isoformat() if self.show_deadline else ""
95+
)
96+
except AttributeError:
97+
next_deadline = ""
98+
return {
99+
"title": title,
100+
"description": description,
101+
"image": image,
102+
"weight": weight,
103+
"next_deadline": next_deadline,
104+
}
105+
106+
76107
@method_decorator(
77108
ratelimit(key="ip", rate=settings.DEFAULT_RATE_LIMIT, method="POST"), name="serve"
78109
)
79-
class ApplicationBase(EmailForm, WorkflowStreamForm): # type: ignore
110+
class ApplicationBase(EmailForm, WorkflowStreamForm, AsJsonMixin): # type: ignore
80111
is_creatable = False
81112

82113
# Adds validation around forms & workflows. Isn't on Workflow class due to not displaying workflow field on Round
@@ -555,7 +586,7 @@ def serve(self, request, *args, **kwargs):
555586
@method_decorator(
556587
ratelimit(key="ip", rate=settings.DEFAULT_RATE_LIMIT, method="POST"), name="serve"
557588
)
558-
class LabBase(EmailForm, WorkflowStreamForm, SubmittableStreamForm): # type: ignore
589+
class LabBase(EmailForm, WorkflowStreamForm, SubmittableStreamForm, AsJsonMixin): # type: ignore
559590
is_creatable = False
560591
submission_class = ApplicationSubmission
561592

hypha/home/views.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from django.conf import settings
2+
from django.http import JsonResponse
13
from django.shortcuts import render
4+
from django_ratelimit.decorators import ratelimit
25

36
from hypha.apply.funds.models import ApplicationBase, LabBase
47

@@ -18,3 +21,22 @@ def home(request):
1821
fund for fund in all_funds if fund.open_round and fund.list_on_front_page
1922
]
2023
return render(request, "home/home.html", ctx)
24+
25+
26+
@ratelimit(key="ip", rate=settings.DEFAULT_RATE_LIMIT)
27+
def open_calls_json(request):
28+
"""Open calls in JSON format.
29+
30+
List open calls in JSON format, useful when you want to list open calls on an external site.
31+
"""
32+
rounds = ApplicationBase.objects.order_by_end_date().specific()
33+
labs = LabBase.objects.public().live().specific()
34+
all_funds = list(rounds) + list(labs)
35+
36+
# Only pass rounds/labs that are open & visible for the front page
37+
data = [
38+
fund.as_json
39+
for fund in all_funds
40+
if fund.open_round and fund.list_on_front_page
41+
]
42+
return JsonResponse(data, safe=False)

hypha/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
from hypha.apply.users.urls import urlpatterns as user_urls
1616
from hypha.apply.users.views import become, oauth_complete
1717
from hypha.apply.utils.views import custom_wagtail_page_delete
18-
from hypha.home.views import home
18+
from hypha.home.views import home, open_calls_json
1919

2020
urlpatterns = [
2121
path("", home, name="home"),
22+
path("open-calls.json", open_calls_json),
2223
path("apply/", include("hypha.apply.funds.urls", "apply")),
2324
path("activity/", include("hypha.apply.activity.urls", "activity")),
2425
path("todo/", include("hypha.apply.todo.urls", "todo")),

0 commit comments

Comments
 (0)