Haven't tested this yet, but I think it might be a way to go about this particular problem.This seems to do the trick rather nicely. Comments welcome!
from UserString import UserString
def get_dynamic_sender():
return "Sender A" # expand here...
class DynamicEmailValue(UserString):
_data = None
@property
def data(self):
return self._data.format(name=get_dynamic_sender())
@data.setter
def data(self, value):
self._data = value
Instances of this class behave just like regular strings:
>>> DEFAULT_FROM_EMAIL = DynamicEmailValue('{name} <some.mail@example.com>')
>>> DEFAULT_FROM_EMAIL
'Sender A <some.mail@example.com>'
>>> "sent by " + DEFAULT_FROM_EMAIL + " two days ago"
'sent by Sender A <some.mail@example.com> two days ago'
>>> "from: {}".format(DEFAULT_FROM_EMAIL)
'from: Sender A <some.mail@example.com>'
>>> "from: %s" % DEFAULT_FROM_EMAIL
'from: Sender A <some.mail@example.com>'
>>> dynamic_sender = "Sender B"
>>> "from: %s" % DEFAULT_FROM_EMAIL
'from: Sender B <some.mail@example.com>'
Note:
- Works only in Python 2.x
- Not 100% tested with Django (settings might be cached at some places, etc.)
- Assigning a new value to it after initialization (
DEFAULT_FROM_EMAIL = 'new value'
) renders the whole thing mute, unless the new value also contains {name}
. But settings should not get written to, so this probably won't happen... hopefully... yeah...