Skip to content

Commit

Permalink
Add validation for the feedback and model solution reveal rules
Browse files Browse the repository at this point in the history
The validation rejected courses that used the reveal rule settings
since the validation code did not include those settings at all.

The SimpleDuration and AnyDate codes were moved so that the new
code may also refer to AnyDate.
  • Loading branch information
markkuriekkinen committed Jan 30, 2023
1 parent 8869e9e commit 5b7c89f
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions access/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,48 @@ def _get_datetime(value: Any) -> Optional[datetime]:
return value


class SimpleDuration(PydanticModel):
__root__: str

@root_validator
def simple_duration(cls, values: dict):
delta = values.get('__root__')
if not isinstance(delta, str):
raise ValueError("A duration must be a string")
if not delta:
raise ValueError("An empty string cannot be turned into a duration")

try:
int(delta[:-1])
except:
raise ValueError("Format: <integer>(y|m|d|h|w) e.g. 3d")

if delta[-1] in ("y", "m", "w", "d", "h"):
return values
else:
raise ValueError("Format: <integer>(y|m|d|h|w) e.g. 3d")

AnyDuration = Union[timedelta, SimpleDuration]
AnyDate = Union[datetime, date, str]
# FIXME: str in AnyDate passes invalid dates and any strings.
# str should be removed. However, removing it causes crashes with the Undefined type.


Float0to1 = confloat(ge=0, le=1)


class ConfigureOptions(PydanticModel):
files: Dict[str,str] = {}
# ellipsis (...) makes the field required in the case that a default url isn't specified
url: str = settings.DEFAULT_GRADER_URL or ... # type: ignore


class RevealRuleOptions(PydanticModel):
trigger: Literal["immediate", "manual", "time", "deadline", "deadline_all", "completion"]
time: NotRequired[AnyDate]
delay_minutes: NotRequired[NonNegativeInt]


class ExerciseConfig(PydanticModel):
data: Dict[str, dict]
file: str
Expand Down Expand Up @@ -180,6 +216,8 @@ class Exercise(Item):
max_group_size: NotRequired[NonNegativeInt]
max_points: NotRequired[NonNegativeInt]
points_to_pass: NotRequired[NonNegativeInt]
reveal_submission_feedback: NotRequired[RevealRuleOptions]
reveal_model_solutions: NotRequired[RevealRuleOptions]
_config_obj: Optional[ExerciseConfig] = PrivateAttr(default=None)

def config_file_info(self, course_dir: str, grader_config_dir: str) -> Optional[Tuple[str, str]]:
Expand Down Expand Up @@ -309,36 +347,6 @@ def validate_static_content(cls, paths: Localized[Path]):
Chapter.update_forward_refs()


class SimpleDuration(PydanticModel):
__root__: str

@root_validator
def simple_duration(cls, values: dict):
delta = values.get('__root__')
if not isinstance(delta, str):
raise ValueError("A duration must be a string")
if not delta:
raise ValueError("An empty string cannot be turned into a duration")

try:
int(delta[:-1])
except:
raise ValueError("Format: <integer>(y|m|d|h|w) e.g. 3d")

if delta[-1] in ("y", "m", "w", "d", "h"):
return values
else:
raise ValueError("Format: <integer>(y|m|d|h|w) e.g. 3d")

AnyDuration = Union[timedelta, SimpleDuration]
AnyDate = Union[datetime, date, str]
# FIXME: str in AnyDate passes invalid dates and any strings.
# str should be removed. However, removing it causes crashes with the Undefined type.


Float0to1 = confloat(ge=0, le=1)


class Module(Parent):
name: Localized[str]
key: str
Expand Down

0 comments on commit 5b7c89f

Please sign in to comment.