-
Notifications
You must be signed in to change notification settings - Fork 2
Feature: Retrive site information trough webservice #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04878be
feat: add function to retrieve site info
google-labs-jules[bot] 538f5a1
Cherrypicke makefile
erseco 9c457ea
Fix lint issues
erseco fd99bb2
Update src/py_moodle/session.py
erseco 621d39c
Merge branch 'main' into feat/site-info
erseco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Site commands for ``py-moodle``.""" | ||
|
|
||
| import json | ||
|
|
||
| import typer | ||
| from rich.console import Console | ||
| from rich.table import Table | ||
|
|
||
| from py_moodle.session import MoodleSession | ||
| from py_moodle.site import get_site_info | ||
|
|
||
| app = typer.Typer(help="Get site information.") | ||
| console = Console() | ||
|
|
||
|
|
||
| @app.command("info") | ||
| def info( | ||
| ctx: typer.Context, | ||
| as_json: bool = typer.Option(False, "--json", help="Output as JSON."), | ||
| ): | ||
| """Get site info.""" | ||
| ms = MoodleSession.get(ctx.obj["env"]) | ||
| site_info = get_site_info(ms) | ||
|
|
||
| if as_json: | ||
| # We need to handle the dataclasses in the site_info object | ||
| # to make them serializable. | ||
| class DataclassEncoder(json.JSONEncoder): | ||
| def default(self, o): | ||
| from dataclasses import asdict, is_dataclass | ||
|
|
||
| if is_dataclass(o): | ||
| return asdict(o) | ||
| return super().default(o) | ||
|
|
||
| console.print( | ||
| json.dumps(site_info, indent=4, cls=DataclassEncoder, ensure_ascii=False) | ||
| ) | ||
| return | ||
|
|
||
| table = Table(title="Site Info") | ||
| table.add_column("Property", style="cyan") | ||
| table.add_column("Value", style="magenta") | ||
|
|
||
| for key, value in site_info.__dict__.items(): | ||
| if isinstance(value, list): | ||
| table.add_row(key, str(len(value)) + " items") | ||
| else: | ||
| table.add_row(key, str(value)) | ||
|
|
||
| console.print(table) | ||
|
|
||
|
|
||
| __all__ = ["app"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Site information.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| from py_moodle.session import MoodleSession | ||
|
|
||
|
|
||
| @dataclass | ||
| class SiteFunction: | ||
| """A dataclass to represent a function available in the Moodle site.""" | ||
|
|
||
| name: str | ||
| version: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class AdvancedFeature: | ||
| """A dataclass to represent an advanced feature available in the Moodle site.""" | ||
|
|
||
| name: str | ||
| value: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class SiteInfo: | ||
| """A dataclass to represent the site information.""" | ||
|
|
||
| sitename: str | ||
| username: str | ||
| firstname: str | ||
| lastname: str | ||
| fullname: str | ||
| lang: str | ||
| userid: int | ||
| siteurl: str | ||
| userpictureurl: str | ||
| functions: List[SiteFunction] | ||
| downloadfiles: int | ||
| uploadfiles: int | ||
| release: str | ||
| version: str | ||
| mobilecssurl: str | ||
| advancedfeatures: List[AdvancedFeature] | ||
| usercanmanageownfiles: bool | ||
| userquota: int | ||
| usermaxuploadfilesize: int | ||
| userhomepage: int | ||
| userprivateaccesskey: str | ||
| siteid: int | ||
| sitecalendartype: str | ||
| usercalendartype: str | ||
| userissiteadmin: bool | ||
| theme: str | ||
| limitconcurrentlogins: int | ||
| policyagreed: int | ||
|
|
||
|
|
||
| def get_site_info(session: MoodleSession) -> SiteInfo: | ||
| """Get site info. | ||
|
|
||
| Args: | ||
| session (MoodleSession): The Moodle session. | ||
|
|
||
| Returns: | ||
| SiteInfo: The site information. | ||
| """ | ||
| response = session.call("core_webservice_get_site_info") | ||
| response["functions"] = [ | ||
| SiteFunction(**function) for function in response["functions"] | ||
| ] | ||
| response["advancedfeatures"] = [ | ||
| AdvancedFeature(**feature) for feature in response["advancedfeatures"] | ||
| ] | ||
| return SiteInfo(**response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """Tests for the site module.""" | ||
|
|
||
| from py_moodle.session import MoodleSession | ||
| from py_moodle.site import SiteInfo, get_site_info | ||
|
|
||
|
|
||
| def test_get_site_info_real(request): | ||
| """Test get_site_info in a real environment.""" | ||
| env = request.config.moodle_target.name | ||
| ms = MoodleSession.get(env) | ||
| site_info = get_site_info(ms) | ||
|
|
||
| assert isinstance(site_info, SiteInfo) | ||
| assert site_info.sitename is not None | ||
| assert site_info.username is not None | ||
| assert site_info.firstname is not None | ||
| assert site_info.lastname is not None | ||
| assert site_info.fullname is not None | ||
| assert site_info.lang is not None | ||
| assert site_info.userid is not None | ||
| assert site_info.siteurl is not None | ||
| assert site_info.userpictureurl is not None | ||
| assert site_info.functions is not None | ||
| assert site_info.downloadfiles is not None | ||
| assert site_info.uploadfiles is not None | ||
| assert site_info.release is not None | ||
| assert site_info.version is not None | ||
| assert site_info.mobilecssurl is not None | ||
| assert site_info.advancedfeatures is not None | ||
| assert site_info.usercanmanageownfiles is not None | ||
| assert site_info.userquota is not None | ||
| assert site_info.usermaxuploadfilesize is not None | ||
| assert site_info.userhomepage is not None | ||
| assert site_info.userprivateaccesskey is not None | ||
| assert site_info.siteid is not None | ||
| assert site_info.sitecalendartype is not None | ||
| assert site_info.usercalendartype is not None | ||
| assert site_info.userissiteadmin is not None | ||
| assert site_info.theme is not None | ||
| assert site_info.limitconcurrentlogins is not None | ||
| assert site_info.policyagreed is not None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DataclassEncoder class is defined inside the function scope. Consider moving this to module level or a utility module to improve reusability and avoid recreating the class on each function call.