Skip to content

Commit a2b87ef

Browse files
committed
register by class token
1 parent c569235 commit a2b87ef

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

scratchattach/site/classroom.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
class Classroom(BaseSiteComponent):
1919
def __init__(self, **entries):
2020
# Info on how the .update method has to fetch the data:
21+
# NOTE: THIS DOESN'T WORK WITH CLOSED CLASSES!
2122
self.update_function = requests.get
2223
if "id" in entries:
2324
self.update_API = f"https://api.scratch.mit.edu/classrooms/{entries['id']}"
@@ -46,6 +47,9 @@ def __init__(self, **entries):
4647
self._json_headers["accept"] = "application/json"
4748
self._json_headers["Content-Type"] = "application/json"
4849

50+
def __repr__(self):
51+
return f"classroom called '{self.title}'"
52+
4953
def _update_from_dict(self, classrooms):
5054
try:
5155
self.id = int(classrooms["id"])
@@ -215,6 +219,25 @@ def close(self):
215219
warnings.warn(f"{self._session} may not be authenticated to edit {self}")
216220
raise e
217221

222+
def register_user(self, username: str, password: str, birth_month: int, birth_year: int,
223+
gender: str, country: str, is_robot: bool = False):
224+
return register_user(self.id, self.classtoken, username, password, birth_month, birth_year, gender, country, is_robot)
225+
226+
def generate_signup_link(self):
227+
if self.classtoken is not None:
228+
return f"https://scratch.mit.edu/signup/{self.classtoken}"
229+
230+
self._check_session()
231+
232+
response = requests.get(f"https://scratch.mit.edu/site-api/classrooms/generate_registration_link/{self.id}/", headers=self._headers, cookies=self._cookies)
233+
# Should really check for '404' page
234+
data = response.json()
235+
if "reg_link" in data:
236+
return data["reg_link"]
237+
else:
238+
raise exceptions.Unauthorized(f"{self._session} is not authorised to generate a signup link of {self}")
239+
240+
218241
def public_activity(self, *, limit=20):
219242
"""
220243
Returns:
@@ -288,3 +311,25 @@ def get_classroom_from_token(class_token) -> Classroom:
288311
"""
289312
warnings.warn("For methods that require authentication, use session.connect_classroom instead of get_classroom")
290313
return commons._get_object("classtoken", class_token, Classroom, exceptions.ClassroomNotFound)
314+
315+
316+
def register_user(class_id: int, class_token: str, username: str, password: str, birth_month: int, birth_year: int, gender: str, country: str, is_robot: bool = False):
317+
data = {"classroom_id": class_id,
318+
"classroom_token": class_token,
319+
320+
"username": username,
321+
"password": password,
322+
"birth_month": birth_month,
323+
"birth_year": birth_year,
324+
"gender": gender,
325+
"country": country,
326+
"is_robot": is_robot}
327+
328+
response = requests.post("https://scratch.mit.edu/classes/register_new_student/",
329+
data=data, headers=headers, cookies={"scratchcsrftoken": 'a'})
330+
ret = response.json()[0]
331+
332+
if "username" in ret:
333+
return
334+
else:
335+
raise exceptions.Unauthorized(f"Can't create account: {response.text}")

0 commit comments

Comments
 (0)