Skip to content

Draftfile

py_moodle.draftfile

Draft file management module for Moodle CLI.

Provides functions to create draft areas, upload files to the draft area, list draft files, and manage draft IDs needed for Moodle module creation.

All code and comments must be in English.

Classes:

Name Description
MoodleDraftFileError

Exception raised for errors in draft file operations.

Functions:

Name Description
get_new_draft_itemid

Obtain a new draft item ID for the current user.

detect_upload_repo

Detects the upload repository ID by scraping the filemanager configuration

upload_file_to_draft_area

Uploads a file to a draft area using the session-based AJAX endpoint,

list_draft_files

List files in a Moodle draft area.

Classes

MoodleDraftFileError

Bases: Exception

Exception raised for errors in draft file operations.

Functions

get_new_draft_itemid

get_new_draft_itemid(
    session: Session, base_url: str, sesskey: str
) -> int

Obtain a new draft item ID for the current user. This is done by getting the user's private files info, which contains a valid itemid.

detect_upload_repo

detect_upload_repo(
    session: Session, base_url: str, course_id: int
) -> int

Detects the upload repository ID by scraping the filemanager configuration from the course edit page. This is the most robust method.

upload_file_to_draft_area

upload_file_to_draft_area(
    session: Session,
    base_url: str,
    sesskey: str,
    course_id: int,
    course_context_id: int,
    file_path: str,
    itemid: Optional[int] = None,
    savepath: str = "/",
    timeout: tuple = (300, 3600),
    progress_callback: Optional[
        Callable[[int], None]
    ] = None,
) -> Tuple[int, str]

Uploads a file to a draft area using the session-based AJAX endpoint, returning the itemid of the draft area and the final filename. If the file exists, Moodle renames it; this function returns the new name.

list_draft_files

list_draft_files(
    session: Session,
    base_url: str,
    sesskey: str,
    draft_itemid: int,
) -> List[Dict[str, Any]]

List files in a Moodle draft area.