-
Notifications
You must be signed in to change notification settings - Fork 3k
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
SDK: Add organization support to the high-level layer #5718
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8597c3f
cvat-sdk/cvat_sdk/core/client.py: refactor _get_repo
SpecLad da5fbe8
SDK: Add organization support to the high-level layer
SpecLad 3fcd4fe
Add organization context support to high-level API
SpecLad 629df6b
Update CHANGELOG
SpecLad 907a674
Add an example for client.current_organization
SpecLad 9108f31
Rename `current_organization` to `organization_slug`
SpecLad 4e468cc
SDK: Add a context manager to set the org slug
SpecLad 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 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 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 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,37 @@ | ||
# Copyright (C) 2022 CVAT.ai Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
|
||
from cvat_sdk.api_client import apis, models | ||
from cvat_sdk.core.proxies.model_proxy import ( | ||
ModelCreateMixin, | ||
ModelDeleteMixin, | ||
ModelListMixin, | ||
ModelRetrieveMixin, | ||
ModelUpdateMixin, | ||
build_model_bases, | ||
) | ||
|
||
_OrganizationEntityBase, _OrganizationRepoBase = build_model_bases( | ||
models.OrganizationRead, apis.OrganizationsApi, api_member_name="organizations_api" | ||
) | ||
|
||
|
||
class Organization( | ||
models.IOrganizationRead, | ||
_OrganizationEntityBase, | ||
ModelUpdateMixin[models.IPatchedOrganizationWriteRequest], | ||
ModelDeleteMixin, | ||
): | ||
_model_partial_update_arg = "patched_organization_write_request" | ||
|
||
|
||
class OrganizationsRepo( | ||
_OrganizationRepoBase, | ||
ModelCreateMixin[Organization, models.IOrganizationWriteRequest], | ||
ModelListMixin[Organization], | ||
ModelRetrieveMixin[Organization], | ||
): | ||
_entity_type = Organization | ||
This file contains 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 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 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,84 @@ | ||
# Copyright (C) 2022 CVAT.ai Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import io | ||
from logging import Logger | ||
from typing import Tuple | ||
|
||
import pytest | ||
from cvat_sdk import Client, models | ||
from cvat_sdk.api_client import exceptions | ||
from cvat_sdk.core.proxies.organizations import Organization | ||
|
||
|
||
class TestOrganizationUsecases: | ||
@pytest.fixture(autouse=True) | ||
def setup( | ||
self, | ||
fxt_login: Tuple[Client, str], | ||
fxt_logger: Tuple[Logger, io.StringIO], | ||
fxt_stdout: io.StringIO, | ||
): | ||
logger, self.logger_stream = fxt_logger | ||
self.client, self.user = fxt_login | ||
self.client.logger = logger | ||
|
||
api_client = self.client.api_client | ||
for k in api_client.configuration.logger: | ||
api_client.configuration.logger[k] = logger | ||
|
||
yield | ||
|
||
assert fxt_stdout.getvalue() == "" | ||
|
||
@pytest.fixture() | ||
def fxt_organization(self) -> Organization: | ||
org = self.client.organizations.create( | ||
models.OrganizationWriteRequest( | ||
slug="testorg", | ||
name="Test Organization", | ||
description="description", | ||
contact={"email": "nowhere@cvat.invalid"}, | ||
) | ||
) | ||
|
||
try: | ||
yield org | ||
finally: | ||
# It's not allowed to create multiple orgs with the same slug, | ||
# so we have to remove the org at the end of each test. | ||
org.remove() | ||
|
||
def test_can_create_organization(self, fxt_organization: Organization): | ||
assert fxt_organization.slug == "testorg" | ||
assert fxt_organization.name == "Test Organization" | ||
assert fxt_organization.description == "description" | ||
assert fxt_organization.contact == {"email": "nowhere@cvat.invalid"} | ||
|
||
def test_can_retrieve_organization(self, fxt_organization: Organization): | ||
org = self.client.organizations.retrieve(fxt_organization.id) | ||
|
||
assert org.id == fxt_organization.id | ||
assert org.slug == fxt_organization.slug | ||
|
||
def test_can_list_organizations(self, fxt_organization: Organization): | ||
orgs = self.client.organizations.list() | ||
|
||
assert fxt_organization.slug in set(o.slug for o in orgs) | ||
|
||
def test_can_update_organization(self, fxt_organization: Organization): | ||
fxt_organization.update( | ||
models.PatchedOrganizationWriteRequest(description="new description") | ||
) | ||
assert fxt_organization.description == "new description" | ||
|
||
retrieved_org = self.client.organizations.retrieve(fxt_organization.id) | ||
assert retrieved_org.description == "new description" | ||
|
||
def test_can_remove_organization(self): | ||
org = self.client.organizations.create(models.OrganizationWriteRequest(slug="testorg2")) | ||
org.remove() | ||
|
||
with pytest.raises(exceptions.NotFoundException): | ||
org.fetch() |
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.
Probably it will be reasonable to add invitations and memberships as well, because it's quite hard to work with orgs without this. I think, just adding the bases with no extra methods is enough for now.
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.
Yeah, fair point. I'll take a look.
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.
Turns out, dealing with invitations is tricky, because they're identified by key instead of ID, which breaks some assumptions in the model mixins. I'll try to solve it, but maybe we can postpone that until the next PR?
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.
Ok, sounds reasonable.