-
Notifications
You must be signed in to change notification settings - Fork 13
feat(medcat-trainer): trainer-ee-scaffolding #524
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| """ | ||
| MedCAT Trainer extension API. | ||
|
|
||
| Stable contract used by enterprise / third-party plugins. The OSS app itself | ||
| emits these signals and consults the registries from a small number of | ||
| explicit sites (see :mod:`api.signals`, :mod:`api.views`, :mod:`api.permissions`). | ||
|
|
||
| Plugins are discovered via the ``mct.plugins`` Python entry-point group; each | ||
| entry point points at a Django ``AppConfig`` subclass. Plugin URL configs are | ||
| mounted at ``/api/ee/<app_label>/`` when the ``AppConfig`` sets | ||
| ``is_mct_plugin = True``. See :mod:`core.settings` and :mod:`core.urls`. | ||
|
|
||
| The shape of this module is contract-tested by ``test_extensions.py`` and the | ||
| ``contract-tests`` CI job; changes that break the documented shape are | ||
| breaking changes. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import copy | ||
| from collections.abc import Callable, Iterable | ||
| from typing import Any, Optional | ||
|
|
||
| from django.dispatch import Signal | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Signals | ||
| # --------------------------------------------------------------------------- | ||
| # | ||
| # All signals MUST include at least the documented kwargs. Extra kwargs may | ||
| # be added over time but documented kwargs are stable. | ||
|
|
||
| #: Sent immediately before a document is submitted to training. | ||
| #: kwargs: ``project`` (ProjectAnnotateEntities), ``document`` (Document), | ||
| #: ``user`` (User or None). | ||
| pre_document_submit = Signal() | ||
|
|
||
| #: Sent immediately after a document is submitted to training. | ||
| #: kwargs: ``project`` (ProjectAnnotateEntities), ``document`` (Document), | ||
| #: ``user`` (User or None). | ||
| post_document_submit = Signal() | ||
|
|
||
| #: Sent after an :class:`AnnotatedEntity` row is created. | ||
| #: kwargs: ``annotation`` (AnnotatedEntity), ``project`` (ProjectAnnotateEntities), | ||
| #: ``document`` (Document), ``user`` (User or None). | ||
| annotation_created = Signal() | ||
|
|
||
| #: Sent after an :class:`AnnotatedEntity` row is updated. | ||
| #: kwargs: ``annotation`` (AnnotatedEntity), ``project`` (ProjectAnnotateEntities), | ||
| #: ``document`` (Document), ``user`` (User or None). | ||
| annotation_updated = Signal() | ||
|
|
||
| #: Sent after an :class:`AnnotatedEntity` row is deleted. | ||
| #: kwargs: ``annotation`` (AnnotatedEntity, instance prior to delete), | ||
| #: ``project`` (ProjectAnnotateEntities), ``document`` (Document). | ||
| annotation_deleted = Signal() | ||
|
|
||
| #: Sent after a :class:`ProjectGroup` row is created. | ||
| #: kwargs: ``project_group`` (ProjectGroup). | ||
| project_group_created = Signal() | ||
|
|
||
| #: Sent after a :class:`ProjectGroup` row is updated. | ||
| #: kwargs: ``project_group`` (ProjectGroup). | ||
| project_group_updated = Signal() | ||
|
|
||
| #: Sent after the OIDC user resolver returns a Django user. | ||
| #: kwargs: ``user`` (User), ``id_token`` (dict), ``created`` (bool). | ||
| user_oidc_resolved = Signal() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Permission hook registry | ||
| # --------------------------------------------------------------------------- | ||
| # | ||
| # Permission hooks are grant-only: a hook returning ``True`` grants the | ||
| # permission; ``None`` or ``False`` abstains and the OSS default decision is | ||
| # used. Hooks MUST NOT be used to deny access that the OSS code would | ||
| # otherwise grant. | ||
| # | ||
| # Hooks are called with two positional arguments. For the ``is_project_admin`` | ||
| # hook these are the ``User`` and ``ProjectAnnotateEntities`` instances; the | ||
| # arguments are typed as ``Any`` so the registry stays generic across hook | ||
| # names without coupling to specific model classes. | ||
|
|
||
| PermissionHook = Callable[[Any, Any], Optional[bool]] | ||
| _permission_hooks: dict[str, list[PermissionHook]] = {} | ||
|
|
||
|
|
||
| def register_permission_hook(name: str, fn: PermissionHook) -> None: | ||
| """Register a grant-only permission hook for ``name``. | ||
|
|
||
| Hooks are called in registration order until one returns ``True``. | ||
| Any value other than ``True`` (including ``None`` and ``False``) abstains. | ||
| """ | ||
| if not callable(fn): | ||
| raise TypeError("permission hook must be callable") | ||
| _permission_hooks.setdefault(name, []).append(fn) | ||
|
|
||
|
|
||
| def get_permission_hooks(name: str) -> Iterable[PermissionHook]: | ||
| return tuple(_permission_hooks.get(name, ())) | ||
|
|
||
|
|
||
| def clear_permission_hooks(name: Optional[str] = None) -> None: | ||
| """Clear hooks. Intended for tests.""" | ||
| if name is None: | ||
| _permission_hooks.clear() | ||
| else: | ||
| _permission_hooks.pop(name, None) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Menu / route / feature registries (frontend bootstrap) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| _menu_extensions: list[dict[str, Any]] = [] | ||
| _plugin_routes: list[dict[str, Any]] = [] | ||
| _features: set[str] = set() | ||
|
|
||
|
|
||
| def register_menu_extension(item: dict[str, Any]) -> None: | ||
| """Register a top-nav menu item exposed via ``GET /api/bootstrap/``. | ||
|
|
||
| ``item`` MUST contain ``id`` (str) and ``label`` (str). It SHOULD | ||
| contain ``route`` (str) or ``href`` (str). Additional keys pass through | ||
| verbatim to the frontend. | ||
| """ | ||
| if not isinstance(item, dict): | ||
| raise TypeError("menu extension item must be a dict") | ||
| if "id" not in item or "label" not in item: | ||
| raise ValueError("menu extension item requires 'id' and 'label'") | ||
| _menu_extensions.append(copy.deepcopy(item)) | ||
|
|
||
|
|
||
| def get_menu_extensions() -> list[dict[str, Any]]: | ||
| return [copy.deepcopy(it) for it in _menu_extensions] | ||
|
|
||
|
|
||
| def register_route(route: dict[str, Any]) -> None: | ||
| """Register a frontend route descriptor exposed via ``GET /api/bootstrap/``. | ||
|
|
||
| ``route`` MUST contain ``path`` (str) and ``component`` (str — module | ||
| specifier or registered component name resolved by the frontend). | ||
| """ | ||
| if not isinstance(route, dict): | ||
| raise TypeError("route must be a dict") | ||
| if "path" not in route or "component" not in route: | ||
| raise ValueError("route requires 'path' and 'component'") | ||
| _plugin_routes.append(copy.deepcopy(route)) | ||
|
|
||
|
|
||
| def get_routes() -> list[dict[str, Any]]: | ||
| return [dict(r) for r in _plugin_routes] | ||
|
|
||
|
|
||
| def register_feature(name: str) -> None: | ||
| """Declare a license-gated feature as enabled.""" | ||
| if not isinstance(name, str) or not name: | ||
| raise ValueError("feature name must be a non-empty string") | ||
| _features.add(name) | ||
|
|
||
|
|
||
| def get_features() -> list[str]: | ||
| return sorted(_features) | ||
|
|
||
|
|
||
| def clear_registries() -> None: | ||
| """Reset menu / route / feature registries. Intended for tests.""" | ||
| _menu_extensions.clear() | ||
| _plugin_routes.clear() | ||
| _features.clear() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Plugin discovery state | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| #: Populated by :mod:`core.settings` at import time with the dotted ``AppConfig`` | ||
| #: paths of installed MCT plugins. Read-only at runtime. | ||
| discovered_plugin_apps: list[str] = [] | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "pre_document_submit", | ||
| "post_document_submit", | ||
| "annotation_created", | ||
| "annotation_updated", | ||
| "annotation_deleted", | ||
| "project_group_created", | ||
| "project_group_updated", | ||
| "user_oidc_resolved", | ||
| "register_permission_hook", | ||
| "get_permission_hooks", | ||
| "clear_permission_hooks", | ||
| "register_menu_extension", | ||
| "get_menu_extensions", | ||
| "register_route", | ||
| "get_routes", | ||
| "register_feature", | ||
| "get_features", | ||
| "clear_registries", | ||
| "discovered_plugin_apps", | ||
| ] | ||
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from django.contrib.auth.forms import User | ||
| from rest_framework import permissions | ||
| from rest_framework.exceptions import PermissionDenied | ||
| from .models import ProjectAnnotateEntities, ProjectGroup | ||
| from .models import ProjectAnnotateEntities | ||
|
|
||
|
|
||
| class IsReadOnly(permissions.BasePermission): | ||
|
|
@@ -14,23 +14,31 @@ def has_permission(self, request, view): | |
| return request.method in permissions.SAFE_METHODS | ||
|
|
||
|
|
||
| def is_project_admin(user, project): | ||
| def is_project_admin(user: [User], project: [ProjectAnnotateEntities]): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is correct type annotation?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah - yep - thanks |
||
| """ | ||
| Check if a user is an admin of a project. | ||
| A user is a project admin if: | ||
| 1. They are a member of the project, OR | ||
| 2. They are an administrator of the project's group (if the project has a group) | ||
| 3. They are a superuser/staff | ||
| 1. They are a superuser/staff, OR | ||
| 2. They are a member of the project, OR | ||
| 3. They are an administrator of the project's group (if the project has a group), OR | ||
| 4. A registered ``is_project_admin`` permission hook grants access. | ||
|
|
||
| Hooks are grant-only (see :mod:`api.extensions`): they may extend the set | ||
| of users considered admins (e.g. via OIDC group claims in an enterprise | ||
| plugin) but never narrow it. | ||
| """ | ||
| if user.is_superuser or user.is_staff: | ||
| return True | ||
|
|
||
| # Check if user is a member of the project | ||
| if project.members.filter(id=user.id).exists(): | ||
| return True | ||
|
|
||
| # Check if user is an administrator of the project's group | ||
| if project.group and project.group.administrators.filter(id=user.id).exists(): | ||
| return True | ||
|
|
||
| from .extensions import get_permission_hooks | ||
| for hook in get_permission_hooks('is_project_admin'): | ||
| if hook(user, project) is True: | ||
| return True | ||
|
|
||
| return False | ||
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
41 changes: 41 additions & 0 deletions
41
medcat-trainer/webapp/api/api/tests/fixtures/bootstrap_schema.json
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 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "$id": "https://cogstack.org/medcat-trainer/bootstrap.json", | ||
| "title": "MedCAT Trainer bootstrap payload", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "required": ["features", "menu_extensions", "routes"], | ||
| "properties": { | ||
| "features": { | ||
| "type": "array", | ||
| "items": { "type": "string", "minLength": 1 } | ||
| }, | ||
| "menu_extensions": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "object", | ||
| "required": ["id", "label"], | ||
| "properties": { | ||
| "id": { "type": "string", "minLength": 1 }, | ||
| "label": { "type": "string", "minLength": 1 }, | ||
| "route": { "type": "string" }, | ||
| "href": { "type": "string" } | ||
| }, | ||
| "additionalProperties": true | ||
| } | ||
| }, | ||
| "routes": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "object", | ||
| "required": ["path", "component"], | ||
| "properties": { | ||
| "path": { "type": "string", "minLength": 1 }, | ||
| "component": { "type": "string", "minLength": 1 }, | ||
| "name": { "type": "string" } | ||
| }, | ||
| "additionalProperties": true | ||
| } | ||
| } | ||
| } | ||
| } |
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.
Same note here regarding shallow copy