diff --git a/src/flantier/_commands_santa.py b/src/flantier/_commands_santa.py index 8565540..57990b3 100644 --- a/src/flantier/_commands_santa.py +++ b/src/flantier/_commands_santa.py @@ -26,6 +26,14 @@ def wishes(update: Update, _: CallbackContext) -> None: ) +def comments(update: Update, _: CallbackContext) -> None: + """Affiche la liste de cadeaux et les commentaires associés d'une personne.""" + keyboard = build_people_inline_kb("comments") + update.message.reply_text( + "🤷 De qui veux tu consulter la liste de souhaits? 🤷", reply_markup=keyboard + ) + + def update_wishes_list(update: Update, context: CallbackContext) -> None: """Met à jour la liste des cadeaux.""" _santa.create_missing_users() @@ -39,22 +47,6 @@ def update_wishes_list(update: Update, context: CallbackContext) -> None: ######### -def comments(update: Update, context: CallbackContext) -> None: - """Affiche la liste de cadeaux et les commentaires associés d'une personne.""" - if len(update.message.text.split(" ")) > 1: - name = update.message.text.split(" ")[1] - - reply_del_kb = ReplyKeyboardRemove() - context.bot.send_message( - chat_id=update.message.chat_id, - text=_santa.find_wishes(update.message.from_user.id, name), - reply_markup=reply_del_kb, - ) - - else: - _keyboards.build_people_keyboard("/commentaires") - - def add_gifter(tg_id: int, message: list) -> str: """Ajoute un offrant à un cadeau. vérifie que la personne existe et la disponiblité du cadeau diff --git a/src/flantier/_keyboards.py b/src/flantier/_keyboards.py index 0e6a70d..f09d270 100644 --- a/src/flantier/_keyboards.py +++ b/src/flantier/_keyboards.py @@ -90,6 +90,9 @@ def user_button(update: Update, _: CallbackContext) -> None: if command == "wishes": text = user_wishes_message(user_name) + if command == "comments": + text = user_comments_message(user_name) + # TODO "/offrir" text = "À qui veux-tu offrir ?" # TODO "/commentaires, /exclude" logger.info("response: %s", text) diff --git a/src/flantier/_santa.py b/src/flantier/_santa.py index 02b87d6..d2ebcad 100644 --- a/src/flantier/_santa.py +++ b/src/flantier/_santa.py @@ -10,7 +10,7 @@ from apiclient.discovery import build from flantier._settings import SettingsManager -from flantier._users import User, UserManager +from flantier._users import User, UserManager, Wish logger = getLogger("flantier") @@ -62,12 +62,22 @@ def update_wishes_list() -> None: for column in range(0, len(values), 2): name = values[column][0] gifts = values[column][1:] + comments = values[column + 1][1:] logger.debug("mise à jour des cadeaux de %s", name) user = user_manager.search_user(name) if not user: pass - user.wishes = gifts + + # user.wishes = list(zip(gifts, comments)) + + wishes = [] + for i, j in zip(gifts, comments): + logger.info("adding wish %s and comment %s", i, j) + wishes.append(Wish(wish=i, comment=j)) + + user.wishes = wishes + logger.info(user.wishes) user_manager.update_user(user) @@ -76,6 +86,11 @@ def get_wish_list(user: User) -> str: return "\n".join(w for w in user.wishes) +def get_comment_list(user: User) -> str: + """Récupère la liste des commentaires d'un participant avec son nom.""" + return "\n".join(w for w in user.comments) + + # called by the people inline keyboard def user_wishes_message(user_name: str) -> str: wishes = get_wish_list(UserManager().search_user(user_name)) @@ -86,6 +101,15 @@ def user_wishes_message(user_name: str) -> str: return text +def user_comments_message(user_name: str) -> str: + wishes = get_wish_list(UserManager().search_user(user_name)) + text = f"🎅 {user_name} voudrait pour Noël:\n" + wishes + if not wishes: + text = f"🎅 {user_name} ne veut rien pour Noël 🫥" + + return text + + def update_gifts_background_task(interval_sec: int = 600) -> None: """Update gifts list in background. Function to be run in a thread""" ticker = threading.Event() diff --git a/src/flantier/_users.py b/src/flantier/_users.py index 2a5d60d..5df99eb 100644 --- a/src/flantier/_users.py +++ b/src/flantier/_users.py @@ -1,5 +1,7 @@ #!/usr/bin/python3 """Gère les utilisateurs stockés dans le fichier de configuration users.json +We are using dataclass to represent Users and wishes (gifts) +source https://www.delftstack.com/howto/python/dataclass-to-json-in-python/ """ import json @@ -12,11 +14,21 @@ logger = getLogger("flantier") -# @dataclass -# class Wish: -# wish: str # cadeaux qui viennent du google doc -# comment: str # commentaires qui viennent du google doc -# giver: str # la personne qui offre ce cadeau +@dataclass +class Wish: + """Represents a wish from a user.""" + + wish: str # cadeaux qui viennent du google doc + comment: str # commentaires qui viennent du google doc + giver: int = 0 # la personne qui offre ce cadeau + + @property + def __dict__(self): + return asdict(self) + + @property + def json(self): + return json.dumps(self.__dict__) @dataclass @@ -29,8 +41,9 @@ class User: giftee: int = 0 # telegram id of the person to offer a gift last_giftee: int = 0 # telegram id of the person who recieved the gift last year registered: bool = False # is the user registered for secret santa - # TODO use Wish dataclass instead of dict - wishes: list = field(default_factory=list) # list of wishes as tuple (wish, comment) + wishes: list[Wish] = field( + default_factory=list[Wish] + ) # list of wishes as Wish objects class UserJSONEncoder(json.JSONEncoder):