Skip to content

Commit df80af3

Browse files
committed
feat: set featured project data
closes #597 also makes the virtualenv for tests/ refer to scratchattach in an editable manner. I am not satisfied with the setup though. there may be a more idiomatic way to do it with workspaces but I do not know how
1 parent faaf9e0 commit df80af3

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ select = [
9494
[tool.ruff.lint.mccabe]
9595
max-complexity = 10
9696

97+
[tool.uv]
98+
config-settings = { editable_mode = "compat" }
99+
97100
[tool.setuptools.packages.find]
98101
where = ["."]
99102
include = ["scratchattach*"]

scratchattach/site/session.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import zlib
1616

1717
from dataclasses import dataclass, field
18-
from typing import Optional, TypeVar, TYPE_CHECKING, overload, Any, Union, cast
18+
from typing import Literal, Optional, TypeVar, TYPE_CHECKING, overload, Any, Union, cast
1919
from contextlib import contextmanager
2020
from threading import local
2121

@@ -318,6 +318,36 @@ def logout(self):
318318
"""
319319
requests.post("https://scratch.mit.edu/accounts/logout/", headers=self._headers, cookies=self._cookies)
320320

321+
def set_featured_data(self, project_id: Optional[int] | Literal[""], project_label: Optional[int] | Literal[""] = None):
322+
"""
323+
Sends a request to change your featured project area.
324+
325+
Positional arguments:
326+
project_id: None -> don't change; empty string -> set to latest project (this is what most accounts have); int -> set the featured project to the one with the corresponding ID. If you do not own that project, an error is raised.
327+
project_lavel: None -> don't change; empty string -> "Featured project"; 0 -> "Featured Tutorial"; 1 -> "Work in progress"; 2 -> "Remix this!"; 3 -> "My favorite things"; 4 -> "Why I scratch"
328+
329+
Returns:
330+
list<scratch.activity.Activity>: List that contains all messages as Activity objects.
331+
332+
"""
333+
# TODO: consider using an enum here for project label and match that with user.get_featured_data
334+
payload: dict[str, int | str] = {}
335+
if project_label is not None:
336+
payload["featured_project_label"] = str(project_label)
337+
if project_id is not None:
338+
payload["featured_project"] = project_id
339+
340+
data = requests.put(
341+
f"https://scratch.mit.edu/site-api/users/all/{self.username}/",
342+
json=payload,
343+
headers=self._headers,
344+
cookies=self._cookies,
345+
).json()
346+
if errors := data.get("errors"):
347+
raise Exception(f"Backend responded with error: {errors[0] if len(errors) == 1 else errors}")
348+
349+
return data
350+
321351
@property
322352
def ocular_headers(self) -> dict[str, str]:
323353
self._assert_ocular_auth()

tests/pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ dependencies = [
1717
requires = ["uv_build>=0.10.10,<0.11.0"]
1818
build-backend = "uv_build"
1919

20+
[tool.uv]
21+
config-settings = { editable_mode = "compat" }
22+
2023
[tool.uv.sources]
21-
scratchattach = { path = "../" }
24+
scratchattach = { path = "../", editable = true }

tests/uv.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)