Skip to content

Commit 37e44cd

Browse files
committed
feat: add more parsing based on scratch html & add message property
1 parent d3811f0 commit 37e44cd

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

scratchattach/site/alert.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import TYPE_CHECKING, Self, Any
1111

1212
from . import user, project, studio, comment, session
13+
from ..utils import enums
1314

1415
if TYPE_CHECKING:
1516
...
@@ -30,7 +31,8 @@ class EducatorAlert:
3031
time_created: datetime = None
3132
target: user.User = None
3233
actor: user.User = None
33-
target_object: project.Project | studio.Studio | comment.Comment = None
34+
target_object: project.Project | studio.Studio | comment.Comment | studio.Studio = None
35+
notification_type: str = None
3436
_session: session.Session = None
3537

3638
@classmethod
@@ -68,7 +70,8 @@ def from_json(cls, data: dict[str, Any], _session: session.Session = None) -> Se
6870
target_object: project.Project | studio.Studio | comment.Comment | None = None
6971

7072
extra_data: dict[str, Any] = json.loads(admin_action.get("extra_data", "{}"))
71-
# todo: properly implement the 2 incomplete parts of this parser (look for warning.warn())
73+
# todo: if possible, properly implement the incomplete parts of this parser (look for warning.warn())
74+
notification_type: str = None
7275

7376
if "project_title" in extra_data:
7477
# project
@@ -126,23 +129,25 @@ def from_json(cls, data: dict[str, Any], _session: session.Session = None) -> Se
126129
source_id=comment_obj_id,
127130
_session=_session
128131
)
132+
133+
elif "gallery_title" in extra_data:
134+
# studio
135+
# possible implemented incorrectly
136+
target_object = studio.Studio(
137+
id=object_id,
138+
title=extra_data["gallery_title"],
139+
_session=_session
140+
)
141+
elif "notification_type" in extra_data:
142+
# possible implemented incorrectly
143+
notification_type = extra_data["notification_type"]
129144
else:
130-
# probably a studio
131-
# possibly forums? Profile?
132145
warnings.warn(
133146
f"The parser was not able to recognise the \"extra_data\" in the alert JSON response.\n"
134147
f"Full response: \n{pprint.pformat(data)}.\n\n"
135148
f"Please draft an issue on github: https://github.com/TimMcCool/scratchattach/issues, providing this "
136149
f"whole error message. This will allow us to implement an incomplete part of this parser")
137150

138-
# theoretical parser. might now work
139-
# also, what if it's a profile?
140-
target_object = studio.Studio(
141-
id=object_id,
142-
title=extra_data.get("gallery_title"), # i have no idea if this is the correct key
143-
_session=_session
144-
)
145-
146151
return cls(
147152
id=alert_id,
148153
model=model,
@@ -153,5 +158,38 @@ def from_json(cls, data: dict[str, Any], _session: session.Session = None) -> Se
153158
target=target,
154159
actor=actor,
155160
target_object=target_object,
161+
notification_type=notification_type,
156162
_session=_session
157163
)
164+
165+
def __str__(self):
166+
return f"EducatorAlert: {self.message}"
167+
168+
@property
169+
def alert_type(self) -> enums.AlertType:
170+
alert_type = enums.AlertTypes.find(self.type)
171+
if not alert_type:
172+
alert_type = enums.AlertTypes.default.value
173+
174+
return alert_type
175+
176+
@property
177+
def message(self):
178+
raw_message = self.alert_type.message
179+
comment_content = ""
180+
if isinstance(self.target_object, comment.Comment):
181+
comment_content = self.target_object.content
182+
183+
return raw_message.format(username=self.target.username,
184+
project=self.target_object_title,
185+
studio=self.target_object_title,
186+
notification_type=self.notification_type,
187+
comment=comment_content)
188+
189+
@property
190+
def target_object_title(self):
191+
if isinstance(self.target_object, project.Project):
192+
return self.target_object.title
193+
if isinstance(self.target_object, studio.Studio):
194+
return self.target_object.title
195+
return None # explicit

0 commit comments

Comments
 (0)