[7358] | 1 | ## $Id: utils.py 7620 2012-02-09 19:29:24Z 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 | """ |
---|
[7568] | 20 | import os |
---|
[7358] | 21 | import grok |
---|
[7365] | 22 | import string |
---|
| 23 | from random import SystemRandom as r |
---|
[7358] | 24 | from waeup.sirp.interfaces import ISIRPUtils |
---|
[7471] | 25 | from waeup.sirp.smtp import send_mail as send_mail_internally |
---|
[7358] | 26 | |
---|
[7471] | 27 | def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): |
---|
| 28 | """Wrapper for the real SMTP functionality in :mod:`waeup.sirp.smtp`. |
---|
[7382] | 29 | |
---|
[7471] | 30 | Merely here to stay compatible with lots of calls to this place. |
---|
[7400] | 31 | """ |
---|
[7471] | 32 | mail_id = send_mail_internally( |
---|
| 33 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
| 34 | subject, body, config) |
---|
[7399] | 35 | return True |
---|
| 36 | |
---|
[7358] | 37 | class SIRPUtils(grok.GlobalUtility): |
---|
| 38 | """A collection of methods subject to customization. |
---|
| 39 | """ |
---|
| 40 | grok.implements(ISIRPUtils) |
---|
| 41 | |
---|
[7404] | 42 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
[7402] | 43 | from_username,usertype,portal,body,subject): |
---|
[7358] | 44 | """Send an email with data provided by forms. |
---|
| 45 | """ |
---|
| 46 | config = grok.getSite()['configuration'] |
---|
| 47 | text = """Fullname: %s |
---|
| 48 | User Id: %s |
---|
| 49 | User Type: %s |
---|
| 50 | Portal: %s |
---|
| 51 | |
---|
| 52 | %s |
---|
| 53 | """ |
---|
[7402] | 54 | body = text % (from_name,from_username,usertype,portal,body) |
---|
[7400] | 55 | return send_mail( |
---|
[7402] | 56 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
[7359] | 57 | |
---|
| 58 | def fullname(self,firstname,lastname,middlename=None): |
---|
[7477] | 59 | """Full name constructor. |
---|
| 60 | """ |
---|
[7359] | 61 | # We do not necessarily have the middlename attribute |
---|
| 62 | if middlename: |
---|
[7365] | 63 | return string.capwords( |
---|
| 64 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
[7359] | 65 | else: |
---|
[7365] | 66 | return string.capwords( |
---|
| 67 | '%s %s' % (firstname, lastname)) |
---|
| 68 | |
---|
| 69 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 70 | """Generate a random password. |
---|
| 71 | """ |
---|
[7365] | 72 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 73 | |
---|
[7382] | 74 | |
---|
[7407] | 75 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
[7399] | 76 | """Send credentials as email. |
---|
| 77 | |
---|
| 78 | Input is the applicant for which credentials are sent and the |
---|
| 79 | password. |
---|
| 80 | |
---|
| 81 | Returns True or False to indicate successful operation. |
---|
[7365] | 82 | """ |
---|
[7399] | 83 | subject = 'Your SIRP credentials' |
---|
[7365] | 84 | text = """Dear %s, |
---|
| 85 | |
---|
| 86 | %s |
---|
| 87 | Student Registration and Information Portal of |
---|
[7368] | 88 | %s. |
---|
[7365] | 89 | |
---|
[7368] | 90 | Your user name: %s |
---|
| 91 | Your password: %s |
---|
[7365] | 92 | Login page: %s |
---|
| 93 | |
---|
| 94 | Please remember your user name and keep |
---|
| 95 | your password secret! |
---|
| 96 | |
---|
[7382] | 97 | Please also note that passwords are case-sensitive. |
---|
| 98 | |
---|
[7365] | 99 | Regards |
---|
| 100 | """ |
---|
[7399] | 101 | config = grok.getSite()['configuration'] |
---|
| 102 | from_name = config.name_admin |
---|
[7402] | 103 | from_addr = config.email_admin |
---|
[7407] | 104 | rcpt_name = user.title |
---|
| 105 | rcpt_addr = user.email |
---|
[7382] | 106 | body = text % ( |
---|
[7407] | 107 | rcpt_name, msg,config.name,user.name,password,login_url) |
---|
[7399] | 108 | return send_mail( |
---|
[7402] | 109 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|