Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions hypha/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def open_calls_json(request):

List open calls in JSON format, useful when you want to list open calls on an external site.
"""
rounds = ApplicationBase.objects.order_by_end_date().specific()
labs = LabBase.objects.public().live().specific()
all_funds = list(rounds) + list(labs)
funds = (
ApplicationBase.objects.order_by_end_date()
.filter(list_on_front_page=True)
.specific()
.with_open_round()
)
labs = LabBase.objects.public().live().filter(list_on_front_page=True).specific()

# Only pass rounds/labs that are open & visible for the front page
data = [
fund.as_json
for fund in all_funds
if fund.open_round and fund.list_on_front_page
]
data = [fund.as_json for fund in funds + list(labs)]
return JsonResponse(data, safe=False)
44 changes: 42 additions & 2 deletions hypha/apply/funds/models/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,44 @@ def order_by_end_date(self):
)
return qs.order_by("end_date")

def with_open_round(self):
"""Return the funds in this queryset that have an open round.

Sets open_round on each fund from a single query for all funds,
instead of one query per fund via ApplicationBase.open_round.

Wagtail pages store their tree position as a materialised path made
of fixed-width steps, one per level (steplen = 4), e.g.
000100020005 a fund
0001000200050003 a round under that fund
Removing the last step from a round's path gives its parent fund's path.

A fund should only have one open round, but if it has several,
setdefault() keeps the first one. Ordering by path makes that the same
round ApplicationBase.open_round (child_of(fund).open().first()) gives.

The rounds are loaded with only path and end_date, which is all
next_deadline() needs.
"""
funds = list(self)
rounds = (
RoundBase.objects.open(specific=False)
.annotate(parent_path=Left("path", Length("path") - RoundBase.steplen))
.filter(parent_path__in=[fund.path for fund in funds])
.order_by("path")
.only("path", "end_date")
)
open_rounds = {}
for open_round in rounds:
open_rounds.setdefault(open_round.parent_path, open_round)

funds_with_open_round = []
for fund in funds:
fund.open_round = open_rounds.get(fund.path)
if fund.open_round:
funds_with_open_round.append(fund)
return funds_with_open_round


class AsJsonMixin:
@cached_property
Expand Down Expand Up @@ -236,8 +274,10 @@ def new(self):
rounds = rounds.filter(start_date__gt=date.today())
return rounds

def open(self):
rounds = self.live().public().specific()
def open(self, specific=True):
rounds = self.live().public()
if specific:
rounds = rounds.specific()
rounds = rounds.filter(
Q(start_date__lte=date.today())
& Q(Q(end_date__isnull=True) | Q(end_date__gte=date.today()))
Expand Down
14 changes: 8 additions & 6 deletions hypha/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ def home(request):
Displays the list of active rounds and labs to both logged in and logged out users.
"""
ctx = {}
rounds = ApplicationBase.objects.order_by_end_date().specific()
labs = LabBase.objects.public().live().specific()
all_funds = list(rounds) + list(labs)
funds = (
ApplicationBase.objects.order_by_end_date()
.filter(list_on_front_page=True)
.specific()
.with_open_round()
)
labs = LabBase.objects.public().live().filter(list_on_front_page=True).specific()

# Only pass rounds/labs that are open & visible for the front page
ctx["funds"] = [
fund for fund in all_funds if fund.open_round and fund.list_on_front_page
]
ctx["funds"] = funds + list(labs)
return render(request, "home/home.html", ctx)