diff --git a/hypha/api/v2/views.py b/hypha/api/v2/views.py index fd3f6f5f92..79e1269d31 100644 --- a/hypha/api/v2/views.py +++ b/hypha/api/v2/views.py @@ -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) diff --git a/hypha/apply/funds/models/applications.py b/hypha/apply/funds/models/applications.py index 6a67003173..ee215a8039 100644 --- a/hypha/apply/funds/models/applications.py +++ b/hypha/apply/funds/models/applications.py @@ -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 @@ -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())) diff --git a/hypha/home/views.py b/hypha/home/views.py index f0f7587ba4..ee1992e783 100644 --- a/hypha/home/views.py +++ b/hypha/home/views.py @@ -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)