Skip to content

Commit

Permalink
More documentation and revert change from model_dump to model_dump_js…
Browse files Browse the repository at this point in the history
…on back to model_dump(mode='json') in API.
  • Loading branch information
loechel committed Apr 26, 2024
1 parent 10f99e3 commit 8f73896
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/edutap/wallet_google/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ def save_link(
obj = GoogleWalletObjectWithClassReferenceMixin.model_validate(obj)
if isinstance(obj, GoogleWalletObjectWithClassReferenceMixin):
payload[name].append(
obj.model_dump_json(
obj.model_dump(
# explicitly set to model_dump(mode="json") instead of model_dump_json due to problems
# reported by jensens
mode="json",
exclude_none=True,
exclude_unset=True,
exclude_defaults=True,
Expand All @@ -388,7 +391,14 @@ def save_link(
# otherwise it must be a registered model
model = lookup_model_by_plural_name(name)
obj = _validate_data(model, obj)
obj_json = obj.model_dump_json(exclude_none=True)
obj_json = obj.model_dump(
# explicitly set to model_dump(mode="json") instead of model_dump_json due to problems
# reported by jensens
mode="json",
exclude_none=True,
exclude_unset=True,
exclude_defaults=True,
)
payload[name].append(obj_json)
claims = {
"iat": "",
Expand Down
20 changes: 13 additions & 7 deletions src/edutap/wallet_google/modelbase.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .modelcore import GoogleWalletModel
from .modelcore import GoogleWalletWithIdModel
from .models.primitives import CallbackOptions
from .models.primitives import GroupingInfo
Expand All @@ -14,13 +15,12 @@
from .models.primitives.enums import MultipleDevicesAndHoldersAllowedStatus
from .models.primitives.enums import ViewUnlockRequirement
from .models.primitives.message import Message
from pydantic import BaseModel
from pydantic import Field


class GoogleWalletClassModel(GoogleWalletWithIdModel):
"""
Base model for all Google Wallet Class models.
BaseModel for all Google Wallet Class Models.
"""

# Templating and Visual Data
Expand All @@ -30,6 +30,7 @@ class GoogleWalletClassModel(GoogleWalletWithIdModel):
linksModuleData: LinksModuleData | None = None
infoModuleData: InfoModuleData | None = Field(
description="deprecated",
deprecated=True,
exclude=True,
default=None,
)
Expand All @@ -50,6 +51,7 @@ class GoogleWalletClassModel(GoogleWalletWithIdModel):
)
allowMultipleUsersPerObject: bool | None = Field(
description="deprecated",
deprecated=True,
exclude=True,
default=None,
)
Expand All @@ -64,6 +66,7 @@ class GoogleWalletClassModel(GoogleWalletWithIdModel):
# heroImage: Image | None = None
wordMark: Image | None = Field(
description="deprecated",
deprecated=True,
exclude=True,
default=None,
)
Expand Down Expand Up @@ -103,25 +106,25 @@ class GoogleWalletObjectModel(GoogleWalletWithIdModel):
rotatingBarcode: RotatingBarcode | None = None


class GoogleWalletObjectWithClassReferenceMixin(BaseModel):
class GoogleWalletObjectWithClassReferenceMixin(GoogleWalletModel):
"""
Model for all Google Wallet Object with a Class references.
Mixin for all Google Wallet Object with a classReferences attribute, that reflects the whole class data.
"""

classReference: GoogleWalletClassModel | None = None


class GoogleWalletMessageableMixin:
"""
Model for Google Wallet Classes or Objects that can retrieve Messages
Mixin for Google Wallet Classes or Objects that can retrieve Messages
"""

messages: list[Message] | None = None


class GoogleWalletStyleableClassMixin:
"""
Model for Google Wallet Classes that can be styled
Mixin for Google Wallet Classes that can be styled.
"""

# Design Options
Expand All @@ -133,7 +136,10 @@ class GoogleWalletStyleableClassMixin:

class GoogleWalletStyleableObjectMixin:
"""
Model for Google Wallet Objects that can be styled
Mixin for Google Wallet Objects that can be styled.
TODO: Check if really all Objects that can be styled have all four attributes and also with this name.
Potential Override necessary for programLogo, ...
"""

# Design Options
Expand Down
18 changes: 13 additions & 5 deletions src/edutap/wallet_google/modelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

class GoogleWalletModel(BaseModel):
"""
Base model for all Google Wallet models.
Base Model for all Google Wallet Models.
Sets a model_config for all Google Wallet Models that enforce that all attributes must be explicitly modeled, and trying to set an unknown attribute would raise an Exception.
This Follows the Zen of Python (PEP 20) --> Explicit is better than implicit.
"""

model_config = ConfigDict(
Expand All @@ -17,16 +20,21 @@ class GoogleWalletModel(BaseModel):

class GoogleWalletWithKindMixin(GoogleWalletModel):
"""
Base model for Google Wallet models with an deprecated kind identifier.
Mixin Class for Google Wallet Models with an deprecated kind identifier.
Explicit kind value should be provided by the inheriting concret class.
"""

kind: str | None = Field(description="deprecated", exclude=True, default=None)
kind: str | None = Field(
description="deprecated",
deprecated=True,
exclude=True,
default=None,
)


class GoogleWalletWithIdModel(GoogleWalletWithKindMixin):
"""
Base model for Google Wallet models with an identifier.
Model for Google Wallet models with an identifier.
"""

# kind: str | None = Field(description="deprecated", exclude=True, default=None)
id: str

0 comments on commit 8f73896

Please sign in to comment.