Skip to content
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

Visibility update #33

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6e6d5ed
Added visibility argument to simplepopulate.py script
diveshjain-phy Dec 18, 2024
1298459
Added CLI support for setting product and collection visibility
diveshjain-phy Dec 18, 2024
240050a
Updated collections.py with the core function of set-visibility for c…
diveshjain-phy Dec 18, 2024
79f807e
Add set_visibility function to hippoclient
diveshjain-phy Dec 18, 2024
07f5f0a
Update product.py with visibility field to the CreateProductRequest a…
diveshjain-phy Dec 18, 2024
f9d6644
Updated relationships.py to make it compatible with updated read_by_i…
diveshjain-phy Dec 18, 2024
d832584
Updated product.py with apt changes to read_by_id and endpoint for se…
diveshjain-phy Dec 18, 2024
21a04b4
Updated __init__.py with enum class for Visibility and fields in prod…
diveshjain-phy Dec 18, 2024
facb9bb
Updated product.py with apt changes to read_by_id and read_by_name an…
diveshjain-phy Dec 18, 2024
9827eae
Updated VersionRevision with new field VISIBILITY_REV which does not …
diveshjain-phy Dec 18, 2024
e40dc23
Update collection.html to show visibility field
diveshjain-phy Dec 18, 2024
470da18
Update index.html to show visibility fields
diveshjain-phy Dec 18, 2024
4583be2
Update product.html to show visibility fields
diveshjain-phy Dec 18, 2024
3aafd77
Updated test_product_api.py to be consistent with the api layer
diveshjain-phy Dec 18, 2024
e8a807b
Update test_relationship_api.py for updates with api layer
diveshjain-phy Dec 18, 2024
e26daa3
Update test_product_service.py
diveshjain-phy Dec 18, 2024
400a613
Update conftest.py
diveshjain-phy Dec 18, 2024
55dbcda
Update __init__.py
diveshjain-phy Dec 18, 2024
a5dbe03
updated the code to be inline with ruff
diveshjain-phy Dec 18, 2024
b93f0d7
Updated code by introducing flag for Visibility_Update in Privilege
diveshjain-phy Jan 13, 2025
ac89c44
Modified the incorrect get request to post request
diveshjain-phy Jan 13, 2025
a9cf444
Updated ValueError for invalid Visibility Enum values
diveshjain-phy Jan 15, 2025
45dbcf8
Updated type safety in class CreateProductRequest and class UpdatePro…
diveshjain-phy Jan 15, 2025
0ba09cf
updated collection client and exception handling
diveshjain-phy Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/hilton/simplepopulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Populates the simple example server with a bunch of ACT maps.
"""

import argparse
from pathlib import Path

from hippoclient import Client
Expand All @@ -25,6 +26,21 @@
identical to v1.0.
"""


parser = argparse.ArgumentParser(
description="Populate ACT maps to the server with visbility."
)
parser.add_argument(
"--visibility",
type=str,
choices=["public", "private", "collaboration"],
default="collaboration",
help="Set visibility for the products.",
)
args = parser.parse_args()

USER_VISIBILITY = args.visibility

catalogs = {
"DR5_cluster-catalog_v1.1.fits": CatalogMetadata(
file_type="fits",
Expand Down Expand Up @@ -130,6 +146,7 @@
metadata=catalogs[catalog],
sources=[Path(catalog)],
source_descriptions=["Catalog file"],
visibility=USER_VISIBILITY,
)

add_to_collection(
Expand All @@ -145,6 +162,7 @@
metadata=mask,
sources=[Path("DR5_cluster-search-area-mask_v1.0.fits")],
source_descriptions=["Mask file"],
visibility=USER_VISIBILITY,
)

add_to_collection(
Expand Down
29 changes: 29 additions & 0 deletions hippoclient/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ def product_read(id: str):
CONSOLE.print("Parents: " + ", ".join(product_extracted_version.child_of))


@product_app.command("set-visibility")
def product_set_visibility(id: str, visibility: str):
"""
Set the visibility level of a product.

Example:
$ hippo product set-visibility <product-id> <visibility>
"""

global CLIENT
sc.product.set_visibility(client=CLIENT, id=id, visibility=visibility)
CONSOLE.print(f"Visibility set to {visibility} for product {id}.")
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved


@product_app.command("delete")
def product_delete(id: str):
"""
Expand Down Expand Up @@ -148,6 +162,21 @@ def collection_cache(id: str):
CONSOLE.print(f"Cached collection {id} including {len(response)} files")


@collection_app.command("set-visibility")
def collection_set_visibility(id: str, visibility: str):
"""
Set the visibility level of a collection.

Example:
$ hippo collection set-visibility <collection-id> <visibility>
"""
global CLIENT
sc.collections.set_collection_visibility(
client=CLIENT, collection_id=id, visibility=visibility
)
CONSOLE.print(f"Visibility set to {visibility} for collection {id}.")


@collection_app.command("uncache")
def collection_uncache(id: str):
"""
Expand Down
59 changes: 59 additions & 0 deletions hippoclient/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from pathlib import Path

from hippoclient.product import set_visibility as set_product_visibility
from hipposerve.api.models.relationships import ReadCollectionResponse
from hipposerve.database import Visibility

from .core import Client, MultiCache, console
from .product import cache as cache_product
Expand Down Expand Up @@ -272,3 +274,60 @@ def uncache(client: Client, cache: MultiCache, id: str) -> None:

for product in collection.products:
uncache_product(client, cache, product.id)


def set_collection_visibility(
client: Client, collection_id: str, visibility: str
) -> None:
"""
Update the visibility of all products in a collection.

Arguments
---------
client: Client
The client to use for interacting with the hippo API.
collection_id : str
The ID of the collection whose products' visibility is being updated.
visibility : str
The new visibility level ('public', 'collaboration', or 'private').

Raises
------
ValueError
If an invalid visibility level is provided.
httpx.HTTPStatusError
If a request to the API fails.
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved
"""

# Validate visibility
try:
visibility_enum = Visibility(visibility)
except ValueError:
raise ValueError(
"Invalid visibility level. Choose from 'public', 'collaboration', or 'private'."
)

# Fetch the collection details
collection = read(client, collection_id)

# Update visibility for each product in the collection
console.print(
f"Updating visibility to '{visibility}' for all products in collection '{collection.name}'...",
style="blue",
)
for product in collection.products:
try:
set_product_visibility(client, product.id, visibility_enum.value)
console.print(
f"Successfully updated visibility for product {product.id}.",
style="green",
)
except Exception as e:
console.print(
f"Failed to update product {product.id}: {str(e)}", style="red"
)
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved

console.print(
f"Successfully updated visibility for all products in collection '{collection.name}'.",
style="bold green",
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved
)
47 changes: 46 additions & 1 deletion hippoclient/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hippometa import ALL_METADATA_TYPE
from hippometa.simple import SimpleMetadata
from hipposerve.api.models.product import ReadProductResponse
from hipposerve.database import ProductMetadata
from hipposerve.database import ProductMetadata, Visibility
from hipposerve.service.product import PostUploadFile

from .core import Client, MultiCache, console
Expand All @@ -22,6 +22,7 @@ def create(
metadata: ALL_METADATA_TYPE,
sources: list[Path],
source_descriptions: list[str | None],
visibility: str = "collaboration",
) -> str:
"""
Create a product in hippo.
Expand Down Expand Up @@ -57,6 +58,12 @@ def create(
If a request to the API fails
"""

# Validate visibility
if visibility not in ["public", "collaboration", "private"]:
raise ValueError(
"Invalid visibility level. Choose from 'public', 'collaboration', or 'private'."
)

# Check and validate the sources.
assert len(sources) == len(source_descriptions)

Expand Down Expand Up @@ -88,6 +95,7 @@ def create(
"description": description,
"metadata": metadata.model_dump(),
"sources": source_metadata,
"visibility": visibility,
},
)

Expand Down Expand Up @@ -352,3 +360,40 @@ def uncache(client: Client, cache: MultiCache, id: str) -> None:
console.print(f"Removed file {source.name} ({source.uuid}) from cache")

return


def set_visibility(client: Client, id: str, visibility: str) -> None:
"""
Update the visibility of a product.

Arguments
---------
client: Client
The client to use for interacting with the hippo API.
collection_id : str
The ID of the product whose visibility is being updated.
visibility : str
The new visibility level ('public', 'collaboration', or 'private').

Raises
------
ValueError
If an invalid visibility level is provided.
"""
# Validate the provided visibility level
try:
visibility_enum = Visibility(visibility) # Convert string to Visibility enum
except ValueError:
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
"Invalid visibility level. Choose from 'public', 'collaboration', or 'private'."
)
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved

response = client.get(f"/product/{id}/set-visibility/{visibility}")
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved

response.raise_for_status()

if client.verbose:
console.print(
f"Successfully updated product {id} to {visibility_enum.value} visibility.",
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved
style="bold green",
)
2 changes: 2 additions & 0 deletions hipposerve/api/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CreateProductRequest(BaseModel):
description: str
metadata: ALL_METADATA_TYPE
sources: list[PreUploadFile]
visibility: str


class CreateProductResponse(BaseModel):
Expand Down Expand Up @@ -43,6 +44,7 @@ class UpdateProductRequest(BaseModel):
replace_sources: list[PreUploadFile] = []
drop_sources: list[str] = []
level: VersionRevision = VersionRevision.MINOR
visibility: str = "collaboration"
diveshjain-phy marked this conversation as resolved.
Show resolved Hide resolved


class UpdateProductResponse(BaseModel):
Expand Down
Loading