Skip to content

Commit

Permalink
[frontend/accessible_time] Refactoring parameter check for Accessible…
Browse files Browse the repository at this point in the history
…Time
  • Loading branch information
AlexandreDoneux committed Jan 11, 2024
1 parent d2dfebf commit 4f9d220
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions inginious/frontend/accessible_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ def parse_date(date, default=None):
class AccessibleTime(object):
""" represents the period of time when a course/task is accessible """

def __init__(self, period=None):
def __init__(self, period):
"""
Used to represent the period of time when a course/task is accessible.
:param val : bool, optionnal, if False, it is never accessible, if True, it is always accessible or limited
by period dict
:param period : dict, contains start, end and optionally soft_end as datetime objects or strings
"""

if not isinstance(period, dict):
raise Exception("Wrong period given to AccessibleTime")

# transforming strings into datetimes in case AccessibleTime is used in html files, where datetime objects are not supported
for key, date in period.items():
if isinstance(date, str) and date != "":
period[key] = parse_date(date)
elif isinstance(date, str) and date == "":
period[key] = None # don't want to transform in None ... Or maybe yes ? It could raise an error if the period given has a problem
elif not isinstance(date, (datetime, str)):
raise Exception("Wrong period given to AccessibleTime")
elif date == "":
raise Exception("Empty date given to AccessibleTime")

self._start = self.adapt_database_date(period["start"])
self._end = self.adapt_database_date(period["end"])
Expand Down Expand Up @@ -108,21 +113,21 @@ def is_never_accessible(self):
def get_std_start_date(self):
""" If the date is custom, return the start datetime with the format %Y-%m-%d %H:%M:%S. Else, returns "". """
if self._start != datetime.min and self._start != datetime.max:
return self._start.strftime("%Y-%m-%d %H:%M:%S") if self._start is not None else ""
return self._start.strftime("%Y-%m-%d %H:%M:%S")
else:
return ""

def get_std_end_date(self):
""" If the date is custom, return the end datetime with the format %Y-%m-%d %H:%M:%S. Else, returns "". """
if self._end != datetime.max:
return self._end.strftime("%Y-%m-%d %H:%M:%S") if self._end is not None else ""
return self._end.strftime("%Y-%m-%d %H:%M:%S")
else:
return ""

def get_std_soft_end_date(self):
""" If the date is custom, return the soft datetime with the format %Y-%m-%d %H:%M:%S. Else, returns "". """
if self._soft_end != datetime.max:
return self._soft_end.strftime("%Y-%m-%d %H:%M:%S") if self._soft_end is not None else ""
return self._soft_end.strftime("%Y-%m-%d %H:%M:%S")
else:
return ""

Expand Down

0 comments on commit 4f9d220

Please sign in to comment.