[7358] | 1 | ## $Id: utils.py 7568 2012-02-02 21:09:31Z 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 | |
---|
[7568] | 42 | def storage(self): |
---|
| 43 | """Return the initial storage path of the data center. |
---|
| 44 | """ |
---|
| 45 | return os.path.dirname(__file__).replace('utils','files') |
---|
| 46 | |
---|
[7404] | 47 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
[7402] | 48 | from_username,usertype,portal,body,subject): |
---|
[7358] | 49 | """Send an email with data provided by forms. |
---|
| 50 | """ |
---|
| 51 | config = grok.getSite()['configuration'] |
---|
| 52 | text = """Fullname: %s |
---|
| 53 | User Id: %s |
---|
| 54 | User Type: %s |
---|
| 55 | Portal: %s |
---|
| 56 | |
---|
| 57 | %s |
---|
| 58 | """ |
---|
[7402] | 59 | body = text % (from_name,from_username,usertype,portal,body) |
---|
[7400] | 60 | return send_mail( |
---|
[7402] | 61 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
[7359] | 62 | |
---|
| 63 | def fullname(self,firstname,lastname,middlename=None): |
---|
[7477] | 64 | """Full name constructor. |
---|
| 65 | """ |
---|
[7359] | 66 | # We do not necessarily have the middlename attribute |
---|
| 67 | if middlename: |
---|
[7365] | 68 | return string.capwords( |
---|
| 69 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
[7359] | 70 | else: |
---|
[7365] | 71 | return string.capwords( |
---|
| 72 | '%s %s' % (firstname, lastname)) |
---|
| 73 | |
---|
| 74 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
[7477] | 75 | """Generate a random password. |
---|
| 76 | """ |
---|
[7365] | 77 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
| 78 | |
---|
[7382] | 79 | |
---|
[7407] | 80 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
[7399] | 81 | """Send credentials as email. |
---|
| 82 | |
---|
| 83 | Input is the applicant for which credentials are sent and the |
---|
| 84 | password. |
---|
| 85 | |
---|
| 86 | Returns True or False to indicate successful operation. |
---|
[7365] | 87 | """ |
---|
[7399] | 88 | subject = 'Your SIRP credentials' |
---|
[7365] | 89 | text = """Dear %s, |
---|
| 90 | |
---|
| 91 | %s |
---|
| 92 | Student Registration and Information Portal of |
---|
[7368] | 93 | %s. |
---|
[7365] | 94 | |
---|
[7368] | 95 | Your user name: %s |
---|
| 96 | Your password: %s |
---|
[7365] | 97 | Login page: %s |
---|
| 98 | |
---|
| 99 | Please remember your user name and keep |
---|
| 100 | your password secret! |
---|
| 101 | |
---|
[7382] | 102 | Please also note that passwords are case-sensitive. |
---|
| 103 | |
---|
[7365] | 104 | Regards |
---|
| 105 | """ |
---|
[7399] | 106 | config = grok.getSite()['configuration'] |
---|
| 107 | from_name = config.name_admin |
---|
[7402] | 108 | from_addr = config.email_admin |
---|
[7407] | 109 | rcpt_name = user.title |
---|
| 110 | rcpt_addr = user.email |
---|
[7382] | 111 | body = text % ( |
---|
[7407] | 112 | rcpt_name, msg,config.name,user.name,password,login_url) |
---|
[7399] | 113 | return send_mail( |
---|
[7402] | 114 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|