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 | """ |
---|
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 storage(self): |
---|
43 | """Return the initial storage path of the data center. |
---|
44 | """ |
---|
45 | return os.path.dirname(__file__).replace('utils','files') |
---|
46 | |
---|
47 | def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, |
---|
48 | from_username,usertype,portal,body,subject): |
---|
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 | """ |
---|
59 | body = text % (from_name,from_username,usertype,portal,body) |
---|
60 | return send_mail( |
---|
61 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|
62 | |
---|
63 | def fullname(self,firstname,lastname,middlename=None): |
---|
64 | """Full name constructor. |
---|
65 | """ |
---|
66 | # We do not necessarily have the middlename attribute |
---|
67 | if middlename: |
---|
68 | return string.capwords( |
---|
69 | '%s %s %s' % (firstname, middlename, lastname)) |
---|
70 | else: |
---|
71 | return string.capwords( |
---|
72 | '%s %s' % (firstname, lastname)) |
---|
73 | |
---|
74 | def genPassword(self, length=8, chars=string.letters + string.digits): |
---|
75 | """Generate a random password. |
---|
76 | """ |
---|
77 | return ''.join([r().choice(chars) for i in range(length)]) |
---|
78 | |
---|
79 | |
---|
80 | def sendCredentials(self, user, password=None, login_url=None, msg=None): |
---|
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. |
---|
87 | """ |
---|
88 | subject = 'Your SIRP credentials' |
---|
89 | text = """Dear %s, |
---|
90 | |
---|
91 | %s |
---|
92 | Student Registration and Information Portal of |
---|
93 | %s. |
---|
94 | |
---|
95 | Your user name: %s |
---|
96 | Your password: %s |
---|
97 | Login page: %s |
---|
98 | |
---|
99 | Please remember your user name and keep |
---|
100 | your password secret! |
---|
101 | |
---|
102 | Please also note that passwords are case-sensitive. |
---|
103 | |
---|
104 | Regards |
---|
105 | """ |
---|
106 | config = grok.getSite()['configuration'] |
---|
107 | from_name = config.name_admin |
---|
108 | from_addr = config.email_admin |
---|
109 | rcpt_name = user.title |
---|
110 | rcpt_addr = user.email |
---|
111 | body = text % ( |
---|
112 | rcpt_name, msg,config.name,user.name,password,login_url) |
---|
113 | return send_mail( |
---|
114 | from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) |
---|