app/soc/logic/mail_dispatcher.py
changeset 1560 656aace0b15f
parent 1551 78c6c14c6b63
child 1600 0aa3de1b2acc
equal deleted inserted replaced
1559:283046e54c01 1560:656aace0b15f
    65 
    65 
    66 
    66 
    67 from django.template import loader
    67 from django.template import loader
    68 
    68 
    69 from google.appengine.api import mail
    69 from google.appengine.api import mail
       
    70 from google.appengine.api import users
    70 
    71 
    71 from soc.logic import dicts
    72 from soc.logic import dicts
    72 
    73 
    73 
    74 
    74 def sendMailFromTemplate(template, context):
    75 def sendMailFromTemplate(template, context):
   112 
   113 
   113   # construct the EmailMessage from the given context
   114   # construct the EmailMessage from the given context
   114   message = mail.EmailMessage(**context)
   115   message = mail.EmailMessage(**context)
   115   message.check_initialized()
   116   message.check_initialized()
   116 
   117 
   117   # send the message
   118   try:
   118   message.send()
   119     # send the message
       
   120     message.send()
       
   121   except Exception, e:
       
   122     import logging
       
   123     logging.info(context)
       
   124     logging.exception(e)
   119 
   125 
   120 def getDefaultMailSender():
   126 def getDefaultMailSender():
   121   """Returns the sender that currently can be used to send emails.
   127   """Returns the sender that currently can be used to send emails.
   122   
   128   
   123   Returns:
   129   Returns:
   139 
   145 
   140   if site_entity.noreply_email:
   146   if site_entity.noreply_email:
   141     return (site_entity.site_name, site_entity.noreply_email)
   147     return (site_entity.site_name, site_entity.noreply_email)
   142 
   148 
   143   # use the email address of the current logged in user
   149   # use the email address of the current logged in user
   144   user_entity = user_logic.logic.getForCurrentAccount()
   150   account = users.get_current_user()
   145 
   151 
   146   if not user_entity:
   152   # we need to retrieve account seperately, as user_logic normalizes it
       
   153   # and the GAE admin API is case sensitive
       
   154   user_entity = user_logic.logic.getForAccount(account)
       
   155 
       
   156   if not account:
   147     logging.warning('Non-Authenticated user triggered getDefaultMailSender')
   157     logging.warning('Non-Authenticated user triggered getDefaultMailSender')
   148     return None
   158     return None
   149 
   159 
   150   # denormalize the account and retrieve the email
   160   return (user_entity.name, account.email())
   151   sender = accounts.denormalizeAccount(user_entity.account).email()
       
   152 
       
   153   return (user_entity.name, sender)