Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Feature/carousel on class (#4)
Browse files Browse the repository at this point in the history
* change Carousel widget implementation to class

Co-authored-by: Tamirlan Dzhemirzoev <[email protected]>
  • Loading branch information
d-tamirlan and Tamirlan Dzhemirzoev authored Aug 5, 2021
1 parent 8c09e18 commit d52aede
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 296 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.5.0 (Aug 3, 2021)

### Changed
* **Breaking change:** change `Carousel` widget implementation to class

---

## 0.4.3 (Aug 2, 2021)

### Fix
Expand Down
9 changes: 4 additions & 5 deletions pybotx_widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@


class Widget:
message: Message
bot: Bot
command: str
additional_markup: MessageMarkup = None

def __init__(
self,
message: Message,
Expand All @@ -29,3 +24,7 @@ def __init__(

async def display(self) -> Optional[Any]:
raise NotImplementedError

@classmethod
async def get_value(cls, message: Message, bot: Bot) -> Any:
raise NotImplementedError
40 changes: 17 additions & 23 deletions pybotx_widgets/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dateutil import parser
from dateutil.relativedelta import relativedelta

from botx import BubbleElement, Message, MessageMarkup
from botx import BubbleElement, Message, MessageMarkup, Bot
from pybotx_widgets.base import Widget
from pybotx_widgets.resources import strings
from pybotx_widgets.service import merge_markup, send_or_update_message
Expand All @@ -18,7 +18,7 @@
SELECTED_DATE_KEY = "calendar_selected_date"


class BubblesMixin:
class MarkupMixin:
message: Message
command: str

Expand Down Expand Up @@ -182,11 +182,7 @@ def add_day_bubbles(self) -> None:
self.markup.bubbles.append(calendar_dates_row)


class CalendarWidget(Widget, BubblesMixin):
start_date: date = None
end_date: date = date.max
include_past: bool = False

class CalendarWidget(Widget, MarkupMixin):
LEFT_ARROW = strings.LEFT_ARROW
RIGHT_ARROW = strings.RIGHT_ARROW
AFTER_SELECT_TEXT = strings.CAL_DATE_SELECTED
Expand Down Expand Up @@ -218,29 +214,20 @@ def __init__(
self.month_to_display = self.message.data.get(MONTH_TO_DISPLAY_KEY)
self.selected_date = self.message.data.get(SELECTED_DATE_KEY)

def _clear_calendar_data(self) -> None:
"""Clear widget data form message.data."""

self.message.data.pop(MONTH_TO_DISPLAY_KEY, None)
self.message.data.pop(SELECTED_DATE_KEY, None)

async def get_value(self) -> date:
@classmethod
async def get_value(cls, message: Message, bot: Bot) -> date:
selected_date = message.data[SELECTED_DATE_KEY]
try:
selected_date = parser.parse(self.selected_date).date()
selected_date = parser.parse(selected_date).date()
except parser.ParserError: # type: ignore
raise RuntimeError("Date is not selected.")

self._clear_calendar_data()
_clear_calendar_data(message)
# Remove buttons
await self.update_after_select()
await send_or_update_message(message, bot, cls.AFTER_SELECT_TEXT)

return selected_date

async def update_after_select(self):
"""Update last calendar message, after user selects date."""

await send_or_update_message(self.message, self.bot, self.AFTER_SELECT_TEXT)

def get_prev_and_next_year(self) -> Tuple[date, date]:
month = 1 if self.include_past else date.today().month
prev_year = (self.current_date - relativedelta(years=1)).replace(
Expand All @@ -264,7 +251,7 @@ async def display(self) -> Optional[date]:
self.current_date = parser.parse(self.month_to_display).date()

elif self.selected_date:
return await self.get_value()
return await self.get_value(self.message, self.bot)

self.add_year_bubbles()
self.add_month_bubbles()
Expand All @@ -277,3 +264,10 @@ async def display(self) -> Optional[date]:
await send_or_update_message(
self.message, self.bot, self.SELECT_DATE, self.markup
)


def _clear_calendar_data(message: Message) -> None:
"""Clear widget data form message.data."""

message.data.pop(MONTH_TO_DISPLAY_KEY, None)
message.data.pop(SELECTED_DATE_KEY, None)
Loading

0 comments on commit d52aede

Please sign in to comment.