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