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