77 String ,
88 Text ,
99)
10- from sqlalchemy .ext .hybrid import hybrid_property
1110from sqlalchemy .orm import Mapped , mapped_column
1211
1312from constants import (
2120 NomineeApplicationUpdateParams ,
2221)
2322from officers .types import OfficerPositionEnum
23+ from utils .types import StringList
2424
2525MAX_ELECTION_NAME = 64
2626MAX_ELECTION_SLUG = 64
@@ -36,20 +36,13 @@ class Election(Base):
3636 datetime_start_voting : Mapped [datetime ] = mapped_column (DateTime , nullable = False )
3737 datetime_end_voting : Mapped [datetime ] = mapped_column (DateTime , nullable = False )
3838
39- # a csv list of positions which must be elements of OfficerPosition
40- _available_positions : Mapped [str ] = mapped_column ("available_positions" , Text , nullable = False )
39+ # a comma-separated string of positions which must be elements of OfficerPosition
40+ # By giving it the type `StringList`, the database entry will automatically be marshalled to the correct form
41+ # DB -> Python: str -> list[str]
42+ # Python -> DB: list[str] -> str
43+ available_positions : Mapped [list [OfficerPositionEnum ]] = mapped_column (StringList (), nullable = False ,)
4144 survey_link : Mapped [str | None ] = mapped_column (String (300 ))
4245
43- @hybrid_property
44- def available_positions (self ) -> str : # pyright: ignore
45- return self ._available_positions
46-
47- @available_positions .setter
48- def available_positions (self , value : str | list [str ]) -> None :
49- if isinstance (value , list ):
50- value = "," .join (value )
51- self ._available_positions = value
52-
5346 def private_details (self , at_time : datetime ) -> dict :
5447 # is serializable
5548 return {
@@ -62,7 +55,7 @@ def private_details(self, at_time: datetime) -> dict:
6255 "datetime_end_voting" : self .datetime_end_voting .isoformat (),
6356
6457 "status" : self .status (at_time ),
65- "available_positions" : self ._available_positions ,
58+ "available_positions" : self .available_positions ,
6659 "survey_link" : self .survey_link ,
6760 }
6861
@@ -78,7 +71,7 @@ def public_details(self, at_time: datetime) -> dict:
7871 "datetime_end_voting" : self .datetime_end_voting .isoformat (),
7972
8073 "status" : self .status (at_time ),
81- "available_positions" : self ._available_positions ,
74+ "available_positions" : self .available_positions ,
8275 }
8376
8477 def public_metadata (self , at_time : datetime ) -> dict :
@@ -105,7 +98,7 @@ def to_update_dict(self) -> dict:
10598 "datetime_start_voting" : self .datetime_start_voting .date (),
10699 "datetime_end_voting" : self .datetime_end_voting .date (),
107100
108- "available_positions" : self ._available_positions ,
101+ "available_positions" : self .available_positions ,
109102 "survey_link" : self .survey_link ,
110103 }
111104
0 commit comments