| 418 | | |
| 419 | | class ControllerMessage(Message): |
| 420 | | """A message that accepts a callable and arguments. |
| 421 | | |
| 422 | | Example usage:: |
| 423 | | |
| 424 | | import turbomail |
| 425 | | message = turbomail.ControllerMessage( |
| 426 | | "from@host.com", |
| 427 | | "to@host.com", |
| 428 | | "Subject", |
| 429 | | controller.method, |
| 430 | | dict() |
| 431 | | ) |
| 432 | | |
| 433 | | Feel free to set a message.plain content as the controller only |
| 434 | | returns HTML. If you wish to hand-produce content, use the |
| 435 | | Message class. |
| 436 | | |
| 437 | | This class can likely be improved to automatically generate the |
| 438 | | plain text content from processing the HTML, calling two methods, |
| 439 | | or calling a single method with special arguments. Needs |
| 440 | | discussion. |
| 441 | | """ |
| 442 | | |
| 443 | | def __init__(self, sender, recipient, subject, method, variables={}, **kw): |
| 444 | | """Store the additonal template and variable information. |
| 445 | | |
| 446 | | @param method: Any callable that returns HTML; usually an |
| 447 | | exposed controller method. |
| 448 | | @type method: callable |
| 449 | | |
| 450 | | @param variables: A dictionary containing named variables to |
| 451 | | pass to the method. |
| 452 | | @type variables: dict |
| 453 | | """ |
| 454 | | |
| 455 | | self._method = method |
| 456 | | self._variables = variables |
| 457 | | |
| 458 | | super(ControllerMessage, self).__init__(sender, recipient, subject, **kw) |
| 459 | | |
| 460 | | def _process(self): |
| 461 | | """Automatically generate the rich text content.""" |
| 462 | | |
| 463 | | data = dict(sender=self.sender, recipient=self.recipient, subject=self.subject) |
| 464 | | |
| 465 | | for (i, j) in self._variables.iteritems(): |
| 466 | | if callable(j): data[i] = j() |
| 467 | | else: data[i] = j |
| 468 | | |
| 469 | | self.rich = self._method(**data) |
| 470 | | |
| 471 | | return super(ControllerMessage, self)._process() |