Skip to content

Commit

Permalink
[Addons] feat: widgets and addons v2
Browse files Browse the repository at this point in the history
  • Loading branch information
amirsalarsafaei committed Oct 29, 2024
1 parent ecdf0dc commit 3863be5
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 211 deletions.
30 changes: 22 additions & 8 deletions kenar/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreatePostAddonRequest(BaseModel):

@field_serializer("widgets")
def serialize_widgets(self, widgets, _info):
return {"widget_list": [w.serialize_model() for w in widgets]}
return [w.serialize_model() for w in widgets]


class CreatePostAddonResponse(BaseModel):
Expand Down Expand Up @@ -71,19 +71,22 @@ class PostAddon(BaseModel):
semantic: Dict[str, str] = None
semantic_sensitives: List[str] = None

class Config:
exclude= {"semantic_sensitives"}


@field_validator("widgets", mode="before")
@classmethod
def deserialize_model(cls, widgets: Dict):
widget_list = widgets.get("widget_list", [])
return [
get_widget_class(w["widget_type"]).deserialize_model(w) for w in widget_list
get_widget_class(w.keys()).deserialize_model(w) for w in widgets
]

@field_serializer("widgets")
def serialize_widgets(self, widgets, _info):
if widgets:
p = [w.serialize_model() for w in widgets]
return {"widget_list": p}
return p
return None


Expand Down Expand Up @@ -121,11 +124,23 @@ class CreateUserAddonRequest(BaseModel):
categories: List[str]
ticket_uuid: Optional[str] = None
verification_cost: Optional[int] = None
cost: Optional[int] = None

@field_validator("cost", mode="before")
@classmethod
def set_cost_from_verification(cls, cost: Optional[int], values):
if cost is None and values.get("verification_cost") is not None:
return values.get("verification_cost")
return cost

class Config:
exclude = {"verification_cost", "management_permalink",
"notes", "removal_permalink", "semantic_sensitives"}

@field_serializer("widgets")
def serialize_widgets(self, widgets, _info):
p = [w.serialize_model() for w in widgets]
return {"widget_list": p}
return p


class CreateUserAddonResponse(BaseModel):
Expand Down Expand Up @@ -160,16 +175,15 @@ class UserAddon(BaseModel):
@field_validator("widgets", mode="before")
@classmethod
def deserialize_model(cls, widgets: Dict):
widget_list = widgets.get("widget_list", [])
return [
get_widget_class(w["widget_type"]).deserialize_model(w) for w in widget_list
get_widget_class(w.keys()).deserialize_model(w) for w in widgets
]

@field_serializer("widgets")
def serialize_widgets(self, widgets, _info):
if widgets:
p = [w.serialize_model() for w in widgets]
return {"widget_list": p}
return p
return None


Expand Down
10 changes: 5 additions & 5 deletions kenar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def create_user_addon(
@retry(max_retries=max_retry, delay=retry_delay)
def send_request():
return self._client.post(
url=f"/v1/open-platform/addons/user/{data.phone}",
content=data.json(),
url=f"/v2/open-platform/addons/user/{data.phone}",
content=data.model_dump_json(),
headers={ACCESS_TOKEN_HEADER_NAME: access_token},
)

Expand Down Expand Up @@ -222,7 +222,7 @@ def get_user_addons(
def send_request():
return self._client.get(
url=f"/v1/open-platform/addons/user/{data.phone}",
params=data.json(),
params=data.model_dump_json(),
)

rsp = send_request()
Expand All @@ -238,8 +238,8 @@ def create_post_addon(
@retry(max_retries=max_retry, delay=retry_delay)
def send_request():
return self._client.post(
url=f"/v1/open-platform/addons/post/{data.token}",
content=data.json(),
url=f"/v2/open-platform/addons/post/{data.token}",
content=data.model_dump_json(exclude={"token"}),
headers={ACCESS_TOKEN_HEADER_NAME: access_token},
)

Expand Down
1 change: 0 additions & 1 deletion kenar/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .event_row import EventRow
from .group_info_row import GroupInfo
from .image_carousel_row import ImageCarouselRow
from .legend_title_row import LegendTitleRow
from .score_row import ScoreRow
from .selector_row import SelectorRow
from .subtitle_row import SubtitleRow
Expand Down
9 changes: 2 additions & 7 deletions kenar/widgets/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ def get_action(link: str) -> Dict:
return (
{
"action": {
"type": "LOAD_WEB_VIEW_PAGE",
"fallback_link": link,
"payload": {
"@type": "type.googleapis.com/widgets.LoadWebViewPagePayload",
"url": link,
},
"open_direct_link": link,
}
}
if len(link) > 0
Expand All @@ -19,4 +14,4 @@ def get_action(link: str) -> Dict:


def get_link_from_action(action: Dict) -> str:
return action["payload"]["url"]
return action.get("open_direct_link", "")
10 changes: 1 addition & 9 deletions kenar/widgets/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@


class Color(Enum):
COLOR_UNSPECIFIED = "COLOR_UNSPECIFIED"
SUCCESS_PRIMARY = "SUCCESS_PRIMARY"
SUCCESS_SECONDARY = "SUCCESS_SECONDARY"
WARNING_PRIMARY = "WARNING_PRIMARY"
WARNING_SECONDARY = "WARNING_SECONDARY"
ERROR_PRIMARY = "ERROR_PRIMARY"
TEXT_PRIMARY = "TEXT_PRIMARY"
TEXT_SECONDARY = "TEXT_SECONDARY"
TEXT_HINT = "TEXT_HINT"
TEXT_DIVIDER = "TEXT_DIVIDER"
ICON_PRIMARY = "ICON_PRIMARY"
ICON_SECONDARY = "ICON_SECONDARY"
ICON_HINT = "ICON_HINT"
ICON_DIVIDER = "ICON_DIVIDER"
WHITE_PRIMARY = "WHITE_PRIMARY"
11 changes: 4 additions & 7 deletions kenar/widgets/description_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class DescriptionRow(BaseModel, BaseWidget):
text: str
has_divider: bool = False
is_primary: bool
is_primary: bool = False
expandable: bool = False
padded: bool = False
preview_max_line: int = 0
Expand All @@ -26,13 +26,10 @@ def check_preview_max_line(self) -> Self:

def serialize_model(self) -> dict:
return {
"widget_type": "DESCRIPTION_ROW",
"data": {"@type": "type.googleapis.com/widgets.DescriptionRowData"}
| self.model_dump(),
"description_row": self.model_dump(exclude={"is_primary", "padded"}),
}

@classmethod
def deserialize_model(cls, data: Dict):
widget_data = data.get("data", {})
widget_data.pop("@type", None)
return cls.parse_obj(widget_data)
widget_data = data.get("description_row", {})
return cls.model_validate(widget_data)
26 changes: 16 additions & 10 deletions kenar/widgets/evaluation_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

from pydantic import BaseModel, field_validator

from kenar.icons import Icon
from kenar.icons import Icon, IconName
from kenar.widgets.base import BaseWidget
from kenar.widgets.color import Color


class EvaluationRow(BaseModel, BaseWidget):
class Section(BaseModel):
text: str
text_color: Color
text_color: Color = Color.COLOR_UNSPECIFIED
section_color: Color

class Config:
exclude = {"text_color"}


indicator_text: str

indicator_percentage: int
indicator_icon: Icon
indicator_icon: Icon = Icon(icon_name=IconName.UNKNOWN)

indicator_color: Color
indicator_color: Color = Color.COLOR_UNSPECIFIED

left: Section
middle: Section
Expand All @@ -33,13 +37,15 @@ def check_indicator_percentage(cls, indicator_percentage: int) -> int:

def serialize_model(self) -> dict:
return {
"widget_type": "EVALUATION_ROW",
"data": {"@type": "type.googleapis.com/widgets.EvaluationRowData"}
| self.model_dump(),
"evaluation_row": self.model_dump(exclude={"indicator_color", "indicator_icon"})
| self.indicator_icon.model_dump() ,
}

@classmethod
def deserialize_model(cls, data: Dict):
widget_data = data.get("data", {})
widget_data.pop("@type", None)
return cls.parse_obj(widget_data)
widget_data = data.get("evaluation_row", {})

widget_data["icon"] = {
"icon_name": "UNKNOWN"
}
return cls.model_validate(widget_data)
24 changes: 15 additions & 9 deletions kenar/widgets/event_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pydantic import BaseModel

from kenar.icons import Icon
from kenar.icons import Icon, IconName
from kenar.widgets.action import get_action, get_link_from_action
from kenar.widgets.base import BaseWidget

Expand All @@ -17,21 +17,27 @@ class EventRow(BaseModel, BaseWidget):
has_divider: bool = False
link: str = ""
padded: bool = False
icon: Icon
icon: Icon = Icon(icon_name=IconName.UNKNOWN)

def serialize_model(self) -> dict:
return {
"widget_type": "EVENT_ROW",
"data": {"@type": "type.googleapis.com/widgets.EventRowData"}
| self.dict(exclude={"link"})
| get_action(link=self.link),
"event_row": self.model_dump(exclude={"link", "icon", "image_url"})
| get_action(link=self.link) | {"image_id": self.image_url} | self.icon.model_dump()
}

class Config:
exclude = {"padded", "has_indicator"}

@classmethod
def deserialize_model(cls, data: Dict):
widget_data = data.get("data", {})
widget_data.pop("@type", None)
widget_data = data.get("event_row", {})
if "action" in widget_data:
widget_data["link"] = get_link_from_action(widget_data["action"])
widget_data.pop("action", None)
return cls.parse_obj(widget_data)
widget_data["image_url"] = widget_data.pop("image_id", "")

widget_data["icon"] = {
"icon_name": widget_data.pop("icon_name", "UNKNOWN")
}

return cls.model_validate(widget_data)
6 changes: 2 additions & 4 deletions kenar/widgets/group_info_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ def check_items_length(cls, items: List[GroupInfoItem]) -> List[GroupInfoItem]:

def serialize_model(self) -> dict:
return {
"widget_type": "GROUP_INFO_ROW",
"data": {"@type": "type.googleapis.com/widgets.GroupInfoRow"} | self.dict(),
"group_info_row": self.model_dump(),
}

@classmethod
def deserialize_model(cls, data: Dict):
widget_data = data.get("data", {})
widget_data.pop("@type", None)
widget_data = data.get("group_info_row", {})
return cls.parse_obj(widget_data)
14 changes: 9 additions & 5 deletions kenar/widgets/image_carousel_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ class ImageCarouselRowItem(BaseModel):
image_url: str
description: str

def serialize_model(self) -> dict:
return {
"image_id" : self.image_url,
"description": self.description
}

items: List[ImageCarouselRowItem]
has_divider: bool = False


def serialize_model(self) -> dict:
return {
"widget_type": "IMAGE_CAROUSEL_ROW",
"data": {"@type": "type.googleapis.com/widgets.ImageCarouselRowData"} | self.dict(),
"image_carousel_row": self.model_dump(),
}

@classmethod
def deserialize_model(cls, data: Dict):
widget_data = data.get("data", {})
widget_data.pop("@type", None)
return cls.parse_obj(widget_data)
widget_data = data.get("image_carousel_row", {})
return cls.model_validate(widget_data)
38 changes: 0 additions & 38 deletions kenar/widgets/legend_title_row.py

This file was deleted.

Loading

0 comments on commit 3863be5

Please sign in to comment.