1 | ## $Id: utils.py 7368 2011-12-18 08:16:16Z 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 grok |
---|
21 | import smtplib |
---|
22 | import string |
---|
23 | from random import SystemRandom as r |
---|
24 | from email.mime.text import MIMEText |
---|
25 | from waeup.sirp.interfaces import ISIRPUtils |
---|
26 | |
---|
27 | class SIRPUtils(grok.GlobalUtility): |
---|
28 | """A collection of methods subject to customization. |
---|
29 | """ |
---|
30 | grok.implements(ISIRPUtils) |
---|
31 | |
---|
32 | def sendMail(self,fullname,username,usertype,portal,body, |
---|
33 | email_from,email_to,subject): |
---|
34 | """Send an email with data provided by forms. |
---|
35 | """ |
---|
36 | config = grok.getSite()['configuration'] |
---|
37 | text = """Fullname: %s |
---|
38 | User Id: %s |
---|
39 | User Type: %s |
---|
40 | Portal: %s |
---|
41 | |
---|
42 | %s |
---|
43 | """ |
---|
44 | msg = MIMEText(text % (fullname,username,usertype,portal,body)) |
---|
45 | msg['From'] = '%s <%s>' % (fullname,email_from) |
---|
46 | msg['To'] = email_to |
---|
47 | msg['Subject'] = subject |
---|
48 | server = smtplib.SMTP(config.smtp_server) |
---|
49 | if config.smtp_requires_login: |
---|
50 | server.login(config.smtp_username,config.smtp_password) |
---|
51 | try: |
---|
52 | server.sendmail(email_from,email_to,msg.as_string()) |
---|
53 | except: |
---|
54 | return False |
---|
55 | server.quit() |
---|
56 | return True |
---|
57 | |
---|
58 | def fullname(self,firstname,lastname,middlename=None): |
---|
59 | # We do not necessarily have the middlename attribute |
---|
60 | if middlename: |
---|
61 | return string.capwords( |
---|
62 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
63 | else: |
---|
64 | return string.capwords( |
---|
65 | '%s %s' % (firstname, lastname)) |
---|
66 | |
---|
67 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
68 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
69 | |
---|
70 | def sendPassword(self,fullname,msg,username,password,login_url, |
---|
71 | email_to,subject): |
---|
72 | """Send an email with user credentials. |
---|
73 | """ |
---|
74 | config = grok.getSite()['configuration'] |
---|
75 | text = """Dear %s, |
---|
76 | |
---|
77 | %s |
---|
78 | Student Registration and Information Portal of |
---|
79 | %s. |
---|
80 | |
---|
81 | Your user name: %s |
---|
82 | Your password: %s |
---|
83 | Login page: %s |
---|
84 | |
---|
85 | Please remember your user name and keep |
---|
86 | your password secret! |
---|
87 | |
---|
88 | Regards |
---|
89 | """ |
---|
90 | msg = MIMEText(text % (fullname,msg,config.name,username,password,login_url)) |
---|
91 | msg['From'] = '%s <%s>' % (config.name_admin,config.email_admin) |
---|
92 | msg['To'] = email_to |
---|
93 | msg['Subject'] = subject |
---|
94 | server = smtplib.SMTP(config.smtp_server) |
---|
95 | if config.smtp_requires_login: |
---|
96 | server.login(config.smtp_username,config.smtp_password) |
---|
97 | try: |
---|
98 | server.sendmail(config.email_admin,email_to,msg.as_string()) |
---|
99 | except: |
---|
100 | return False |
---|
101 | server.quit() |
---|
102 | return True |
---|
103 | |
---|