This is a Secret Santa game CLI tool.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(notifications): added feature to set the notification template on the yaml config file.

+47 -30
+17 -5
README.md
··· 1 1 # Secret Santa CLI 2 2 3 - This is a Secret Santa gane CLI tool. It takes a `.yaml` file to configure the game, setting up the participants and the exclusions. 3 + This is a Secret Santa gane CLI tool. It takes a `.yaml` file to configure the game, 4 + setting up the participants and the exclusions. 4 5 5 - Then, uses [Mailgun API](https://documentation.mailgun.com/docs/mailgun/user-manual/get-started/) to send the results of the draw via email. 6 + Then, uses [Mailgun API](https://documentation.mailgun.com/docs/mailgun/user-manual/get-started/) 7 + to send the results of the draw via email. 6 8 7 9 ## Game config 8 10 ··· 11 13 secret-santa: 12 14 name: "Secret Santa Game 2024" 13 15 notification: 16 + from: "Secret Santa <secretsanta@example.com>" 14 17 subject: "Secret Santa result!" # Subject of the email 15 - template: "notification.html" # Jinja2 template name from templates folder. Optional. 18 + template_file: "notification.html" # Jinja2 template name from templates folder. Optional. 19 + # You can also specify the template directly in the config file, and this template, 20 + # if defined, would be used as first option 21 + template: | 22 + <p>Hello {{ from_name }},</p> 23 + <p>You have been assigned to be the Secret Santa for <strong>{{ to_name }}</strong>!</p> 24 + <p>Make sure to keep this a secret and prepare a thoughtful gift.</p> 25 + <p>Happy Holidays! 🎅🎁</p> 16 26 participants: 17 27 - name: Alice 18 28 email: alice@example.com ··· 33 43 34 44 ## Environment variables 35 45 36 - Some environment variables have to be set in order to use Mailgun API. YOu can also create a local `.env` file with the variables. 46 + Some environment variables have to be set in order to use Mailgun API. You can also 47 + create a local `.env` file with the variables. 37 48 38 49 ``` 39 50 # Mailgun configuration ··· 43 54 44 55 ## Usage 45 56 46 - This package uses `uv` to handle the dependencies and virtual environment. The script can be executed by running: 57 + This package uses `uv` to handle the dependencies and virtual environment. The script 58 + can be executed by running: 47 59 48 60 ```bash 49 61 uv run secretsanta <config_yaml_file> [--dry]
-4
src/secretsanta/__init__.py
··· 7 7 class Settings(BaseSettings): 8 8 """Configuration for the secret santa game.""" 9 9 10 - # email notification defaults 11 - default_notification_from: str = "Amigo Invisible <amigoinvisible@mgabarda.com>" 12 - default_notification_subject: str = "Sorteo Amigo Invisible" 13 - 14 10 # mailgun settings 15 11 mailgun_api_url: str 16 12 mailgun_api_key: SecretStr
+17 -9
src/secretsanta/models.py
··· 29 29 class Game(BaseModel): 30 30 """A game game is a set of players.""" 31 31 32 + # name of the game 32 33 name: str 33 34 34 - notification_template: str = "notification.html" 35 - notification_from: str = "Amigo Invisible <amigoinvisible@mgabarda.com>" 36 - notification_subject: str = "Sorteo Amigo Invisible" 35 + # notification options for the game 36 + notification_template_file: str = "notification.html" 37 + notification_from: str = "Secret Santa <secretsanta@example.com>" 38 + notification_subject: str = "Secret Santa" 39 + notification_template: str | None = None 37 40 41 + # players and exclusions 38 42 players: dict[str, Player] 39 43 exclusions: list[tuple[str, str]] 40 44 ··· 46 50 with config_file.open() as file: 47 51 config = yaml.load(file, Loader=Loader) 48 52 49 - secret_santa_config = config["secret-santa"] 53 + secret_santa_config = config["secretsanta"] 50 54 51 55 # load players 52 56 players = { ··· 72 76 ) 73 77 74 78 # load notification if defined 75 - notification_config = secret_santa_config.get("notification") 76 - if notification_config.get("template"): 77 - game.notification_template = notification_config.get("template") 78 - if notification_config.get("subject"): 79 - game.notification_subject = notification_config.get("subject") 79 + notification_config = secret_santa_config.get("notification", {}) 80 + game.notification_from = notification_config.get("from", game.notification_from) 81 + game.notification_subject = notification_config.get( 82 + "subject", game.notification_subject 83 + ) 84 + game.notification_template_file = notification_config.get( 85 + "template_file", game.notification_template_file 86 + ) 87 + game.notification_template = notification_config.get("template") 80 88 81 89 return game 82 90
+9 -3
src/secretsanta/notifications.py
··· 34 34 print("From:", game.notification_from) 35 35 print("To:", _from.email) 36 36 print("Subject:", game.notification_subject) 37 + print("---") 37 38 print(content) 39 + print("---\n") 38 40 else: 39 41 response = httpx.post( 40 42 f"{settings.mailgun_api_url}/messages", ··· 53 55 """Notify the result of the draw in the game.""" 54 56 55 57 # load template 56 - templates_path = Path(__file__).parent / "templates" 57 - environment = Environment(loader=FileSystemLoader(templates_path)) 58 - template = environment.get_template(game.notification_template) 58 + if game.notification_template: 59 + environment = Environment() 60 + template = environment.from_string(game.notification_template) 61 + else: 62 + templates_path = Path(__file__).parent / "templates" 63 + environment = Environment(loader=FileSystemLoader(templates_path)) 64 + template = environment.get_template(game.notification_template_file) 59 65 60 66 # iterate over solution 61 67 for _from_name, _to_name in draw.solution:
+4 -9
src/secretsanta/templates/notification.html
··· 1 - <p>¡Hola {{ from_name }}!</p> 2 - 3 - <p>Tienes que regalar a: <strong>{{ to_name }}</strong>.</p> 4 - 5 - <p>Recuerda que hemos establecido un presupuesto de entre 20 y 25 euros.</p> 6 - 7 - <p><strong>🎄 ¡Feliz Navidad! 🎄</strong></p> 8 - 9 - <p>🎅 El Amigo Invisible 🎅</p> 1 + <p>Hello {{ from_name }},</p> 2 + <p>You have been assigned to be the Secret Santa for <strong>{{ to_name }}</strong>!</p>` 3 + <p>Make sure to keep this a secret and prepare a thoughtful gift.</p> 4 + <p>Happy Holidays! 🎅🎁</p>