-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cc3de0
commit a3b03f8
Showing
26 changed files
with
738 additions
and
146 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
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
23 changes: 23 additions & 0 deletions
23
django_slack_tools/slack_messages/migrations/0004_slackmessagingpolicy_template_type.py
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,23 @@ | ||
# Generated by Django 4.2.16 on 2024-10-19 05:07 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("slack_messages", "0003_default_recipient"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="slackmessagingpolicy", | ||
name="template_type", | ||
field=models.CharField( | ||
choices=[("D", "Dictionary"), ("DJ", "Django"), ("DI", "Django Inline"), ("?", "Unknown")], | ||
default="D", | ||
help_text="Type of message template.", | ||
max_length=2, | ||
verbose_name="Template type", | ||
), | ||
), | ||
] |
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 was deleted.
Oops, something went wrong.
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,5 @@ | ||
from .base import BaseTemplate | ||
from .dict import DictTemplate | ||
from .django import DjangoTemplate | ||
|
||
__all__ = ("BaseTemplate", "DictTemplate", "DjangoTemplate") |
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,17 @@ | ||
"""Abstraction for dictionary templates.""" | ||
|
||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
|
||
class BaseTemplate(ABC): | ||
"""Abstract base class for dictionary templates.""" | ||
|
||
@abstractmethod | ||
def render(self, *, context: dict[str, Any] | None = None) -> dict: | ||
"""Render template with given context.""" |
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,52 @@ | ||
# noqa: D100 | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, TypeVar | ||
|
||
from .base import BaseTemplate | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DictTemplate(BaseTemplate): | ||
"""Simple dictionary-based template.""" | ||
|
||
def __init__(self, template: dict) -> None: | ||
"""Initialize template. | ||
Args: | ||
template: Dictionary template. | ||
kwargs: Keyword arguments passed to template. | ||
""" | ||
self.template = template | ||
|
||
def render(self, *, context: dict[str, Any] | None = None) -> dict: # noqa: D102 | ||
context = {} if context is None else context | ||
result = self.template.copy() | ||
for k, v in result.items(): | ||
result[k] = _format_obj(v, context=context) | ||
|
||
return result | ||
|
||
|
||
T = TypeVar("T", dict, list, str) | ||
|
||
|
||
def _format_obj(obj: T, *, context: dict[str, Any]) -> T: | ||
"""Format object recursively.""" | ||
if isinstance(obj, dict): | ||
return {k: _format_obj(v, context=context) for k, v in obj.items()} | ||
|
||
if isinstance(obj, str): | ||
return obj.format_map(context) | ||
|
||
if isinstance(obj, list): | ||
return [_format_obj(item, context=context) for item in obj] | ||
|
||
return obj |
Oops, something went wrong.