1 | ## $Id: utils.py 7585 2012-02-04 06:28:29Z 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 os |
---|
21 | import grok |
---|
22 | import string |
---|
23 | from random import SystemRandom as r |
---|
24 | from waeup.sirp.interfaces import ISIRPUtils |
---|
25 | from waeup.sirp.smtp import send_mail as send_mail_internally |
---|
26 | |
---|
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`. |
---|
29 | |
---|
30 | Merely here to stay compatible with lots of calls to this place. |
---|
31 | """ |
---|
32 | mail_id = send_mail_internally( |
---|
33 | from_name, from_addr, rcpt_name, rcpt_addr, |
---|
34 | subject, body, config) |
---|
35 | return True |
---|
36 | |
---|
37 | class SIRPUtils(grok.GlobalUtility): |
---|
38 | """A collection of methods subject to customization. |
---|
39 | """ |
---|
40 | grok.implements(ISIRPUtils) |
---|
41 | |
---|
42 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
43 | from_username,usertype,portal,body,subject): |
---|
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 | """ |
---|
54 | body = text % (from_name,from_username,usertype,portal,body) |
---|
55 | return send_mail( |
---|
56 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
57 | |
---|
58 | def fullname(self,firstname,lastname,middlename=None): |
---|
59 | """Full name constructor. |
---|
60 | """ |
---|
61 | # We do not necessarily have the middlename attribute |
---|
62 | if middlename: |
---|
63 | return string.capwords( |
---|
64 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
65 | else: |
---|
66 | return string.capwords( |
---|
67 | '%s %s' % (firstname, lastname)) |
---|
68 | |
---|
69 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
70 | """Generate a random password. |
---|
71 | """ |
---|
72 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
73 | |
---|
74 | |
---|
75 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
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. |
---|
82 | """ |
---|
83 | subject = 'Your SIRP credentials' |
---|
84 | text = """Dear %s, |
---|
85 | |
---|
86 | %s |
---|
87 | Student Registration and Information Portal of |
---|
88 | %s. |
---|
89 | |
---|
90 | Your user name: %s |
---|
91 | Your password: %s |
---|
92 | Login page: %s |
---|
93 | |
---|
94 | Please remember your user name and keep |
---|
95 | your password secret! |
---|
96 | |
---|
97 | Please also note that passwords are case-sensitive. |
---|
98 | |
---|
99 | Regards |
---|
100 | """ |
---|
101 | config = grok.getSite()['configuration'] |
---|
102 | from_name = config.name_admin |
---|
103 | from_addr = config.email_admin |
---|
104 | rcpt_name = user.title |
---|
105 | rcpt_addr = user.email |
---|
106 | body = text % ( |
---|
107 | rcpt_name, msg,config.name,user.name,password,login_url) |
---|
108 | return send_mail( |
---|
109 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|