From 3f10a7bedcb17024a515df2c198e856962864161 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <theskumar@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:38:24 +0800 Subject: [PATCH] Display "View Live" button only if round is live in admin (#3805) Currently the "view live" button on the round detail admin page, links to the round pages with the url containing fund slug followed by slug of the round page. The round can be accessed only via the fund url and the fund url serves the active round, so the current link leads to 404. This fixes the admin "view live" button so that it's displayed only if the round is live and also links it correctly to the fund slug Fixes #3794 --- hypha/apply/funds/models/applications.py | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/hypha/apply/funds/models/applications.py b/hypha/apply/funds/models/applications.py index d8db5792f..951efbabf 100644 --- a/hypha/apply/funds/models/applications.py +++ b/hypha/apply/funds/models/applications.py @@ -1,9 +1,11 @@ from datetime import date +from typing import Optional from django import forms from django.conf import settings from django.contrib.postgres.fields import ArrayField from django.core.exceptions import ValidationError +from django.core.handlers.wsgi import WSGIRequest from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django.db.models import ( @@ -232,6 +234,18 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore ) sealed = models.BooleanField(default=False) + def get_url(self, request: Optional[WSGIRequest] = None) -> Optional[str]: + """Generates the live url, primarily used in the wagtail admin for the "view live" button. + + Returns: + str: The live url of the page, or None if the page is not live. + """ + if self.is_open: + return self.fund.url + return None + + url = property(get_url) + content_panels = SubmittableStreamForm.content_panels + [ FieldPanel("lead"), MultiFieldPanel( @@ -302,8 +316,19 @@ class RoundBase(WorkflowStreamForm, SubmittableStreamForm): # type: ignore return self.sealed and self.is_open @property - def is_open(self): - return self.start_date <= date.today() <= self.end_date + def is_open(self) -> bool: + """ + Checks if the application is open based on the current date. + + The application is considered open if the current date is between the start and end dates (inclusive). + If the end date is not set, the application is considered open if the current date is on or after the start date. + + Returns: + bool: True if the application is open, False otherwise. + """ + if self.start_date and self.end_date: + return self.start_date <= date.today() <= self.end_date + return self.start_date <= date.today() def save(self, *args, **kwargs): is_new = not self.id -- GitLab