Ensure¶
Idempotent, ensure-style provisioning helpers for sections and content
modules. Each helper looks up an existing object by its human-readable natural
key and only creates a new one when nothing matches, mirroring
ensure_course.
Idempotent ensure-style provisioning helpers for Moodle content.
This module extends the ensure_course pattern (see
:mod:py_moodle.course) to sections and content modules. Every helper is
idempotent: it first looks up an existing object by its human-readable
natural key and only creates a new one when nothing matches, mirroring how
ensure_course keys on shortname.
The idempotency keys are:
- :func:
ensure_module(and its wrappers):(name, modname)within the course. - :func:
ensure_section: the sectionnamewithin the course.
These helpers only ever create or reuse; they never delete or reconcile
away extra objects. The heavy lookup
(get_course_with_sections_and_modules) and the mutating primitives
(add_label/add_resource/add_folder/create_section) are imported
lazily inside the functions to avoid import cycles and to keep the module easy
to test with monkeypatching.
Classes:
| Name | Description |
|---|---|
EnsureModuleResult |
Outcome of an :func: |
EnsureSectionResult |
Outcome of an :func: |
Functions:
| Name | Description |
|---|---|
ensure_module |
Ensure a module with the given name and type exists in the course. |
ensure_label |
Ensure a |
ensure_resource |
Ensure a |
ensure_folder |
Ensure a |
ensure_section |
Ensure a section with the given name exists in the course. |
Attributes¶
Classes¶
EnsureModuleResult
dataclass
¶
EnsureModuleResult(
status: EnsureModuleStatus,
cmid: int,
module: Optional[Dict[str, Any]] = None,
)
Outcome of an :func:ensure_module call.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
EnsureModuleStatus
|
|
cmid |
int
|
The course module ID of the resulting module. |
module |
Optional[Dict[str, Any]]
|
The matched module dictionary when |
Attributes¶
EnsureSectionResult
dataclass
¶
EnsureSectionResult(
status: EnsureSectionStatus, section: Dict[str, Any]
)
Outcome of an :func:ensure_section call.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
EnsureSectionStatus
|
|
section |
Dict[str, Any]
|
The resulting section dictionary. When |
Attributes¶
Functions:¶
ensure_module ¶
ensure_module(
session: Session,
base_url: str,
sesskey: str,
course_id: int,
section_id: int,
*,
name: str,
modname: str,
create_fn: Callable[[], int],
token: Optional[str] = None
) -> EnsureModuleResult
Ensure a module with the given name and type exists in the course.
Scans every section of the course for a module whose name and
modname both match. If found, the module is reused and create_fn
is never called. Otherwise create_fn is invoked exactly once (it must
create the module and return its cmid) and the result is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Authenticated requests session. |
required |
base_url
|
str
|
Base URL of the Moodle instance. |
required |
sesskey
|
str
|
Session key for AJAX/form calls. |
required |
course_id
|
int
|
ID of the course to search within. |
required |
section_id
|
int
|
ID of the section the module belongs to (passed by the
wrappers to their |
required |
name
|
str
|
Human-readable module name used as part of the idempotency key. |
required |
modname
|
str
|
Moodle module type (e.g. |
required |
create_fn
|
Callable[[], int]
|
Zero-argument callable that creates the module and returns
its |
required |
token
|
Optional[str]
|
Optional webservice token forwarded to the course lookup. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EnsureModuleResult |
EnsureModuleResult
|
Typed result with |
EnsureModuleResult
|
|
ensure_label ¶
ensure_label(
session: Session,
base_url: str,
sesskey: str,
course_id: int,
section_id: int,
*,
name: str,
html: str,
visible: int = 1,
token: Optional[str] = None
) -> EnsureModuleResult
Ensure a label module with the given name exists.
Thin wrapper over :func:ensure_module binding modname="label" and
delegating creation to :func:py_moodle.label.add_label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Authenticated requests session. |
required |
base_url
|
str
|
Base URL of the Moodle instance. |
required |
sesskey
|
str
|
Session key for AJAX/form calls. |
required |
course_id
|
int
|
ID of the course. |
required |
section_id
|
int
|
ID of the section to create the label in when missing. |
required |
name
|
str
|
Label name (idempotency key together with the module type). |
required |
html
|
str
|
HTML content of the label. |
required |
visible
|
int
|
Whether the label is visible ( |
1
|
token
|
Optional[str]
|
Optional webservice token forwarded to the course lookup. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EnsureModuleResult |
EnsureModuleResult
|
Result of the create-or-reuse operation. |
ensure_resource ¶
ensure_resource(
session: Session,
base_url: str,
sesskey: str,
course_id: int,
section_id: int,
*,
name: str,
file_path: str,
intro: str = "",
visible: int = 1,
token: Optional[str] = None
) -> EnsureModuleResult
Ensure a resource (single-file) module with the given name exists.
Thin wrapper over :func:ensure_module binding modname="resource" and
delegating creation to :func:py_moodle.resource.add_resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Authenticated requests session. |
required |
base_url
|
str
|
Base URL of the Moodle instance. |
required |
sesskey
|
str
|
Session key for AJAX/form calls. |
required |
course_id
|
int
|
ID of the course. |
required |
section_id
|
int
|
ID of the section to create the resource in when missing. |
required |
name
|
str
|
Resource name (idempotency key together with the module type). |
required |
file_path
|
str
|
Local path to the file to upload when creating. |
required |
intro
|
str
|
Optional HTML introduction for the resource. |
''
|
visible
|
int
|
Whether the resource is visible ( |
1
|
token
|
Optional[str]
|
Optional webservice token forwarded to the course lookup. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EnsureModuleResult |
EnsureModuleResult
|
Result of the create-or-reuse operation. |
ensure_folder ¶
ensure_folder(
session: Session,
base_url: str,
sesskey: str,
course_id: int,
section_id: int,
*,
name: str,
files_itemid: int,
intro_html: str = "",
visible: int = 1,
token: Optional[str] = None
) -> EnsureModuleResult
Ensure a folder module with the given name exists.
Thin wrapper over :func:ensure_module binding modname="folder" and
delegating creation to :func:py_moodle.folder.add_folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Authenticated requests session. |
required |
base_url
|
str
|
Base URL of the Moodle instance. |
required |
sesskey
|
str
|
Session key for AJAX/form calls. |
required |
course_id
|
int
|
ID of the course. |
required |
section_id
|
int
|
ID of the section to create the folder in when missing. |
required |
name
|
str
|
Folder name (idempotency key together with the module type). |
required |
files_itemid
|
int
|
Draft-area itemid holding the folder's files. |
required |
intro_html
|
str
|
Optional HTML introduction for the folder. |
''
|
visible
|
int
|
Whether the folder is visible ( |
1
|
token
|
Optional[str]
|
Optional webservice token forwarded to the course lookup. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EnsureModuleResult |
EnsureModuleResult
|
Result of the create-or-reuse operation. |
ensure_section ¶
ensure_section(
session: Session,
base_url: str,
sesskey: str,
course_id: int,
*,
name: str,
token: Optional[str] = None
) -> EnsureSectionResult
Ensure a section with the given name exists in the course.
Looks up an existing section by name via
get_course_with_sections_and_modules. If found, it is reused. Otherwise
a new section is created with create_section and then renamed to
name (see :func:_rename_section for the renaming mechanism and its
limitations).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Authenticated requests session. |
required |
base_url
|
str
|
Base URL of the Moodle instance. |
required |
sesskey
|
str
|
Session key for AJAX/form calls. |
required |
course_id
|
int
|
ID of the course. |
required |
name
|
str
|
Section name used as the idempotency key. |
required |
token
|
Optional[str]
|
Optional webservice token forwarded to the course lookup. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
EnsureSectionResult |
EnsureSectionResult
|
Typed result with |
EnsureSectionResult
|
|