Ignore:
Timestamp:
Mar 20, 2010, 5:31:07 AM (15 years ago)
Author:
Alex Dehnert <adehnert@…>
Branches:
master, client
Children:
c020a3b
Parents:
14adb6b
git-author:
Alex Dehnert <adehnert@…> (03/20/10 05:31:07)
git-committer:
Alex Dehnert <adehnert@…> (03/20/10 05:31:07)
Message:

Allow sending certain stock emails (Closes: #10)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • remit/vouchers/models.py

    r6054f18 re8550be  
    3636            ('can_list', 'Can list requests',),
    3737            ('can_approve', 'Can approve requests',),
     38            ('can_email', 'Can send mail about requests',),
    3839        )
    3940
     
    107108        )
    108109
     110
     111class StockEmail:
     112    def __init__(self, name, label, recipients, template, subject_template, context, ):
     113        """
     114        Initialize a stock email object.
     115       
     116        Each argument is required.
     117       
     118        name:       Short name. Letters, numbers, and hyphens only.
     119        label:      User-readable label. Briefly describe what the email says
     120        recipients: Who receives the email. List of "recipient" (check recipient), "area" (area owner), "admins" (site admins)
     121        template:   Django template filename with the actual text
     122        subject_template: Django template string with the subject
     123        context:    Type of context the email needs. Must be 'request' currently.
     124        """
     125
     126        self.name       = name
     127        self.label      = label
     128        self.recipients = recipients
     129        self.template   = template
     130        self.subject_template = subject_template
     131        self.context    = context
     132
     133    def send_email_request(self, request,):
     134        """
     135        Send an email that requires context "request".
     136        """
     137
     138        assert self.context == 'request'
     139
     140        # Generate text
     141        from django.template import Context, Template
     142        from django.template.loader import get_template
     143        ctx = Context({
     144            'prefix': settings.EMAIL_SUBJECT_PREFIX,
     145            'request': request,
     146            'sender': settings.USER_EMAIL_SIGNATURE,
     147        })
     148        tmpl = get_template(self.template)
     149        body = tmpl.render(ctx)
     150        subject_tmpl = Template(self.subject_template)
     151        subject = subject_tmpl.render(ctx)
     152
     153        # Generate recipients
     154        recipients = []
     155        for rt in self.recipients:
     156            if rt == 'recipient':
     157                recipients.append(request.check_to_email)
     158            elif rt == 'area':
     159                recipients.append(request.budget_area.owner_address())
     160            elif rt == 'admins':
     161                pass # you don't *actually* have a choice...
     162        for name, addr in settings.ADMINS:
     163            recipients.append(addr)
     164
     165        # Send mail!
     166        from django.core.mail import send_mail
     167        send_mail(
     168            subject,
     169            body,
     170            settings.SERVER_EMAIL,
     171            recipients,
     172        )
     173
     174stock_emails = {
     175    'nodoc': StockEmail(
     176        name='nodoc',
     177        label='No documentation',
     178        recipients=['recipient', 'area',],
     179        template='vouchers/emails/no_docs_user.txt',
     180        subject_template='{{prefix}}Missing documentation for reimbursement',
     181        context = 'request',
     182    ),
     183    'voucher-sao': StockEmail(
     184        name='voucher-sao',
     185        label='Voucher submitted to SAO',
     186        recipients=['recipient', ],
     187        template='vouchers/emails/voucher_sao_user.txt',
     188        subject_template='{{prefix}}Reimbursement sent to SAO for processing',
     189        context = 'request',
     190    ),
     191}
Note: See TracChangeset for help on using the changeset viewer.