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

Upgrade invitations service (Pydantic v2) #6513

7 changes: 6 additions & 1 deletion packages/common-library/src/common_library/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from common_library.pydantic_fields_extension import get_type
from pydantic import BaseModel, SecretStr
from pydantic_core import Url


def model_dump_with_secrets(
Expand All @@ -19,11 +20,15 @@ def model_dump_with_secrets(
if isinstance(field_data, timedelta):
data[field_name] = field_data.total_seconds()

if isinstance(field_data, SecretStr):
elif isinstance(field_data, SecretStr):
if show_secrets:
data[field_name] = field_data.get_secret_value()
else:
data[field_name] = str(field_data)

elif isinstance(field_data, Url):
data[field_name] = str(field_data)

elif isinstance(field_data, dict):
field_type = get_type(settings_obj.model_fields[field_name])
if issubclass(field_type, BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion packages/models-library/src/models_library/invitations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class InvitationContent(InvitationInputs):
created: datetime = Field(..., description="Timestamp for creation")

def as_invitation_inputs(self) -> InvitationInputs:
return self.copy(exclude={"created"})
return self.model_validate(self.model_dump(exclude={"created"})) # copy excluding "created"

@classmethod
def create_from_inputs(
Expand Down
118 changes: 82 additions & 36 deletions services/invitations/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,32 @@
"description": "Invitee's email. Note that the registration can ONLY be used with this email"
},
"trial_account_days": {
"type": "integer",
"exclusiveMinimum": true,
"anyOf": [
{
"type": "integer",
"exclusiveMinimum": true,
"minimum": 0
},
{
"type": "null"
}
],
"title": "Trial Account Days",
"description": "If set, this invitation will activate a trial account.Sets the number of days from creation until the account expires",
"minimum": 0
"description": "If set, this invitation will activate a trial account.Sets the number of days from creation until the account expires"
},
"extra_credits_in_usd": {
"type": "integer",
"exclusiveMinimum": true,
"anyOf": [
{
"type": "integer",
"exclusiveMinimum": true,
"minimum": 0
},
{
"type": "null"
}
],
"title": "Extra Credits In Usd",
"description": "If set, the account's primary wallet will add extra credits corresponding to this ammount in USD",
"minimum": 0
"description": "If set, the account's primary wallet will add extra credits corresponding to this ammount in USD"
},
"product": {
"type": "string",
Expand All @@ -208,13 +222,12 @@
"created"
],
"title": "ApiInvitationContent",
"description": "Data in an invitation",
"example": {
"issuer": "issuerid",
"created": "2023-01-11 13:11:47.293595",
"guest": "[email protected]",
"trial_account_days": 2,
"issuer": "issuerid",
"product": "osparc",
"created": "2023-01-11 13:11:47.293595"
"trial_account_days": 2
}
},
"ApiInvitationContentAndLink": {
Expand All @@ -233,18 +246,32 @@
"description": "Invitee's email. Note that the registration can ONLY be used with this email"
},
"trial_account_days": {
"type": "integer",
"exclusiveMinimum": true,
"anyOf": [
{
"type": "integer",
"exclusiveMinimum": true,
"minimum": 0
},
{
"type": "null"
}
],
"title": "Trial Account Days",
"description": "If set, this invitation will activate a trial account.Sets the number of days from creation until the account expires",
"minimum": 0
"description": "If set, this invitation will activate a trial account.Sets the number of days from creation until the account expires"
},
"extra_credits_in_usd": {
"type": "integer",
"exclusiveMinimum": true,
"anyOf": [
{
"type": "integer",
"exclusiveMinimum": true,
"minimum": 0
},
{
"type": "null"
}
],
"title": "Extra Credits In Usd",
"description": "If set, the account's primary wallet will add extra credits corresponding to this ammount in USD",
"minimum": 0
"description": "If set, the account's primary wallet will add extra credits corresponding to this ammount in USD"
},
"product": {
"type": "string",
Expand Down Expand Up @@ -275,14 +302,13 @@
"invitation_url"
],
"title": "ApiInvitationContentAndLink",
"description": "Data in an invitation",
"example": {
"issuer": "issuerid",
"created": "2023-01-11 13:11:47.293595",
"guest": "[email protected]",
"trial_account_days": 2,
"invitation_url": "https://foo.com/#/registration?invitation=1234",
"issuer": "issuerid",
"product": "osparc",
"created": "2023-01-11 13:11:47.293595",
"invitation_url": "https://foo.com/#/registration?invitation=1234"
"trial_account_days": 2
}
},
"ApiInvitationInputs": {
Expand All @@ -301,21 +327,42 @@
"description": "Invitee's email. Note that the registration can ONLY be used with this email"
},
"trial_account_days": {
"type": "integer",
"exclusiveMinimum": true,
"anyOf": [
{
"type": "integer",
"exclusiveMinimum": true,
"minimum": 0
},
{
"type": "null"
}
],
"title": "Trial Account Days",
"description": "If set, this invitation will activate a trial account.Sets the number of days from creation until the account expires",
"minimum": 0
"description": "If set, this invitation will activate a trial account.Sets the number of days from creation until the account expires"
},
"extra_credits_in_usd": {
"type": "integer",
"exclusiveMinimum": true,
"anyOf": [
{
"type": "integer",
"exclusiveMinimum": true,
"minimum": 0
},
{
"type": "null"
}
],
"title": "Extra Credits In Usd",
"description": "If set, the account's primary wallet will add extra credits corresponding to this ammount in USD",
"minimum": 0
"description": "If set, the account's primary wallet will add extra credits corresponding to this ammount in USD"
},
"product": {
"type": "string",
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Product",
"description": "If None, it will use INVITATIONS_DEFAULT_PRODUCT"
}
Expand All @@ -326,10 +373,9 @@
"guest"
],
"title": "ApiInvitationInputs",
"description": "Input data necessary to create an invitation",
"example": {
"issuer": "issuerid",
"guest": "[email protected]",
"issuer": "issuerid",
"trial_account_days": 2
}
},
Expand Down
42 changes: 23 additions & 19 deletions services/invitations/requirements/_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ aiormq==6.8.0
# via aio-pika
aiosignal==1.3.1
# via aiohttp
annotated-types==0.7.0
# via pydantic
anyio==4.3.0
# via
# fast-depends
Expand All @@ -31,10 +33,6 @@ arrow==1.3.0
# -r requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/../../../packages/service-library/requirements/_base.in
async-timeout==4.0.3
# via
# aiohttp
# redis
attrs==23.2.0
# via
# aiohttp
Expand Down Expand Up @@ -69,18 +67,10 @@ dnspython==2.6.1
# via email-validator
email-validator==2.1.1
# via pydantic
exceptiongroup==1.2.0
# via anyio
fast-depends==2.4.2
# via faststream
fastapi==0.99.1
fastapi==0.115.0
# via
# -c requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../packages/settings-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/settings-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../requirements/constraints.txt
# -r requirements/../../../packages/service-library/requirements/_fastapi.in
# -r requirements/_base.in
# prometheus-fastapi-instrumentator
Expand Down Expand Up @@ -149,12 +139,11 @@ prometheus-fastapi-instrumentator==6.1.0
# via -r requirements/../../../packages/service-library/requirements/_fastapi.in
pycparser==2.21
# via cffi
pydantic==1.10.14
pydantic==2.9.2
# via
# -c requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../packages/settings-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../packages/settings-library/requirements/_base.in
# -c requirements/../../../packages/service-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/settings-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../requirements/constraints.txt
Expand All @@ -165,14 +154,30 @@ pydantic==1.10.14
# -r requirements/../../../packages/settings-library/requirements/_base.in
# fast-depends
# fastapi
# pydantic-extra-types
# pydantic-settings
pydantic-core==2.23.4
# via pydantic
pydantic-extra-types==2.9.0
# via
# -r requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/_base.in
pydantic-settings==2.5.2
# via
# -r requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/_base.in
# -r requirements/../../../packages/service-library/requirements/../../../packages/settings-library/requirements/_base.in
# -r requirements/../../../packages/settings-library/requirements/_base.in
pygments==2.17.2
# via rich
pyinstrument==4.6.2
# via -r requirements/../../../packages/service-library/requirements/_base.in
python-dateutil==2.9.0.post0
# via arrow
python-dotenv==1.0.1
# via uvicorn
# via
# pydantic-settings
# uvicorn
pyyaml==6.0.1
# via
# -c requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
Expand Down Expand Up @@ -214,7 +219,7 @@ sniffio==1.3.1
# via
# anyio
# httpx
starlette==0.27.0
starlette==0.38.6
# via
# -c requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
# -c requirements/../../../packages/service-library/requirements/../../../packages/models-library/requirements/../../../requirements/constraints.txt
Expand All @@ -241,12 +246,11 @@ typing-extensions==4.10.0
# via
# aiodebug
# aiodocker
# anyio
# fastapi
# faststream
# pydantic
# pydantic-core
# typer
# uvicorn
uvicorn==0.29.0
# via
# -r requirements/../../../packages/service-library/requirements/_fastapi.in
Expand Down
14 changes: 0 additions & 14 deletions services/invitations/requirements/_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ coverage==7.6.1
# via
# -r requirements/_test.in
# pytest-cov
exceptiongroup==1.2.0
# via
# -c requirements/_base.txt
# anyio
# hypothesis
# pytest
faker==27.0.0
# via -r requirements/_test.in
h11==0.14.0
Expand Down Expand Up @@ -90,11 +84,3 @@ sortedcontainers==2.4.0
# via hypothesis
termcolor==2.4.0
# via pytest-sugar
tomli==2.0.1
# via
# coverage
# pytest
typing-extensions==4.10.0
# via
# -c requirements/_base.txt
# anyio
11 changes: 0 additions & 11 deletions services/invitations/requirements/_tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,11 @@ ruff==0.6.1
# via -r requirements/../../../requirements/devenv.txt
setuptools==73.0.1
# via pip-tools
tomli==2.0.1
# via
# -c requirements/_test.txt
# black
# build
# mypy
# pip-tools
# pylint
tomlkit==0.13.2
# via pylint
typing-extensions==4.10.0
# via
# -c requirements/_base.txt
# -c requirements/_test.txt
# astroid
# black
# mypy
virtualenv==20.26.3
# via pre-commit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ async def create_invitation(
default_product=settings.INVITATIONS_DEFAULT_PRODUCT,
)
invitation = ApiInvitationContentAndLink(
**invitation_content.dict(),
**invitation_content.model_dump(),
invitation_url=invitation_link,
)

_logger.info("New invitation: %s", f"{invitation.json(indent=1)}")
_logger.info("New invitation: %s", f"{invitation.model_dump_json(indent=1)}")

return invitation

Expand Down
Loading
Loading