Skip to content

Commit 761a3fb

Browse files
author
Andrei Neagu
committed
Merge remote-tracking branch 'upstream/master' into pr-osparc-s3-zip-stream-worker-code
2 parents 9348f7b + 4a00345 commit 761a3fb

File tree

43 files changed

+355
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+355
-53
lines changed

packages/common-library/src/common_library/json_serialization.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
""" Helpers for json serialization
2-
- built-in json-like API
3-
- implemented using orjson, which performs better. SEE https://github.com/ijl/orjson?tab=readme-ov-file#performance
1+
"""Helpers for json serialization
2+
- built-in json-like API
3+
- implemented using orjson, which performs better. SEE https://github.com/ijl/orjson?tab=readme-ov-file#performance
44
"""
55

66
import datetime
@@ -118,6 +118,28 @@ def pydantic_encoder(obj: Any) -> Any:
118118
raise TypeError(msg)
119119

120120

121+
def representation_encoder(obj: Any):
122+
"""
123+
A fallback encoder that uses `pydantic_encoder` to serialize objects.
124+
If serialization fails, it falls back to using `str(obj)`.
125+
126+
This is practical for representation purposes, such as logging or debugging.
127+
128+
Example:
129+
>>> from common_library.json_serialization import json_dumps, representation_encoder
130+
>>> class CustomObject:
131+
... def __str__(self):
132+
... return "CustomObjectRepresentation"
133+
>>> obj = CustomObject()
134+
>>> json_dumps(obj, default=representation_encoder)
135+
'"CustomObjectRepresentation"'
136+
"""
137+
try:
138+
return pydantic_encoder(obj)
139+
except TypeError:
140+
return str(obj)
141+
142+
121143
def json_dumps(
122144
obj: Any,
123145
*,

packages/common-library/tests/test_json_serialization.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
SeparatorTuple,
1414
json_dumps,
1515
json_loads,
16+
representation_encoder,
1617
)
1718
from faker import Faker
1819
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, HttpUrl, TypeAdapter
@@ -110,3 +111,25 @@ class M(BaseModel):
110111
http_url=faker.url(),
111112
)
112113
json_dumps(obj)
114+
115+
116+
def test_json_dumps_with_representation_encoder():
117+
class CustomObject:
118+
def __str__(self):
119+
return "CustomObjectRepresentation"
120+
121+
class SomeModel(BaseModel):
122+
x: int
123+
124+
obj = {
125+
"custom": CustomObject(),
126+
"some": SomeModel(x=42),
127+
}
128+
129+
# Using representation_encoder as the default encoder
130+
result = json_dumps(obj, default=representation_encoder, indent=1)
131+
132+
assert (
133+
result
134+
== '{\n "custom": "CustomObjectRepresentation",\n "some": {\n "x": 42\n }\n}'
135+
)

packages/notifications-library/src/notifications_library/_email_render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class EmailPartsTuple(NamedTuple):
14-
suject: str
14+
subject: str
1515
text_content: str
1616
html_content: str | None
1717

@@ -58,5 +58,5 @@ def render_email_parts(
5858
html_content = None
5959

6060
return EmailPartsTuple(
61-
suject=subject, text_content=text_content, html_content=html_content
61+
subject=subject, text_content=text_content, html_content=html_content
6262
)

packages/notifications-library/src/notifications_library/_models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ class JinjaTemplateDbGet:
1616

1717
@dataclass(frozen=True)
1818
class UserData:
19+
user_name: str
1920
first_name: str
2021
last_name: str
2122
email: str
2223

2324

25+
@dataclass(frozen=True)
26+
class SharerData:
27+
user_name: str
28+
message: str
29+
30+
2431
@dataclass(frozen=True)
2532
class ProductUIData:
2633
logo_url: str
2734
strong_color: str
35+
project_alias: str
2836

2937

3038
@dataclass(frozen=True)

packages/notifications-library/src/notifications_library/_repository.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class UsersRepo(_BaseRepo):
2525
async def get_user_data(self, user_id: UserID) -> UserData:
2626
query = sa.select(
2727
# NOTE: careful! privacy applies here!
28+
users.c.name,
2829
users.c.first_name,
2930
users.c.last_name,
3031
users.c.email,
@@ -38,7 +39,10 @@ async def get_user_data(self, user_id: UserID) -> UserData:
3839
raise ValueError(msg)
3940

4041
return UserData(
41-
first_name=row.first_name, last_name=row.last_name, email=row.email
42+
user_name=row.name,
43+
first_name=row.first_name,
44+
last_name=row.last_name,
45+
email=row.email,
4246
)
4347

4448

packages/notifications-library/src/notifications_library/templates/on_change_email.email.content.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends 'base.html' %}
22
{% block title %} {% include 'on_change_email.email.subject.txt' %} {% endblock %}
33
{% block content %}
4-
<p>Dear {{ user.first_name }},</p>
4+
<p>Dear {{ user.first_name or user.user_name }},</p>
55
<p>
66
You have requested to change the email address linked to your {{ product.display_name }} account.
77
Please follow the on-screen information in the link below:

packages/notifications-library/src/notifications_library/templates/on_change_email.email.content.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Dear {{ user.first_name }},
1+
Dear {{ user.first_name or user.user_name }},
22

33
You have requested to change the email address linked to your {{ product.display_name }} account. Please follow the on-screen information in the link below:
44

packages/notifications-library/src/notifications_library/templates/on_new_code.email.content.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends 'base.html' %}
22
{% block title %} {% include 'on_new_code.email.subject.txt' %} {% endblock %}
33
{% block content %}
4-
<p>Dear {{ user.first_name }},</p>
4+
<p>Dear {{ user.first_name or user.user_name }},</p>
55
<p>Please find below the Two-factor Authentication sign-in code for your {{ host }} account.</p>
66

77
<p>

packages/notifications-library/src/notifications_library/templates/on_new_code.email.content.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Dear {{ user.first_name }},
1+
Dear {{ user.first_name or user.user_name }},
22

33
Please find below the Two-factor Authentication sign-in code for your {{ host }} account.
44

packages/notifications-library/src/notifications_library/templates/on_new_invitation.email.content.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends 'base.html' %}
22
{% block title %} {% include 'on_new_invitation.email.subject.txt' %} {% endblock %}
33
{% block content %}
4-
<p>Dear {{ user.first_name }},</p>
4+
<p>Dear {{ user.first_name or user.user_name }},</p>
55
<p>Thank you for your interest in {{ product.display_name }}. We are pleased to provide you with a one-time invitation link to register on the platform.</p>
66
<p>
77
<!-- Many email clients strip or ignore certain CSS properties, including them inline -->

0 commit comments

Comments
 (0)