Skip to content

Commit 1324d8b

Browse files
committed
docs: docstrings
1 parent 62d8e33 commit 1324d8b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

scratchattach/site/alert.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222

2323
@dataclass
2424
class EducatorAlert:
25+
"""
26+
Represents an alert for student activity, viewable at https://scratch.mit.edu/site-api/classrooms/alerts/
27+
28+
Attributes:
29+
model: The type of alert (presumably); should always equal "educators.educatoralert" in this class
30+
type: An integer that identifies the type of alert, differentiating e.g. against bans or autoban or censored comments etc
31+
raw: The raw JSON data from the API
32+
id: The ID of the alert (internally called 'pk' by scratch, not sure what this is for)
33+
time_read: The time the alert was read
34+
time_created: The time the alert was created
35+
target: The user that the alert is about (the student)
36+
actor: The user that created the alert (the admin)
37+
target_object: The object that the alert is about (e.g. a project, studio, or comment)
38+
notification_type: not sure what this is for, but inferred from the scratch HTML reference
39+
"""
2540
_: KW_ONLY
2641
model: str = "educators.educatoralert"
2742
type: int = None
@@ -37,6 +52,16 @@ class EducatorAlert:
3752

3853
@classmethod
3954
def from_json(cls, data: dict[str, Any], _session: session.Session = None) -> Self:
55+
"""
56+
Load an EducatorAlert from a JSON object.
57+
58+
Arguments:
59+
data (dict): The JSON object
60+
_session (session.Session): The session object used to load this data, to 'connect' to the alerts rather than just 'get' them
61+
62+
Returns:
63+
EducatorAlert: The loaded EducatorAlert object
64+
"""
4065
model: str = data.get("model") # With this class, should be equal to educators.educatoralert
4166
alert_id: int = data.get("pk") # not sure what kind of pk/id this is. Doesn't seem to be a user or class id.
4267

@@ -148,6 +173,9 @@ def __str__(self):
148173

149174
@property
150175
def alert_type(self) -> enums.AlertType:
176+
"""
177+
Get an associated AlertType object for this alert (based on the type index)
178+
"""
151179
alert_type = enums.AlertTypes.find(self.type)
152180
if not alert_type:
153181
alert_type = enums.AlertTypes.default.value
@@ -156,6 +184,9 @@ def alert_type(self) -> enums.AlertType:
156184

157185
@property
158186
def message(self):
187+
"""
188+
Format the alert message using the alert type's message template, as it would be on the website.
189+
"""
159190
raw_message = self.alert_type.message
160191
comment_content = ""
161192
if isinstance(self.target_object, comment.Comment):
@@ -169,6 +200,9 @@ def message(self):
169200

170201
@property
171202
def target_object_title(self):
203+
"""
204+
Get the title of the target object (if applicable)
205+
"""
172206
if isinstance(self.target_object, project.Project):
173207
return self.target_object.title
174208
if isinstance(self.target_object, studio.Studio):

scratchattach/site/session.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ def admin_messages(self, *, limit=40, offset=0) -> list[dict]:
251251

252252
def classroom_alerts(self, _classroom: Optional[classroom.Classroom | int] = None, mode: str = "Last created",
253253
page: Optional[int] = None):
254+
"""
255+
Load and parse admin alerts, optionally for a specific class, using https://scratch.mit.edu/site-api/classrooms/alerts/
256+
257+
Returns:
258+
list[alert.EducatorAlert]: A list of parsed EducatorAlert objects
259+
"""
260+
254261
if isinstance(_classroom, classroom.Classroom):
255262
_classroom = _classroom.id
256263

scratchattach/utils/enums.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,16 @@ class AlertType:
202202

203203

204204
class AlertTypes(_EnumWrapper):
205+
"""
206+
Enum for associating alert type indecies with their messages, for use with the str.format() method.
207+
"""
205208
# Reference: https://github.com/TimMcCool/scratchattach/issues/304#issuecomment-2800110811
206209
# NOTE: THE TEXT WITHIN THE BRACES HERE MATTERS! IF YOU WANT TO CHANGE IT, MAKE SURE TO EDIT `site.alert.EducatorAlert`!
207210
ban = AlertType(0, "{username} was banned.")
208211
unban = AlertType(1, "{username} was unbanned.")
209212
excluded_from_homepage = AlertType(2, "{username} was excluded from homepage")
210213
excluded_from_homepage2 = AlertType(3, "{username} was excluded from homepage") # for some reason there are duplicates
211-
notified = AlertType(4, "{username} was notified by a Scratch Administrator. Notification type: {notification_type}")
214+
notified = AlertType(4, "{username} was notified by a Scratch Administrator. Notification type: {notification_type}") # not sure what notification type is
212215
autoban = AlertType(5, "{username} was banned automatically")
213216
autoremoved = AlertType(6, "{project} by {username} was removed automatically")
214217
project_censored2 = AlertType(7, "{project} by {username} was censored.") # <empty #7>

0 commit comments

Comments
 (0)