···11# Secret Santa CLI
2233-This is a Secret Santa gane CLI tool. It takes a `.yaml` file to configure the game, setting up the participants and the exclusions.
33+This is a Secret Santa gane CLI tool. It takes a `.yaml` file to configure the game,
44+setting up the participants and the exclusions.
4555-Then, uses [Mailgun API](https://documentation.mailgun.com/docs/mailgun/user-manual/get-started/) to send the results of the draw via email.
66+Then, uses [Mailgun API](https://documentation.mailgun.com/docs/mailgun/user-manual/get-started/)
77+to send the results of the draw via email.
6879## Game config
810···1113secret-santa:
1214 name: "Secret Santa Game 2024"
1315 notification:
1616+ from: "Secret Santa <secretsanta@example.com>"
1417 subject: "Secret Santa result!" # Subject of the email
1515- template: "notification.html" # Jinja2 template name from templates folder. Optional.
1818+ template_file: "notification.html" # Jinja2 template name from templates folder. Optional.
1919+ # You can also specify the template directly in the config file, and this template,
2020+ # if defined, would be used as first option
2121+ template: |
2222+ <p>Hello {{ from_name }},</p>
2323+ <p>You have been assigned to be the Secret Santa for <strong>{{ to_name }}</strong>!</p>
2424+ <p>Make sure to keep this a secret and prepare a thoughtful gift.</p>
2525+ <p>Happy Holidays! 🎅🎁</p>
1626 participants:
1727 - name: Alice
1828 email: alice@example.com
···33433444## Environment variables
35453636-Some environment variables have to be set in order to use Mailgun API. YOu can also create a local `.env` file with the variables.
4646+Some environment variables have to be set in order to use Mailgun API. You can also
4747+create a local `.env` file with the variables.
37483849```
3950# Mailgun configuration
···43544455## Usage
45564646-This package uses `uv` to handle the dependencies and virtual environment. The script can be executed by running:
5757+This package uses `uv` to handle the dependencies and virtual environment. The script
5858+can be executed by running:
47594860```bash
4961uv run secretsanta <config_yaml_file> [--dry]
···2929class Game(BaseModel):
3030 """A game game is a set of players."""
31313232+ # name of the game
3233 name: str
33343434- notification_template: str = "notification.html"
3535- notification_from: str = "Amigo Invisible <amigoinvisible@mgabarda.com>"
3636- notification_subject: str = "Sorteo Amigo Invisible"
3535+ # notification options for the game
3636+ notification_template_file: str = "notification.html"
3737+ notification_from: str = "Secret Santa <secretsanta@example.com>"
3838+ notification_subject: str = "Secret Santa"
3939+ notification_template: str | None = None
37404141+ # players and exclusions
3842 players: dict[str, Player]
3943 exclusions: list[tuple[str, str]]
4044···4650 with config_file.open() as file:
4751 config = yaml.load(file, Loader=Loader)
48524949- secret_santa_config = config["secret-santa"]
5353+ secret_santa_config = config["secretsanta"]
50545155 # load players
5256 players = {
···7276 )
73777478 # load notification if defined
7575- notification_config = secret_santa_config.get("notification")
7676- if notification_config.get("template"):
7777- game.notification_template = notification_config.get("template")
7878- if notification_config.get("subject"):
7979- game.notification_subject = notification_config.get("subject")
7979+ notification_config = secret_santa_config.get("notification", {})
8080+ game.notification_from = notification_config.get("from", game.notification_from)
8181+ game.notification_subject = notification_config.get(
8282+ "subject", game.notification_subject
8383+ )
8484+ game.notification_template_file = notification_config.get(
8585+ "template_file", game.notification_template_file
8686+ )
8787+ game.notification_template = notification_config.get("template")
80888189 return game
8290
+9-3
src/secretsanta/notifications.py
···3434 print("From:", game.notification_from)
3535 print("To:", _from.email)
3636 print("Subject:", game.notification_subject)
3737+ print("---")
3738 print(content)
3939+ print("---\n")
3840 else:
3941 response = httpx.post(
4042 f"{settings.mailgun_api_url}/messages",
···5355 """Notify the result of the draw in the game."""
54565557 # load template
5656- templates_path = Path(__file__).parent / "templates"
5757- environment = Environment(loader=FileSystemLoader(templates_path))
5858- template = environment.get_template(game.notification_template)
5858+ if game.notification_template:
5959+ environment = Environment()
6060+ template = environment.from_string(game.notification_template)
6161+ else:
6262+ templates_path = Path(__file__).parent / "templates"
6363+ environment = Environment(loader=FileSystemLoader(templates_path))
6464+ template = environment.get_template(game.notification_template_file)
59656066 # iterate over solution
6167 for _from_name, _to_name in draw.solution:
+4-9
src/secretsanta/templates/notification.html
···11-<p>¡Hola {{ from_name }}!</p>
22-33-<p>Tienes que regalar a: <strong>{{ to_name }}</strong>.</p>
44-55-<p>Recuerda que hemos establecido un presupuesto de entre 20 y 25 euros.</p>
66-77-<p><strong>🎄 ¡Feliz Navidad! 🎄</strong></p>
88-99-<p>🎅 El Amigo Invisible 🎅</p>
11+<p>Hello {{ from_name }},</p>
22+<p>You have been assigned to be the Secret Santa for <strong>{{ to_name }}</strong>!</p>`
33+<p>Make sure to keep this a secret and prepare a thoughtful gift.</p>
44+<p>Happy Holidays! 🎅🎁</p>