Skip to content

Commit

Permalink
try to record comments in user json as wish dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Perceval ARENOU committed Nov 16, 2023
1 parent 481e67e commit 3060c54
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
24 changes: 8 additions & 16 deletions src/flantier/_commands_santa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/flantier/_keyboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 26 additions & 2 deletions src/flantier/_santa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)


Expand All @@ -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))
Expand All @@ -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()
Expand Down
27 changes: 20 additions & 7 deletions src/flantier/_users.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 3060c54

Please sign in to comment.