-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added favorite management using Telegram (#386)
- Loading branch information
Showing
6 changed files
with
306 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from models.config import Config | ||
from models.cron import Cron | ||
from models.favorites import Favorites | ||
from models.item import Item | ||
from models.location import Location | ||
from models.metrics import Metrics | ||
from models.reservations import Reservations | ||
|
||
__all__ = ['Config', 'Cron', 'Item', 'Metrics', 'Location', 'Reservations'] | ||
__all__ = ['Config', 'Cron', 'Favorites', 'Item', | ||
'Metrics', 'Location', 'Reservations'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logging | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from models.errors import TgtgAPIError | ||
from models.item import Item | ||
from tgtg import TgtgClient | ||
|
||
log = logging.getLogger("tgtg") | ||
|
||
|
||
@dataclass | ||
class AddFavoriteRequest(): | ||
item_id: str | ||
item_display_name: str | ||
proceed: bool | ||
|
||
|
||
@dataclass | ||
class RemoveFavoriteRequest(): | ||
item_id: str | ||
item_display_name: str | ||
proceed: bool | ||
|
||
|
||
class Favorites(): | ||
def __init__(self, client: TgtgClient) -> None: | ||
self.client = client | ||
|
||
def is_item_favorite(self, item_id: str) -> bool: | ||
"""Returns true if the provided item ID is in the favorites | ||
Args: | ||
item_id (str): Item ID | ||
Returns: | ||
bool: true, if the provided item ID is in the favorites | ||
""" | ||
return any(item for item | ||
in self.client.get_favorites() | ||
if Item(item).item_id == item_id) | ||
|
||
def get_item_by_id(self, item_id: str) -> Item: | ||
"""Gets an item by the Item ID | ||
Args: | ||
item_id (str): Item ID | ||
Returns: | ||
Item: the Item for the Item ID or an empty Item | ||
""" | ||
try: | ||
return Item(self.client.get_item(item_id)) | ||
except TgtgAPIError: | ||
return Item({}) | ||
|
||
def get_favorites(self) -> List[Item]: | ||
"""Get all favorite items | ||
Return: | ||
List: List of favorite items | ||
""" | ||
return [Item(item) for item in self.client.get_favorites()] | ||
|
||
def add_favorites(self, item_ids: List[str]) -> None: | ||
"""Adds all the provided item IDs to the favorites | ||
Args: | ||
item_ids (str): Item ID list | ||
""" | ||
for item_id in item_ids: | ||
self.client.set_favorite(item_id, True) | ||
|
||
def remove_favorite(self, item_ids: List[str]) -> None: | ||
"""Removes all the provided item IDs from the favorites | ||
Args: | ||
item_ids (str): Item ID list | ||
""" | ||
for item_id in item_ids: | ||
self.client.set_favorite(item_id, False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.