polls.py
1 from datetime import datetime 2 from typing import Any 3 4 5 class TextQuestion: 6 """ 7 A text prompt 8 """ 9 10 def __init__( 11 self, 12 titre: str = "", 13 texte: str = "", 14 rang: int = 0, 15 student_name: str = "", 16 contenu_cache: str = "", 17 ) -> None: 18 self.rang = rang 19 self.titre = titre 20 self.texte = texte 21 self.student_name = student_name 22 self.contenu_cache = contenu_cache 23 24 def gen(self) -> dict[str, Any]: 25 return { 26 "L": self.titre, 27 "N": self.contenu_cache, 28 "P": 1, 29 "rang": self.rang, 30 "genreReponse": 1, 31 "titre": "", 32 "texte": {"_T": 21, "V": self.texte}, 33 "tailleReponse": 200, 34 "avecMaximum": False, 35 "nombreReponsesMax": 0, 36 "listePiecesJointes": {"_T": 24, "V": []}, 37 "listeChoix": {"_T": 24, "V": []}, 38 "reponse": { 39 "_T": 24, 40 "V": { 41 "N": "", 42 "avecReponse": True, 43 "estReponseAttendue": True, 44 "reponduLe": {"_T": 7, "V": datetime.now().strftime("%d/%m/%Y")}, 45 "strRepondant": self.student_name, 46 "estRepondant": True, 47 }, 48 }, 49 } 50 51 52 class MultipleChoice: 53 """ 54 A multiple choice prompt 55 ! Does not work yet 56 """ # TODO see why the multiple choices awnsers don't get sent to the server 57 58 def __init__( 59 self, 60 titre: str = "", 61 texte: str = "", 62 options: list[str] = [""], 63 rang: int = 0, 64 student_name: str = "", 65 contenu_cache: str = "", 66 ) -> None: 67 self.rang = rang 68 self.titre = titre 69 self.texte = texte 70 self.options = options 71 self.student_name = student_name 72 self.contenu_cache = contenu_cache 73 74 def gen(self) -> dict[str, Any]: 75 return { 76 "L": self.titre, 77 "N": self.contenu_cache, 78 "P": 0, 79 "rang": self.rang, 80 "genreReponse": 3, 81 "titre": "", 82 "texte": {"_T": 21, "V": self.texte}, 83 "tailleReponse": 200, 84 "avecMaximum": False, 85 "nombreReponsesMax": 0, 86 "listePiecesJointes": {"_T": 24, "V": []}, 87 "listeChoix": { 88 "_T": 24, 89 "V": [ 90 {"L": self.options[i], "rang": i + 1} 91 for i in range(len(self.options)) 92 ], 93 }, 94 "reponse": {"_T": 24, "V": {"N": "0"}}, 95 } 96 97 98 class SingleChoice: 99 """ 100 A single choice prompt 101 """ 102 103 def __init__( 104 self, 105 titre: str = "", 106 texte: str = "", 107 options: list[str] = [""], 108 option_choisie: int = 0, 109 rang: int = 0, 110 student_name: str = "", 111 contenu_cache: str = "", 112 ) -> None: 113 self.rang = rang 114 self.titre = titre 115 self.texte = texte 116 self.options = options 117 self.option_choisie = option_choisie 118 self.student_name = student_name 119 self.contenu_cache = contenu_cache 120 121 def gen(self) -> dict[str, Any]: 122 return { 123 "L": self.titre, 124 "N": self.contenu_cache, 125 "P": 0, 126 "rang": self.rang, 127 "genreReponse": 2, 128 "titre": "", 129 "texte": {"_T": 21, "V": self.texte}, 130 "tailleReponse": 200, 131 "avecMaximum": False, 132 "nombreReponsesMax": 0, 133 "listePiecesJointes": {"_T": 24, "V": []}, 134 "listeChoix": { 135 "_T": 24, 136 "V": [ 137 {"L": self.options[i], "N": "", "rang": i} 138 for i in range(len(self.options)) 139 ], 140 }, 141 "reponse": { 142 "_T": 24, 143 "V": { 144 "N": "", 145 "valeurReponse": {"_T": 8, "V": f"[{self.option_choisie}]"}, 146 "avecReponse": True, 147 "estReponseAttendue": True, 148 "reponduLe": {"_T": 7, "V": datetime.now().strftime("%d/%m/%Y")}, 149 "strRepondant": self.student_name, 150 "estRepondant": True, 151 }, 152 }, 153 } 154 155 156 class NoChoice: 157 """ 158 Used for informations 159 """ 160 161 def __init__( 162 self, 163 titre: str = "", 164 texte: str = "", 165 rang: int = 0, 166 student_name: str = "", 167 ) -> None: 168 self.rang = rang 169 self.titre = titre 170 self.texte = texte 171 self.student_name = student_name 172 173 def gen(self) -> dict[str, Any]: 174 return { 175 "L": self.titre, 176 "N": "", 177 "P": 0, 178 "rang": self.rang, 179 "genreReponse": 0, 180 "titre": "", 181 "texte": {"_T": 21, "V": self.texte}, 182 "tailleReponse": 200, 183 "avecMaximum": False, 184 "nombreReponsesMax": 0, 185 "listePiecesJointes": {"_T": 24, "V": []}, 186 "listeChoix": { 187 "_T": 24, 188 "V": [], 189 }, 190 "reponse": { 191 "_T": 24, 192 "V": { 193 "N": "", 194 "avecReponse": False, 195 "estReponseAttendue": True, 196 "reponduLe": {"_T": 7, "V": datetime.now().strftime("%d/%m/%Y")}, 197 "strRepondant": self.student_name, 198 "estRepondant": True, 199 }, 200 }, 201 } 202 203 204 class Poll: 205 """ 206 The poll class. 207 208 Represents a poll in the "Informations and Surveys" tab 209 """ 210 211 def __init__( 212 self, 213 list_polls: list[TextQuestion | SingleChoice | MultipleChoice] = [ 214 SingleChoice() 215 ], 216 titre: str = "", 217 student_name: str = "", 218 ) -> None: 219 self.list_polls = list_polls 220 self.titre = titre 221 self.student_name = student_name 222 223 def gen_poll(self) -> dict[Any, Any]: 224 date = datetime.now() 225 result_dict: dict[Any, Any] = { 226 "L": self.titre, 227 "N": "", 228 "reponseAnonyme": True, 229 "estInformation": False, 230 "estSondage": True, 231 "categorie": {"_T": 24, "V": {"L": "Divers", "N": ""}}, 232 "lue": False, 233 "dateDebut": {"_T": 7, "V": date.strftime("%d/%m/%Y")}, 234 "dateFin": {"_T": 7, "V": date.strftime("%d/%m/%Y")}, 235 "estProlonge": False, 236 "dateCreation": {"_T": 7, "V": date.strftime("%d/%m/%Y")}, 237 "auteur": "Eat-At-12", 238 "estAuteur": False, 239 "elmauteur": { 240 "_T": 24, 241 "V": {"L": "Eat-At-12", "N": "", "G": 3, "avecDiscussion": False}, 242 }, 243 "prenom": self.student_name, 244 "public": { 245 "_T": 24, 246 "V": {"L": self.student_name, "N": "", "G": 4, "P": 2026}, 247 }, 248 "genrePublic": 4, 249 "listeQuestions": {"_T": 24, "V": [poll.gen() for poll in self.list_polls]}, 250 } 251 return result_dict 252 253 254 class Information: 255 """ 256 The poll class. 257 258 Represents a poll in the "Informations and Surveys" tab 259 """ 260 261 def __init__( 262 self, 263 list_polls: list[NoChoice] = [NoChoice()], 264 titre: str = "", 265 student_name: str = "", 266 ) -> None: 267 self.list_polls = list_polls 268 self.titre = titre 269 self.student_name = student_name 270 271 def gen_poll(self) -> dict[Any, Any]: 272 date = datetime.now() 273 result_dict: dict[Any, Any] = { 274 "L": self.titre, 275 "N": "", 276 "reponseAnonyme": True, 277 "estInformation": True, 278 "estSondage": False, 279 "categorie": {"_T": 24, "V": {"L": "Divers", "N": ""}}, 280 "lue": False, 281 "dateDebut": {"_T": 7, "V": date.strftime("%d/%m/%Y")}, 282 "dateFin": {"_T": 7, "V": date.strftime("%d/%m/%Y")}, 283 "estProlonge": False, 284 "dateCreation": {"_T": 7, "V": date.strftime("%d/%m/%Y")}, 285 "auteur": "Eat-At-12", 286 "estAuteur": False, 287 "elmauteur": { 288 "_T": 24, 289 "V": {"L": "Eat-At-12", "N": "", "G": 3, "avecDiscussion": False}, 290 }, 291 "prenom": self.student_name, 292 "public": { 293 "_T": 24, 294 "V": {"L": self.student_name, "N": "", "G": 4, "P": 2026}, 295 }, 296 "genrePublic": 4, 297 "listeQuestions": {"_T": 24, "V": [poll.gen() for poll in self.list_polls]}, 298 } 299 return result_dict