[7358] | 1 | ## $Id: utils.py 7407 2011-12-20 11:05:07Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """General helper utilities for SIRP. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
| 21 | import smtplib |
---|
[7365] | 22 | import string |
---|
| 23 | from random import SystemRandom as r |
---|
[7358] | 24 | from email.mime.text import MIMEText |
---|
| 25 | from waeup.sirp.interfaces import ISIRPUtils |
---|
| 26 | |
---|
[7382] | 27 | from email.Header import Header |
---|
| 28 | from email.Utils import parseaddr, formataddr |
---|
| 29 | |
---|
[7402] | 30 | def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): |
---|
[7400] | 31 | """ |
---|
| 32 | XXX: While this method is unicode-proof (i.e. it can handle |
---|
| 33 | non-plain-ASCII chars in names and messages) other |
---|
| 34 | methods inhere are not and will fail silently. Some |
---|
| 35 | reorganization of SMPT-related code is needed here. |
---|
| 36 | """ |
---|
[7382] | 37 | |
---|
[7399] | 38 | header_charset = 'ISO-8859-1' |
---|
| 39 | from_name = str(Header(unicode(from_name), header_charset)) |
---|
| 40 | rcpt_name = str(Header(unicode(rcpt_name), header_charset)) |
---|
| 41 | from_addr = from_addr.encode('ascii') |
---|
| 42 | rpct_addr = rcpt_addr.encode('ascii') |
---|
| 43 | for body_charset in ['US-ASCII', 'ISO-8859-1', 'UTF-8']: |
---|
| 44 | try: |
---|
| 45 | body.encode(body_charset) |
---|
| 46 | except UnicodeError: |
---|
[7404] | 47 | #print "FAILED: ", body_charset, body |
---|
[7399] | 48 | pass |
---|
| 49 | else: |
---|
| 50 | break |
---|
| 51 | msg = MIMEText(body.encode(body_charset), 'plain', body_charset) |
---|
| 52 | msg['From'] = formataddr((from_name, from_addr)) |
---|
| 53 | msg['To'] = formataddr((rcpt_name, rcpt_addr)) |
---|
| 54 | msg['Subject'] = Header(unicode(subject), header_charset) |
---|
| 55 | server = smtplib.SMTP(config.smtp_server) |
---|
| 56 | if config.smtp_requires_login: |
---|
| 57 | server.login(config.smtp_username,config.smtp_password) |
---|
| 58 | try: |
---|
[7402] | 59 | server.sendmail(from_addr,rcpt_addr,msg.as_string()) |
---|
[7399] | 60 | except Exception, e: |
---|
| 61 | # XXX: log error message |
---|
| 62 | return False |
---|
| 63 | server.quit() |
---|
| 64 | return True |
---|
| 65 | |
---|
| 66 | |
---|
[7358] | 67 | class SIRPUtils(grok.GlobalUtility): |
---|
| 68 | """A collection of methods subject to customization. |
---|
| 69 | """ |
---|
| 70 | grok.implements(ISIRPUtils) |
---|
| 71 | |
---|
[7404] | 72 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
[7402] | 73 | from_username,usertype,portal,body,subject): |
---|
[7358] | 74 | """Send an email with data provided by forms. |
---|
| 75 | """ |
---|
| 76 | config = grok.getSite()['configuration'] |
---|
| 77 | text = """Fullname: %s |
---|
| 78 | User Id: %s |
---|
| 79 | User Type: %s |
---|
| 80 | Portal: %s |
---|
| 81 | |
---|
| 82 | %s |
---|
| 83 | """ |
---|
[7402] | 84 | body = text % (from_name,from_username,usertype,portal,body) |
---|
[7400] | 85 | return send_mail( |
---|
[7402] | 86 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
[7359] | 87 | |
---|
| 88 | def fullname(self,firstname,lastname,middlename=None): |
---|
| 89 | # We do not necessarily have the middlename attribute |
---|
| 90 | if middlename: |
---|
[7365] | 91 | return string.capwords( |
---|
| 92 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
[7359] | 93 | else: |
---|
[7365] | 94 | return string.capwords( |
---|
| 95 | '%s %s' % (firstname, lastname)) |
---|
| 96 | |
---|
| 97 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
| 98 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 99 | |
---|
[7382] | 100 | |
---|
[7407] | 101 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
[7399] | 102 | """Send credentials as email. |
---|
| 103 | |
---|
| 104 | Input is the applicant for which credentials are sent and the |
---|
| 105 | password. |
---|
| 106 | |
---|
| 107 | Returns True or False to indicate successful operation. |
---|
[7365] | 108 | """ |
---|
[7399] | 109 | subject = 'Your SIRP credentials' |
---|
[7365] | 110 | text = """Dear %s, |
---|
| 111 | |
---|
| 112 | %s |
---|
| 113 | Student Registration and Information Portal of |
---|
[7368] | 114 | %s. |
---|
[7365] | 115 | |
---|
[7368] | 116 | Your user name: %s |
---|
| 117 | Your password: %s |
---|
[7365] | 118 | Login page: %s |
---|
| 119 | |
---|
| 120 | Please remember your user name and keep |
---|
| 121 | your password secret! |
---|
| 122 | |
---|
[7382] | 123 | Please also note that passwords are case-sensitive. |
---|
| 124 | |
---|
[7365] | 125 | Regards |
---|
| 126 | """ |
---|
[7399] | 127 | config = grok.getSite()['configuration'] |
---|
| 128 | from_name = config.name_admin |
---|
[7402] | 129 | from_addr = config.email_admin |
---|
[7407] | 130 | rcpt_name = user.title |
---|
| 131 | rcpt_addr = user.email |
---|
[7382] | 132 | body = text % ( |
---|
[7407] | 133 | rcpt_name, msg,config.name,user.name,password,login_url) |
---|
[7399] | 134 | return send_mail( |
---|
[7402] | 135 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|