Load an EducatorAlert from a JSON object. Arguments: data (dict): The JSON object _session (session.Session): The session object used to load this data, to 'connect' to the alerts rather than just 'get' them Returns: EducatorAlert: The l
(cls, data: dict[str, Any], _session: Optional[session.Session] = None)
| 56 | |
| 57 | @classmethod |
| 58 | def from_json(cls, data: dict[str, Any], _session: Optional[session.Session] = None) -> Self: |
| 59 | """ |
| 60 | Load an EducatorAlert from a JSON object. |
| 61 | |
| 62 | Arguments: |
| 63 | data (dict): The JSON object |
| 64 | _session (session.Session): The session object used to load this data, to 'connect' to the alerts rather than just 'get' them |
| 65 | |
| 66 | Returns: |
| 67 | EducatorAlert: The loaded EducatorAlert object |
| 68 | """ |
| 69 | model = data.get("model") # With this class, should be equal to educators.educatoralert |
| 70 | assert isinstance(model, str) |
| 71 | alert_id = data.get("pk") # not sure what kind of pk/id this is. Doesn't seem to be a user or class id. |
| 72 | assert isinstance(alert_id, int) |
| 73 | |
| 74 | fields = data.get("fields") |
| 75 | assert isinstance(fields, dict) |
| 76 | |
| 77 | time_read_raw = fields.get("educator_datetime_read") |
| 78 | assert isinstance(time_read_raw, str) |
| 79 | time_read: datetime = datetime.fromisoformat(time_read_raw) |
| 80 | |
| 81 | admin_action = fields.get("admin_action") |
| 82 | assert isinstance(admin_action, dict) |
| 83 | |
| 84 | time_created_raw = admin_action.get("datetime_created") |
| 85 | assert isinstance(time_created_raw, str) |
| 86 | time_created: datetime = datetime.fromisoformat(time_created_raw) |
| 87 | |
| 88 | alert_type = admin_action.get("type") |
| 89 | assert isinstance(alert_type, int) |
| 90 | |
| 91 | target_data = admin_action.get("target_user") |
| 92 | assert isinstance(target_data, dict) |
| 93 | target = user.User(username=target_data.get("username"), |
| 94 | id=target_data.get("pk"), |
| 95 | icon_url=target_data.get("thumbnail_url"), |
| 96 | admin=target_data.get("admin", False), |
| 97 | _session=_session) |
| 98 | |
| 99 | actor_data = admin_action.get("actor") |
| 100 | assert isinstance(actor_data, dict) |
| 101 | actor = user.User(username=actor_data.get("username"), |
| 102 | id=actor_data.get("pk"), |
| 103 | icon_url=actor_data.get("thumbnail_url"), |
| 104 | admin=actor_data.get("admin", False), |
| 105 | _session=_session) |
| 106 | |
| 107 | object_id = admin_action.get("object_id") # this could be a comment id, a project id, etc. |
| 108 | assert isinstance(object_id, int) |
| 109 | target_object: project.Project | studio.Studio | comment.Comment | None = None |
| 110 | |
| 111 | extra_data: dict[str, Any] = json.loads(admin_action.get("extra_data", "{}")) |
| 112 | # todo: if possible, properly implement the incomplete parts of this parser (look for warning.warn()) |
| 113 | notification_type: str = "" |
| 114 | |
| 115 | if "project_title" in extra_data: |
no test coverage detected