|
|
|
@ -116,44 +116,12 @@ class ElectionBase(BaseModel):
|
|
|
|
|
name: Name
|
|
|
|
|
description: Description = ""
|
|
|
|
|
ref: Ref = ""
|
|
|
|
|
num_voters: int = Field(0, ge=0, le=settings.max_voters)
|
|
|
|
|
date_start: datetime = Field(default_factory=datetime.now)
|
|
|
|
|
date_end: datetime = Field(default_factory=_in_a_long_time)
|
|
|
|
|
hide_results: bool = True
|
|
|
|
|
force_close: bool = False
|
|
|
|
|
restricted: bool = False
|
|
|
|
|
|
|
|
|
|
@validator("hide_results", "num_voters", "date_end")
|
|
|
|
|
def can_finish(cls, value: str, values: dict[str, t.Any], field: ModelField):
|
|
|
|
|
"""
|
|
|
|
|
Enforce that the election is finish-able
|
|
|
|
|
"""
|
|
|
|
|
if "hide_results" in values:
|
|
|
|
|
hide_results = values["hide_results"]
|
|
|
|
|
elif field.name == "hide_results":
|
|
|
|
|
hide_results = value
|
|
|
|
|
else:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
if "num_voters" in values:
|
|
|
|
|
num_voters = values["num_voters"]
|
|
|
|
|
elif field.name == "num_voters":
|
|
|
|
|
num_voters = value
|
|
|
|
|
else:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
if "date_end" in values:
|
|
|
|
|
date_end = values["date_end"]
|
|
|
|
|
elif field.name == "date_end":
|
|
|
|
|
date_end = value
|
|
|
|
|
else:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
if hide_results and num_voters == 0 and date_end is None:
|
|
|
|
|
raise ArgumentsSchemaError("This election can not end")
|
|
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
orm_mode = True
|
|
|
|
|
arbitrary_types_allowed = True
|
|
|
|
@ -182,10 +150,42 @@ class ElectionAndInvitesGet(ElectionGet):
|
|
|
|
|
|
|
|
|
|
class ElectionCreate(ElectionBase):
|
|
|
|
|
grades: list[GradeBase] = Field(..., min_items=2, max_items=settings.max_grades)
|
|
|
|
|
num_voters: int = Field(0, ge=0, le=settings.max_voters)
|
|
|
|
|
candidates: list[CandidateBase] = Field(
|
|
|
|
|
..., min_items=2, max_items=settings.max_candidates
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@validator("hide_results", "num_voters", "date_end")
|
|
|
|
|
def can_finish(cls, value: str, values: dict[str, t.Any], field: ModelField):
|
|
|
|
|
"""
|
|
|
|
|
Enforce that the election is finish-able
|
|
|
|
|
"""
|
|
|
|
|
if "hide_results" in values:
|
|
|
|
|
hide_results = values["hide_results"]
|
|
|
|
|
elif field.name == "hide_results":
|
|
|
|
|
hide_results = value
|
|
|
|
|
else:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
if "num_voters" in values:
|
|
|
|
|
num_voters = values["num_voters"]
|
|
|
|
|
elif field.name == "num_voters":
|
|
|
|
|
num_voters = value
|
|
|
|
|
else:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
if "date_end" in values:
|
|
|
|
|
date_end = values["date_end"]
|
|
|
|
|
elif field.name == "date_end":
|
|
|
|
|
date_end = value
|
|
|
|
|
else:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
if hide_results and num_voters == 0 and date_end is None:
|
|
|
|
|
raise ArgumentsSchemaError("This election can not end")
|
|
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
@validator("grades")
|
|
|
|
|
def all_grades_have_unique_values_and_names(cls, grades: list[GradeBase]):
|
|
|
|
|
values = [g.value for g in grades]
|
|
|
|
|