Skip to content

Commit

Permalink
Group Enum common
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Dec 11, 2024
1 parent 85f17ae commit 6c49ba9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 52 deletions.
13 changes: 13 additions & 0 deletions packages/common-library/src/common_library/groups_enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import enum


class GroupType(enum.Enum):
"""
standard: standard group, e.g. any group that is not a primary group or special group such as the everyone group
primary: primary group, e.g. the primary group is the user own defined group that typically only contain the user (same as in linux)
everyone: the only group for all users
"""

STANDARD = "standard"
PRIMARY = "primary"
EVERYONE = "everyone"
18 changes: 4 additions & 14 deletions packages/models-library/src/models_library/groups.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import enum
from typing import Annotated, Final, NamedTuple, TypeAlias

from common_library.basic_types import DEFAULT_FACTORY
from common_library.groups_enums import GroupType as GroupTypeEnum
from pydantic import BaseModel, ConfigDict, EmailStr, Field, field_validator
from pydantic.types import PositiveInt
from typing_extensions import TypedDict
Expand All @@ -14,24 +14,14 @@

GroupID: TypeAlias = PositiveInt


class GroupTypeInModel(str, enum.Enum):
"""
standard: standard group, e.g. any group that is not a primary group or special group such as the everyone group
primary: primary group, e.g. the primary group is the user own defined group that typically only contain the user (same as in linux)
everyone: the only group for all users
"""

STANDARD = "standard"
PRIMARY = "primary"
EVERYONE = "everyone"
__all__: tuple[str, ...] = ("GroupTypeEnum",)


class Group(BaseModel):
gid: PositiveInt
name: str
description: str
group_type: Annotated[GroupTypeInModel, Field(alias="type")]
group_type: Annotated[GroupTypeEnum, Field(alias="type")]
thumbnail: str | None

inclusion_rules: Annotated[
Expand All @@ -42,7 +32,7 @@ class Group(BaseModel):
] = DEFAULT_FACTORY

_from_equivalent_enums = field_validator("group_type", mode="before")(
create_enums_pre_validator(GroupTypeInModel)
create_enums_pre_validator(GroupTypeEnum)
)

model_config = ConfigDict(populate_by_name=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,15 @@
- Groups have a ID, name and a list of users that belong to the group
"""

import enum

import sqlalchemy as sa
from common_library.groups_enums import GroupType
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func

from ._common import RefActions
from .base import metadata


class GroupType(enum.Enum):
"""
standard: standard group, e.g. any group that is not a primary group or special group such as the everyone group
primary: primary group, e.g. the primary group is the user own defined group that typically only contain the user (same as in linux)
everyone: the only group for all users
"""

STANDARD = "standard"
PRIMARY = "primary"
EVERYONE = "everyone"


groups = sa.Table(
"groups",
metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from models_library.api_schemas_catalog.services_specifications import (
ServiceSpecifications,
)
from models_library.groups import GroupAtDB, GroupID, GroupTypeInModel
from models_library.groups import GroupAtDB, GroupID, GroupTypeEnum
from models_library.products import ProductName
from models_library.services import ServiceKey, ServiceVersion
from models_library.users import UserID
Expand Down Expand Up @@ -597,16 +597,16 @@ async def get_service_specifications(
continue
# filter by group type
group = gid_to_group_map[row.gid]
if (group.group_type == GroupTypeInModel.STANDARD) and _is_newer(
if (group.group_type == GroupTypeEnum.STANDARD) and _is_newer(
teams_specs.get(db_service_spec.gid),
db_service_spec,
):
teams_specs[db_service_spec.gid] = db_service_spec
elif (group.group_type == GroupTypeInModel.EVERYONE) and _is_newer(
elif (group.group_type == GroupTypeEnum.EVERYONE) and _is_newer(
everyone_specs, db_service_spec
):
everyone_specs = db_service_spec
elif (group.group_type == GroupTypeInModel.PRIMARY) and _is_newer(
elif (group.group_type == GroupTypeEnum.PRIMARY) and _is_newer(
primary_specs, db_service_spec
):
primary_specs = db_service_spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncpg.exceptions
from aiohttp import web
from models_library.groups import Group, GroupID, GroupTypeInModel
from models_library.groups import Group, GroupID, GroupTypeEnum
from models_library.projects import ProjectID
from models_library.users import UserID
from simcore_postgres_database.errors import DatabaseError
Expand Down Expand Up @@ -86,9 +86,9 @@ async def get_new_project_owner_gid(
if access_rights[other_gid]["write"] is not True:
continue

if group.group_type == GroupTypeInModel.STANDARD:
if group.group_type == GroupTypeEnum.STANDARD:
standard_groups[other_gid] = access_rights[other_gid]
elif group.group_type == GroupTypeInModel.PRIMARY:
elif group.group_type == GroupTypeEnum.PRIMARY:
primary_groups[other_gid] = access_rights[other_gid]

_logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
NodePatch,
NodeRetrieve,
)
from models_library.groups import EVERYONE_GROUP_ID, Group, GroupID, GroupTypeInModel
from models_library.groups import EVERYONE_GROUP_ID, Group, GroupID, GroupTypeEnum
from models_library.projects import Project, ProjectID
from models_library.projects_nodes_io import NodeID, NodeIDStr
from models_library.services import ServiceKeyVersion
Expand Down Expand Up @@ -566,7 +566,7 @@ async def get_project_services_access_for_gid(
raise GroupNotFoundError(gid=query_params.for_gid)

# Update groups to compare based on the type of sharing group
if _sharing_with_group.group_type == GroupTypeInModel.PRIMARY:
if _sharing_with_group.group_type == GroupTypeEnum.PRIMARY:
_user_id = await get_user_id_from_gid(
app=request.app, primary_gid=query_params.for_gid
)
Expand All @@ -575,7 +575,7 @@ async def get_project_services_access_for_gid(
)
groups_to_compare.update(set(user_groups_ids))
groups_to_compare.add(query_params.for_gid)
elif _sharing_with_group.group_type == GroupTypeInModel.STANDARD:
elif _sharing_with_group.group_type == GroupTypeEnum.STANDARD:
groups_to_compare = {query_params.for_gid}

# Initialize a list for inaccessible services
Expand Down
16 changes: 2 additions & 14 deletions services/web/server/tests/unit/isolated/test_groups_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import models_library.groups
import pytest
import simcore_postgres_database.models.groups
from faker import Faker
from models_library.api_schemas_webserver._base import OutputSchema
from models_library.api_schemas_webserver.groups import (
Expand All @@ -14,23 +12,13 @@
AccessRightsDict,
Group,
GroupMember,
GroupTypeInModel,
GroupTypeEnum,
StandardGroupCreate,
StandardGroupUpdate,
)
from models_library.utils.enums import enum_to_dict
from pydantic import ValidationError


def test_models_library_and_postgress_database_enums_are_equivalent():
# For the moment these two libraries they do not have a common library to share these
# basic types so we test here that they are in sync

assert enum_to_dict(
simcore_postgres_database.models.groups.GroupType
) == enum_to_dict(models_library.groups.GroupTypeInModel)


def test_sanitize_legacy_data():
users_group_1 = GroupGet.model_validate(
{
Expand Down Expand Up @@ -66,7 +54,7 @@ def test_output_schemas_from_models(faker: Faker):
gid=1,
name=faker.word(),
description=faker.sentence(),
group_type=GroupTypeInModel.STANDARD,
group_type=GroupTypeEnum.STANDARD,
thumbnail=None,
)
output_schema = GroupGet.from_model(
Expand Down

0 comments on commit 6c49ba9

Please sign in to comment.